我刚开始测试这个非常简单的录音应用程序,它是通过Monotouch在今天的实际iPhone设备上构建的。我遇到了AVAudioRecorder
和AVPlayer
对象在首次使用后似乎重复使用的问题,我想知道如何解决它。
基本概述
该申请包括以下三个部分:
工作流
创建录音时,用户将单击“录音列表”区域中的“添加”按钮,应用程序将按下新录音视图控制器。
在新录制控制器中,可以使用以下变量:
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 )
答案 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.