博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
音频(网络,非本地)在后台连续播放…
阅读量:4071 次
发布时间:2019-05-25

本文共 2111 字,大约阅读时间需要 7 分钟。

 

原帖地址:http://www.cocoachina.com/bbs/read.php?tid=106913&page=1

 

用MPMoviePlayerController播放的网络上的音频。

想让程序在按了home键,切到后台以后,能继续播放音频,并且一个音频结束后,能再播放下一个~~
现在,切换到后台后,正在播放的音频可以继续播放,没有问题;
但是,经Debug发现,当前的音频播放完了后,MPMoviePlayerPlaybackDidFinishNotification注册的回调函数虽然是被响应了,
程序是停在了第一行,死也不继续运行了~~

 

申请后台任务:

 

两处关键代码:

 

第一个:xxxAppDelegate.m中

 

- (void)applicationDidEnterBackground:(UIApplication *)application {

 

[application beginReceivingRemoteControlEvents];

}

 

 

 

第二个:播放器的play方法处

 

if([UIApplication sharedApplication].applicationState== UIApplicationStateBackground) {

 

[_player play];

 

UIApplication*app = [UIApplication sharedApplication];

UIBackgroundTaskIdentifier newTask = [app beginBackgroundTaskWithExpirationHandler:nil];
if(bgTask!= UIBackgroundTaskInvalid) {

 

[app endBackgroundTask: bgTask];

}
bgTask = newTask;
}
else {

 

[_player play];

}

 

以上!!

2. 锁屏状态下对播放器的控制

在你的xxxAppDelegate.m中,添加如下代码:

- (void)applicationDidEnterBackground:(UIApplication *)application {



        [application beginReceivingRemoteControlEvents];
}

- (void)applicationWillEnterForeground:(UIApplication *)application {


        [application endReceivingRemoteControlEvents];
}

- (void)remoteControlReceivedWithEvent:(UIEvent *)event {


if(event.type== UIEventTypeRemoteControl)  {


NSLog(@"Remote Control Type: %d", event.subtype);

switch (event.subtype) {


case UIEventSubtypeRemoteControlTogglePlayPause:

                                [你的播放器的 播放/暂停方法];

                                break;


case UIEventSubtypeRemoteControlNextTrack:

                                [你的播放器的 播放下一首歌的方法];

                                break;


                 case UIEventSubtypeRemoteControlPreviousTrack:

                                [你的播放器的 播放上一首歌的方法];

                         break;

                     default:
                                break;
                }
        }
}


3. 锁屏状态下当前播放曲目的信息表示

- (void)configNowPlayingInfoCenter {


Class playingInfoCenter = NSClassFromString(@"MPNowPlayingInfoCenter");
     if (playingInfoCenter) {


                MPNowPlayingInfoCenter*center = [MPNowPlayingInfoCenterdefaultCenter];

                // 当前播放歌曲的图片
         MPMediaItemArtwork *artwork = [[MPMediaItemArtwork allocinitWithImage:你想要使用的图片];

NSDictionary*songInfo = [NSDictionary dictionaryWithObjectsAndKeys:艺术家MPMediaItemPropertyArtist,
                                                                                                                                                 歌曲名MPMediaItemPropertyTitle,
                                                                                                                                                 artwork, MPMediaItemPropertyArtwork
                                                                                                                                                 专辑名MPMediaItemPropertyAlbumTitle,
                                                                                                                                                 nil];
                center.nowPlayingInfo = songInfo;

                [artwork release];
        }
}

转载地址:http://tseji.baihongyu.com/

你可能感兴趣的文章
《天亮了》韩红
查看>>
Windows CE下USB摄像头驱动开发(以OV511为例,附带全部源代码以及讲解) [转]
查看>>
出现( linker command failed with exit code 1)错误总结
查看>>
iOS开发中一些常见的并行处理
查看>>
iOS获取手机的Mac地址
查看>>
ios7.1发布企业证书测试包的问题
查看>>
如何自定义iOS中的控件
查看>>
iOS 开发百问
查看>>
Mac环境下svn的使用
查看>>
github简单使用教程
查看>>
如何高效利用GitHub
查看>>
环境分支-git版本管理
查看>>
uni-app 全局变量
查看>>
js判断空对象的几种方法
查看>>
java 不用递归写tree
查看>>
springboot2 集成Hibernate JPA 用 声明式事物
查看>>
fhs-framework jetcache 缓存维护之自动清除缓存
查看>>
SpringBoot 动态编译 JAVA class 解决 jar in jar 的依赖问题
查看>>
fhs-framework springboot mybatis 解决表关联查询问题的关键方案-翻译服务
查看>>
ZUUL2 使用场景
查看>>