在 MonoTouch 中使用 AVPlayer 播放背景音频?

发布于 2024-12-04 07:03:09 字数 1059 浏览 0 评论 0原文

首先,我知道这是一个类似的基于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

暮倦 2024-12-11 07:03:09

好吧,我觉得有点傻。事实证明我的关键 UIApplicationModes 前面有一个空格。哎哟!

好吧,对于任何遇到类似问题的人,请确保您的 info.plist 看起来像这样

<key>UIBackgroundModes</key>
<array>
 <string>audio</string>
</array>

,并像这样设置您的播放代码(记住,这是 Monotouch):

AVAudioSession audioSession = AVAudioSession.SharedInstance();
NSError error;
audioSession.SetCategory(AVAudioSession.CategoryPlayback.ToString(),out error);
        audioSession.SetActive(true,out error);

        UIApplication.SharedApplication.BeginReceivingRemoteControlEvents();

        AudioPlayer = new AVPlayer();

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

<key>UIBackgroundModes</key>
<array>
 <string>audio</string>
</array>

and setup your playback code like this (remember, this is Monotouch):

AVAudioSession audioSession = AVAudioSession.SharedInstance();
NSError error;
audioSession.SetCategory(AVAudioSession.CategoryPlayback.ToString(),out error);
        audioSession.SetActive(true,out error);

        UIApplication.SharedApplication.BeginReceivingRemoteControlEvents();

        AudioPlayer = new AVPlayer();
谁把谁当真 2024-12-11 07:03:09

看来您没有正确设置 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.

说不完的你爱 2024-12-11 07:03:09

我遇到了类似的问题,一个视图正在录制,而另一个视图则回放该录制内容。录音有时只能工作(会产生 5kb 文件),但都是空的。没有显示错误消息。经过几个小时的沮丧之后,我发现了 Dermot 的上述帖子,并通过在 AVAudioRecorder 之前添加 AVAudioSession 使其工作:

AVAudioSession audioSession = AVAudioSession.SharedInstance ();

            audioSession.SetCategory (AVAudioSession.CategoryRecord,out error);
            audioSession.SetActive(true, out error);


        recorder = AVAudioRecorder.ToUrl (url, settings, out error);

以及相同的代码,但更改了 CategoryRecord代码> 到CategoryPlayback

audioSession.SetCategory (AVAudioSession.CategoryPlayback,out error);

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 the AVAudioRecorder:

AVAudioSession audioSession = AVAudioSession.SharedInstance ();

            audioSession.SetCategory (AVAudioSession.CategoryRecord,out error);
            audioSession.SetActive(true, out error);


        recorder = AVAudioRecorder.ToUrl (url, settings, out error);

And the same code but changed CategoryRecord to CategoryPlayback.

audioSession.SetCategory (AVAudioSession.CategoryPlayback,out error);
一影成城 2024-12-11 07:03:09

我知道这是一个老问题,但只是补充一下我的经历:

  1. AVAudioSession.SharedInstance ();在我的设备上不起作用,但创建一个新实例 AVAudioSession audiosession = new AVAudioSession ();

  2. 在info.plist中RequiredBackgroundModes值现在是“音频内容”而不是“音频”

希望有帮助。

I know this is an old question but just to add what I experienced:

  1. AVAudioSession.SharedInstance (); did not work on my device but creating a new instance does AVAudioSession audiosession = new AVAudioSession ();

  2. In info.plist RequiredBackgroundModes value is now "Audible content" as opposed to "Audio"

Hope that helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文