2017-01-12 3 views
0

ich möchte die aktuell wiedergegebenen Audiodaten in UWP oder Windows Phone 8.1 aufnehmen/aufzeichnen, dasselbe geschieht mit der "MEE dj" UWP-App in der App, die aktuell wiedergegebene Audiodateien aufnehmen kann in der App. Wer weiß, teilen Sie Ihre Antwort bitte.Aufnahme/Aufnahme Derzeit wird Audio abgespielt

Antwort

1

wie Eingang einzustellen Knoten Musikwiedergabe statt mic von

Windows.Media.Audio namespace enthält AudioDeviceInputNode, AudioDeviceOutputNode, AudioFileInputNode, AudioFileOutputNode und so weiter. Mikrofon für die Eingabe ist AudioDeviceInputNode, aber für die Wiedergabe von Musikdatei müssen Sie AudioFileInputNode verwenden.

in Ordnung, aber wie kann ich das Audio erfassen und an Speicher

Zum Speichern auf Speicher speichern, wie ich oben sagte, müssen wir AudioFileOutputNode verwenden. Hier ist eine einfache Demo, Sie können eine Datei für die Aufnahme laden und eine Datei aus dem Speicher auswählen, um das Aufnahmeergebnis zu speichern. Code wie folgt:

XAML-Code

<StackPanel Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" Padding="50"> 
    <TextBlock x:Name="txtresult" ></TextBlock> 
    <Button x:Name="fileButton" Content="Load audio File for recording" Click="File_Click" MinWidth="120" MinHeight="45" Margin="0,20,0,0"/> 
    <Button x:Name="OutpuyfileButton" Content="Load output File for save the recording result" Click="OutpuyfileButton_Click" MinWidth="120" MinHeight="45" Margin="0,20,0,0"/> 
    <Button x:Name="graphButton" Content="Start playing" Click="Graph_Click" MinWidth="120" MinHeight="45" Margin="0,50,0,20"/> 
    <Button x:Name="graphrecord" Content="Begin recording" Click="graphrecord_Click" ></Button> 
</StackPanel> 

Code hinter

private AudioFileInputNode fileInput; 
    private AudioFileOutputNode fileOutputNode; 
    private AudioDeviceOutputNode deviceOutput; 
    private AudioGraph graph; 

    StorageFile outputfile; 

    public MainPage() 
    { 
     this.InitializeComponent(); 
    } 
    protected override async void OnNavigatedTo(NavigationEventArgs e) 
    { 
     await CreateAudioGraph(); 
    } 
    private async Task CreateAudioGraph() 
    { 
     // Create an AudioGraph with default settings 
     AudioGraphSettings settings = new AudioGraphSettings(AudioRenderCategory.Media); 
     CreateAudioGraphResult result = await AudioGraph.CreateAsync(settings); 
     if (result.Status != AudioGraphCreationStatus.Success) 
     { 
      // Cannot create graph 
      await new MessageDialog(String.Format("AudioGraph Creation Error because {0}", result.Status.ToString())).ShowAsync(); 
      return; 
     } 
     graph = result.Graph; 
     // Create a device output node 
     CreateAudioDeviceOutputNodeResult deviceOutputNodeResult = await graph.CreateDeviceOutputNodeAsync(); 
     if (deviceOutputNodeResult.Status != AudioDeviceNodeCreationStatus.Success) 
     { 
      // Cannot create device output node 
     txtresult.Text+="\n"+ String.Format("Device Output unavailable because {0}", deviceOutputNodeResult.Status.ToString()); 
      return; 
     } 
     deviceOutput = deviceOutputNodeResult.DeviceOutputNode; 
     txtresult.Text += "\n" + "Device Output Node successfully created"; 
    } 
    private async void File_Click(object sender, RoutedEventArgs e) 
    { 
     // If another file is already loaded into the FileInput node 
     if (fileInput != null) 
     { 
      fileInput.Dispose(); 
     } 
     FileOpenPicker filePicker = new FileOpenPicker(); 
     filePicker.SuggestedStartLocation = PickerLocationId.MusicLibrary; 
     filePicker.FileTypeFilter.Add(".mp3"); 
     filePicker.FileTypeFilter.Add(".wav"); 
     filePicker.FileTypeFilter.Add(".wma"); 
     filePicker.FileTypeFilter.Add(".m4a"); 
     filePicker.ViewMode = PickerViewMode.Thumbnail; 
     StorageFile file = await filePicker.PickSingleFileAsync(); 
     // File can be null if cancel is hit in the file picker 
     if (file == null) 
     { 
      return; 
     } 
     CreateAudioFileInputNodeResult fileInputResult = await graph.CreateFileInputNodeAsync(file); 
     if (AudioFileNodeCreationStatus.Success != fileInputResult.Status) 
     { 
      // Cannot read input file 
      await new MessageDialog(String.Format("Cannot read input file because {0}", fileInputResult.Status.ToString())).ShowAsync(); 
      return; 
     } 

     fileInput = fileInputResult.FileInputNode; 
     txtresult.Text += "\n" + "File load successfully,input nodes created"; 
    } 

    private void Graph_Click(object sender, RoutedEventArgs e) 
    { 
     if (graphButton.Content.Equals("Start playing")) 
     { 
      fileInput.AddOutgoingConnection(deviceOutput); 
      graph.Start();    
      graphButton.IsEnabled = false; 
     } 
    } 

    private async void OutpuyfileButton_Click(object sender, RoutedEventArgs e) 
    { 
     FileSavePicker saveFilePicker = new FileSavePicker(); 
     saveFilePicker.FileTypeChoices.Add("Pulse Code Modulation", new List<string>() { ".wav" }); 
     saveFilePicker.FileTypeChoices.Add("Windows Media Audio", new List<string>() { ".wma" }); 
     saveFilePicker.FileTypeChoices.Add("MPEG Audio Layer-3", new List<string>() { ".mp3" }); 
     saveFilePicker.SuggestedFileName = "New Audio Track"; 
     outputfile = await saveFilePicker.PickSaveFileAsync(); 
     // File can be null if cancel is hit in the file picker 
     if (outputfile == null) 
     { 
      return; 
     } 

     txtresult.Text +="\n"+ String.Format("Recording to {0}", outputfile.Name.ToString()); 
    } 
    private MediaEncodingProfile CreateMediaEncodingProfile(StorageFile file) 
    { 
     switch (file.FileType.ToString().ToLowerInvariant()) 
     { 
      case ".wma": 
       return MediaEncodingProfile.CreateWma(AudioEncodingQuality.High); 
      case ".mp3": 
       return MediaEncodingProfile.CreateMp3(AudioEncodingQuality.High); 
      case ".wav": 
       return MediaEncodingProfile.CreateWav(AudioEncodingQuality.High); 
      default: 
       throw new ArgumentException(); 
     } 
    } 

    private async void graphrecord_Click(object sender, RoutedEventArgs e) 
    { 
     if (graphrecord.Content.Equals("Begin recording")) 
     { 
      MediaEncodingProfile fileProfile = CreateMediaEncodingProfile(outputfile); 
      CreateAudioFileOutputNodeResult fileOutputNodeResult = await graph.CreateFileOutputNodeAsync(outputfile, fileProfile); 
      if (fileOutputNodeResult.Status != AudioFileNodeCreationStatus.Success) 
      { 
       // FileOutputNode creation failed 
       await new MessageDialog(String.Format("Cannot create output file because {0}", fileOutputNodeResult.Status.ToString())).ShowAsync(); 
       return; 
      } 
      fileOutputNode = fileOutputNodeResult.FileOutputNode; 
      fileInput.AddOutgoingConnection(fileOutputNode); 
      graphrecord.Content = "Stop recording"; 
     } 
     else 
     { 
      graph.Stop(); 
      TranscodeFailureReason finalizeResult = await fileOutputNode.FinalizeAsync(); 
      if (finalizeResult != TranscodeFailureReason.None) 
      { 
       // Finalization of file failed. Check result code to see why 
       await new MessageDialog(String.Format("Finalization of file failed because {0}", finalizeResult.ToString())).ShowAsync(); 
       return; 
      } 
      txtresult.Text += "\n" + "Recording completed"; 
      graphrecord.IsEnabled = false; 
     } 
    } 

Für andere komplexe Funktionen finden Sie noch die offizielle Probe verweisen.

+0

Oh Danke, es funktioniert sehr gut ...... :) –

1

Sie können die APIs im Namespace Windows.Media.Audio verwenden, um Audiodiagramme für Audio-Routing-, Misch- und Verarbeitungsszenarien zu erstellen. Für die Erstellung von Audiodiagrammen verweisen Sie bitte auf this article.

Ein Audiodiagramm ist eine Gruppe von miteinander verbundenen Audioknoten. Die Audiodatei, die Sie aufnehmen möchten, liefert die "Audio Input Nodes" und "Audio Output Nodes" sind das Ziel für Audio, das vom Graphen verarbeitet wird. Audio kann aus dem Graphen zu den Ziel-Audiodateien geroutet werden. In der "MeeDJ" Windows Store App kann es zwei Audio mischen und in einem aufnehmen. In dieser Situation können wir "Submix-Knoten" verwenden, die Audio von einem oder mehreren Knoten aufnehmen und zu einer einzigen Ausgabe kombinieren.

Und zum Starten und Stoppen der Aufzeichnung können wir versuchen, Starting and stopping audio graph nodes zu implementieren. Sie können auch versuchen, Adding audio effects als "MeeDJ" tat.

Weitere Funktionen und Beispielcode verweisen auf die official sample.

+0

OK, aber wie kann ich das Audio erfassen und speichern –

+0

Alle gegebenen Szenario in Audio-Erstellung Beispiel ist Aufnahmestudio über Mikrofon nicht zum Abspielen von Audio –

+1

@ShubhamSahu die Probe, die Sie für die Aufnahme über Mikrofon gefunden, weil die Inputnodes Mikrofoneingang sind . Detail Lesen des Szenarios1 und des Dokuments, das Sie finden, können Sie tun, was Sie wollen. Neben dem Beispiel ist als Beispiel, es kann nicht die gleiche Demo wie Sie wollen, auch ich kann nicht für Sie schreiben. Die APIs unterstützen, was Sie wollen, ich biete bereits, wie zu tun, aber Details müssen Sie tun. –

Verwandte Themen