使用Monotouch在iPhone上重复使用音频和录音

时间:2012-11-14 17:48:26

标签: ios audio xamarin.ios recording

我刚开始测试这个非常简单的录音应用程序,它是通过Monotouch在今天的实际i​​Phone设备上构建的。我遇到了AVAudioRecorderAVPlayer对象在首次使用后似乎重复使用的问题,我想知道如何解决它。

基本概述

该申请包括以下三个部分:

  • 录音列表(TableViewController)
  • 录制详细信息(ViewController)
  • 新录制(ViewController)

工作流

创建录音时,用户将单击“录音列表”区域中的“添加”按钮,应用程序将按下新录音视图控制器。

在新录制控制器中,可以使用以下变量:

AVAudioRecorder recorder;
AVPlayer player;

每个在使用之前都已初始化:

//Initialized during the ViewDidLoad event
recorder = AVAudioRecorder.Create(audioPath, audioSettings, out error);

//Initialized in the "Play" event
player = new AVPlayer(audioPath);

每项工作都适用于新录制控制器区域的初始加载,但是任何进一步的尝试似乎都不起作用(无音频播放

“细节”区域还有一个播放部分,允许用户播放任何录音,但是,与“新录音控制器”非常相似,播放也不起作用。

处置

它们都按如下方式处理(退出/离开视图时):

if(recorder != null)
{
    recorder.Dispose();
    recorder = null;
}

if(player != null)
{
    player.Dispose();
    player = null;
}

我还尝试删除任何可能保持任何对象“活着”的观察者,希望能够解决问题,并确保每次都显示新录制区域的每个显示,但是我仍然没有收到初始录制会话后播放音频。

如果有必要,我很乐意提供更多代码。 (这是使用MonoTouch 6.0.6

1 个答案:

答案 0 :(得分:1)

经过进一步调查后,我确定问题是由AudioSession引起的,因为录音和播放都发生在同一个控制器内。

我确定的两个解决方案如下:

解决方案1 ​​ (AudioSessionCategory.PlayAndRecord)

//A single declaration of this will allow both AVAudioRecorders and AVPlayers
//to perform alongside each other.
AudioSession.Category = AudioSessionCategory.PlayAndRecord;

//Upon noticing very quiet playback, I added this second line, which allowed
//playback to come through the main phone speaker
AudioSession.OverrideCategoryDefaultToSpeaker = true;

解决方案2 (AudioSessionCategory.RecordAudio& AudioSessionCategory.MediaPlayback)

void YourRecordingMethod()
{
     //This sets the session to record audio explicitly
     AudioSession.Category = AudioSessionCategory.RecordAudio;
     MyRecorder.record();  
}

void YourPlaybackMethod()
{
     //This sets the session for playback only
     AudioSession.Category = AudioSessionCategory.MediaPlayback;
     YourAudioPlayer.play();
}

有关使用AudioSession的一些其他信息,请访问 Apple's AudioSession Development Area.