无法在录音应用程序中创建暂停和恢复功能.Windows 8.1 Store App

时间:2016-04-22 09:44:35

标签: c# windows-store-apps windows-8.1 windows-store

我尝试使用以下代码生成暂停和恢复功能。

private async void RecordBtn_Click(object sender, RoutedEventArgs e)
{
   if (IsNewRecording)
   {
         mediaFile = await Windows.Storage.KnownFolders.MusicLibrary.CreateFileAsync("newFile.mp3", Windows.Storage.CreationCollisionOption.ReplaceExisting);
         MediaEncodingProfile encodingProfile = null;
         switch (SelectedFormat)
         {
             case AudioEncodingFormat.Mp3:
             encodingProfile = MediaEncodingProfile.CreateMp3(SelectedQuality);
             break;
             case AudioEncodingFormat.Mp4:
             encodingProfile = MediaEncodingProfile.CreateM4a(SelectedQuality);
             break;
             case AudioEncodingFormat.Wma:
             encodingProfile = MediaEncodingProfile.CreateWma(SelectedQuality);
             break;
             default:
             throw new ArgumentOutOfRangeException();
          }

          await CaptureMedia.StartRecordToStorageFileAsync(encodingProfile, mediaFile);
          UpdateRecordingControls(RecordingMode.Recording);
          DishTimer.Start();
          IsNewRecording = false;
          RecordBtn.Content = "Pause";
          return;
     }
    else if (!IsNewRecording && !IsPaused)
    {
         UpdateRecordingControls(RecordingMode.Paused);
         DishTimer.Stop();
         await CaptureMedia.StopRecordAsync();
         IsPaused = true;
         RecordBtn.Content = "Resume";
         return;
     }
     else if (!IsNewRecording && IsPaused)
     {
          MediaEncodingProfile encodingProfile = null;
          encodingProfile = MediaEncodingProfile.CreateMp3(SelectedQuality);
          await CaptureMedia.StartRecordToStorageFileAsync(encodingProfile, mediaFile);
          DishTimer.Start();
          IsPaused = false;
          RecordBtn.Content = "Pause";
          return;
     }
 }

停止录制。

private async void StopBtn_Click(object sender, RoutedEventArgs e)
{
      await CaptureMedia.StopRecordAsync();
      UpdateRecordingControls(RecordingMode.Stopped); 
      DishTimer.Stop();
      InitTimer();
      IsNewRecording = true;
      RecordBtn.Content = "Record";   

 }

初始化MediaCapture。

private async Task InitMediaCapture()
{
     CaptureMedia = new MediaCapture();
     captureInitSettings = new MediaCaptureInitializationSettings();
     captureInitSettings.StreamingCaptureMode = StreamingCaptureMode.Audio;
     await CaptureMedia.InitializeAsync(captureInitSettings);
     CaptureMedia.Failed += MediaCaptureOnFailed;
     CaptureMedia.RecordLimitationExceeded += MediaCaptureOnRecordLimitationExceeded;

}

和OnNavigatedTo事件如下。

protected async override void OnNavigatedTo(NavigationEventArgs e)
 {
      await InitMediaCapture();// Mic disconnection Exception thrown if no device is found
      LoadAudioEncodings();
      LoadAudioQualities();
      UpdateRecordingControls(RecordingMode.Initializing);
      InitTimer();

 }

我面临的另一个问题是,当应用程序最小化或窗口被锁定并且再次启动录制时MediaCapture引发了失败事件,并显示以下错误消息。

错误消息为:&#34;媒体设备无法从停止状态进入暂停状态&#34;,即使MediaCapture没有Windows 8.1支持的暂停方法。< / p>

目前,录音也保存在音乐库文件夹中,但是如何将录音保存在C盘的特定文件夹中。

请帮助我!

1 个答案:

答案 0 :(得分:0)

我没有使用流媒体的经验,但在保存部分,您可以使用fileSavePicker。根据您的代码,录制应该生成一个StorageFile。

var fileSavePicker = new FileSavePicker();
fileSavePicker.FileTypeChoices.Add(".mp3 audio", new List { ".mp3" });
fileSavePicker.DefaultFileExtension = “.mp3”;
fileSavePicker.SuggestedFileName = “name.mp3”;
fileSavePicker.SettingsIdentifier = "pickerName";
fileSavePicker.SuggestedSaveFile = //your storage file here
fileSavePicker.SuggestedStartLocation = //select start location

var file = await fileSavePicker.PickSaveFileAsync();

if (file != null)
{
    Windows.Storage.CachedFileManager.DeferUpdates(file);

    //override file with content, text example below
    //await Windows.Storage.FileIO.WriteTextAsync(file, "yourconten");

    Windows.Storage.Provider.FileUpdateStatus status = 
        await Windows.Storage.CachedFileManager.CompleteUpdatesAsync(file);

    if (status == Windows.Storage.Provider.FileUpdateStatus.Complete)
    {
        //operation done
    }
    else
    {
        //something went wrong
    }
}

更多信息:https://msdn.microsoft.com/en-us/windows/uwp/files/quickstart-save-a-file-with-a-picker