在 MonoTouch 中使用 AVPlayer 播放背景音频?
首先,我知道这是一个类似的基于 Objective C 的问题的副本,但没有得到回答。
我在我的应用程序中使用 AVPlayer 来播放音频。我让它播放音频,正确设置其音频会话(我认为,当我在应用程序中播放音轨时,播放图标出现在状态栏中,并且我的应用程序图标出现在多任务处理坞中的播放器传输控件旁边)。但是,当我按主页按钮时,我无法让它继续在后台播放。音乐逐渐消失并停止。
我的 info.plist 中有以下键:
<key>UIBackgroundModes</key>
<array>
<string>audio</string>
</array>
在处理播放的类中,我有:
AVAudioSession audioSession = AVAudioSession.SharedInstance();
NSError error;
audioSession.SetCategory(AVAudioSession.CategoryPlayback.ToString(),out error);
audioSession.SetActive(true,out error);
UIApplication.SharedApplication.BeginReceivingRemoteControlEvents();
int taskID = 0;
taskID = UIApplication.SharedApplication.BeginBackgroundTask(delegate
{
if(taskID !=0)
{
UIApplication.SharedApplication.EndBackgroundTask(taskID);
taskID = 0;
}
});
PlaylistManager = new ApolloPlaylistManager();
AudioPlayer = new AVPlayer();
不确定是否需要 BeginBackgroundTask 部分,但如果它输入或输出则不起作用,我实际上很确定我不需要它。任何提示都非常感谢,因为我已经坚持了几天:-(
Firstly, I understand this is a copy of a similar objective C based question, but it wasn't answered.
Im using AVPlayer for the audio in my app. I have it playing audio, setting up its audio session correctly (I think, the play icon appears in the status bar when I play audio tracks within my app, and my app icon appears beside the player transport controls in the multitasking dock). However, I cannot get it to continue playing in the background when I press the Home button. The music just fades out and stops.
I have the following key in my info.plist:
<key>UIBackgroundModes</key>
<array>
<string>audio</string>
</array>
In my class that handles playback, I have:
AVAudioSession audioSession = AVAudioSession.SharedInstance();
NSError error;
audioSession.SetCategory(AVAudioSession.CategoryPlayback.ToString(),out error);
audioSession.SetActive(true,out error);
UIApplication.SharedApplication.BeginReceivingRemoteControlEvents();
int taskID = 0;
taskID = UIApplication.SharedApplication.BeginBackgroundTask(delegate
{
if(taskID !=0)
{
UIApplication.SharedApplication.EndBackgroundTask(taskID);
taskID = 0;
}
});
PlaylistManager = new ApolloPlaylistManager();
AudioPlayer = new AVPlayer();
Not sure if I need that BeginBackgroundTask part, but it doesn't work if its in or out, im actually pretty sure I dont need it. Any tips at all greatly appreciated, as I've been stuck on this for a few days :-(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
好吧,我觉得有点傻。事实证明我的关键 UIApplicationModes 前面有一个空格。哎哟!
好吧,对于任何遇到类似问题的人,请确保您的 info.plist 看起来像这样
,并像这样设置您的播放代码(记住,这是 Monotouch):
Ok, I feel a little stupid. It turns out my key UIApplicationModes had a space in front of it. Doh!
Well, to anyone having similar problems, ensure your info.plist looks like this
and setup your playback code like this (remember, this is Monotouch):
看来您没有正确设置
AVAudioSession
。看看这个和这个。我希望这些提示能够引导您走向正确的方向。
Seems you didn't set up the
AVAudioSession
properly. Take a look at this and this.I hope those tips guide you in the right direction.
我遇到了类似的问题,一个视图正在录制,而另一个视图则回放该录制内容。录音有时只能工作(会产生 5kb 文件),但都是空的。没有显示错误消息。经过几个小时的沮丧之后,我发现了 Dermot 的上述帖子,并通过在
AVAudioRecorder
之前添加AVAudioSession
使其工作:以及相同的代码,但更改了
CategoryRecord
代码> 到CategoryPlayback
。I had a similar problem where one view was recording and another view played back that recording. Recording would only work sometimes (would produce 5kb files) that were empty. No error message displayed. After hours of frustration I came across the above post by Dermot and got it to work by adding
AVAudioSession
before theAVAudioRecorder
:And the same code but changed
CategoryRecord
toCategoryPlayback
.我知道这是一个老问题,但只是补充一下我的经历:
AVAudioSession.SharedInstance ();在我的设备上不起作用,但创建一个新实例 AVAudioSession audiosession = new AVAudioSession ();
在info.plist中RequiredBackgroundModes值现在是“音频内容”而不是“音频”
希望有帮助。
I know this is an old question but just to add what I experienced:
AVAudioSession.SharedInstance (); did not work on my device but creating a new instance does AVAudioSession audiosession = new AVAudioSession ();
In info.plist RequiredBackgroundModes value is now "Audible content" as opposed to "Audio"
Hope that helps.