【问题标题】:Unable to create pause and resume functionality in an Audio recording app.Windows 8.1 Store App无法在录音应用程序中创建暂停和恢复功能。Windows 8.1 应用商店应用程序
【发布时间】:2016-08-15 21:06:53
【问题描述】:

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

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 Failed 事件被引发,并给出以下错误消息。

错误消息是:“媒体设备无法从停止状态进入暂停状态”,即使MediaCapture 不支持 Windows 8.1 中的暂停方法。

目前录音也保存在音乐库文件夹中,但我可以做些什么来将录音保存在 C 盘的特定文件夹中。

请帮帮我!

【问题讨论】:

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


    【解决方案1】:

    我对流媒体没有经验,但在保存部分,您可以使用 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

    【讨论】:

    • 谢谢,但是,我想要做的是将文件保存到我选择的位置而不提示 FilePicker,就像我给出了要保存的文件的完整路径并且文件保存在那个位置。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-18
    • 1970-01-01
    相关资源
    最近更新 更多