1、創(chuàng)建播放器時(shí)創(chuàng)建AVAudioSession
- AVAudioSession *session = [AVAudioSessionsharedInstance];
- [session setCategory:AVAudioSessionCategoryPlaybackerror:nil];
- [session setActive:YES error:nil];
2、在plist文件中添加字段
Required background modes
在這里添加后臺(tái)播放:
App plays audio or streams audio/video using AirPlay
3、在設(shè)備將要掛起app時(shí)添加下面代碼
- - (void)applicationWillResignActive:(UIApplication *)application {
-
- if ([MediaPlayerplayer].playStatus == MediaPlayerPlayStatusPlaying) {
- UIDevice* device = [UIDevicecurrentDevice];
- if ([devicerespondsToSelector:@selector(isMultitaskingSupported)]) {
- if(device.multitaskingSupported) {
- if(device.multitaskingSupported) {
- if ([MediaPlayerplayer].bgTaskId ==UIBackgroundTaskInvalid) {
- [MediaPlayerplayer].bgTaskId = [[UIApplicationsharedApplication]beginBackgroundTaskWithExpirationHandler:NULL];
- }
- }
- }
- }
- }
- }
在設(shè)備進(jìn)入前臺(tái)時(shí)添加下面代碼
- if ([MediaPlayerplayer].bgTaskId !=UIBackgroundTaskInvalid) {
- [[UIApplicationsharedApplication]endBackgroundTask:[MediaPlayerplayer].bgTaskId];
- [MediaPlayerplayer].bgTaskId =UIBackgroundTaskInvalid;
- }
使用這三步基本可以保證在后臺(tái)的長時(shí)間播放問題,且不會(huì)因?yàn)楹笈_(tái)掛起APP導(dǎo)致的播放停止問題。
更多建議: