如何使用AVAudioPlayer在后台播放音乐?

时间:2021-05-31 07:27:03

I currently use AVAudioPlayer to play music while in the foreground, but when I press the home button my application goes to the background and the music stops. I set the category for my AVAudioSession to AVAudioSessionCategoryPlayback and set active to YES. Additionally, I added the audio key to UIBackgroundModes in my Info.plist.

我目前使用AVAudioPlayer在前台播放音乐,但当我按下home按钮时,我的应用程序会转到后台,音乐就会停止。我将AVAudioSession的类别设置为AVAudioSessionCategoryPlayback,并将active设置为YES。此外,我在Info.plist中向uibackgroundmode添加了音频键。

Is there anything else I need to do to enable background music playback?

我还需要做什么才能让背景音乐播放?

1 个解决方案

#1


2  

There's apparently a bug in the simulator - it doesn't work there, while it works fine on the device.

很明显,在模拟器中有一个错误——它在那里不工作,但它在设备上工作得很好。

(Edit: and by "doesn't work" I mean the sound stops when the app enters background, yet the player's status remains "playing" and it seems to keep "playing", although you can't hear it).

(编辑:我说的“不工作”是指当应用程序进入后台时,声音停止了,但玩家的状态仍然是“播放”,而且似乎还在“播放”,尽管你听不到)。

#1


2  

There's apparently a bug in the simulator - it doesn't work there, while it works fine on the device.

很明显,在模拟器中有一个错误——它在那里不工作,但它在设备上工作得很好。

(Edit: and by "doesn't work" I mean the sound stops when the app enters background, yet the player's status remains "playing" and it seems to keep "playing", although you can't hear it).

(编辑:我说的“不工作”是指当应用程序进入后台时,声音停止了,但玩家的状态仍然是“播放”,而且似乎还在“播放”,尽管你听不到)。