关闭

iOS_视频后台播放+远程控制

标签: iOS播放视频后台
185人阅读 评论(0) 收藏 举报
分类:

1、Plist加入

对于长时间运行的任务,需要在Info.plist添加一行,键为UIBackgroundModes,值为一个数组,可以包含如下几个字符串:

  • audio
  • location
  • voip
  • newsstand-content
  • external-accessory
  • bluetooth-central











第2步,导入音频播放框架

引用库:


AudioToolBox.framework

MediaPlayer.framework

CoreMedia.framework

AVFoundation.framework






第3步,

在appdelegate导入

#import <AVFoundation/AVFoundation.h>





第4步,

didfinishlaunch执行自定义方法

[self initBackGroundPlay];



/** @programmer beyond@xsism.com
 *  @brief  后台播放第1步
 *  @param  <#无#>
 *  @return <#无#>
 */
- (void)initBackGroundPlay
{
    AVAudioSession *session = [AVAudioSession sharedInstance];
    NSError *setCategoryError = nil;
    [session setCategory:AVAudioSessionCategoryPlayback error:&setCategoryError];
    NSError *activationError = nil;
    [session setActive:YES error:&activationError];
    
    
    //告诉系统,我们要接受远程控制事件,并在播放控制器直接进行响应
    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
    
    
}



第5步,在播放控制器中,直接响应即可


在viewDidLoad中,执行自定义方法

[self configNowPlayingInfoCenter];



#pragma mark - 锁屏播放
-(void)configNowPlayingInfoCenter{
    
    if (NSClassFromString(@"MPNowPlayingInfoCenter")) {
        
        NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
        [dict setObject:@"name" forKey:MPMediaItemPropertyTitle];
        NSString *singer = [NSString stringWithFormat:@"【%@】",kAppName];
        [dict setObject:singer forKey:MPMediaItemPropertyArtist];
//        [dict setObject:@"beyond" forKey:MPMediaItemPropertyAlbumTitle];
        
        UIImage *image = [UIImage imageNamed:@"ok01.jpg"];
        MPMediaItemArtwork *artwork = [[MPMediaItemArtwork alloc] initWithImage:image];
        [dict setObject:artwork forKey:MPMediaItemPropertyArtwork];
        
        [[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:dict];
        
    }
    
}

#pragma mark - 直接响应远程控制
// 响应远程音乐播放控制消息
- (void)remoteControlReceivedWithEvent:(UIEvent *)receivedEvent
{
    
    if (receivedEvent.type == UIEventTypeRemoteControl) {
        
        switch (receivedEvent.subtype) {
            case UIEventSubtypeRemoteControlPlay:
            {
                NSLog(@"RemoteControlEvents: play");
                [self pauseAndPlayBtnClicked:_operationView.xib_btn_pauseAndPlay];
            }
                break;
            case UIEventSubtypeRemoteControlPause:
            {
                NSLog(@"RemoteControlEvents: pause");
                [self pauseAndPlayBtnClicked:_operationView.xib_btn_pauseAndPlay];
            }
                break;
            case UIEventSubtypeRemoteControlTogglePlayPause:
                //                [[PlayController sharedInstance] pause];
                NSLog(@"无效,RemoteControlEvents: pause");
                break;
            case UIEventSubtypeRemoteControlNextTrack:
                //                [[PlayController sharedInstance] playModeNext];
            {
                NSLog(@"RemoteControlEvents: playModeNext");
                [self rewindBtnClicked:_operationView.xib_btn_forward];
            }
                break;
            case UIEventSubtypeRemoteControlPreviousTrack:
                //                [[PlayController sharedInstance] playPrev];
            {
                NSLog(@"RemoteControlEvents: playPrev");
                [self rewindBtnClicked:_operationView.xib_btn_rewind];
            }
                break;
            default:
                break;
        }
    }
}



1
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:360337次
    • 积分:8370
    • 等级:
    • 排名:第2340名
    • 原创:472篇
    • 转载:2篇
    • 译文:0篇
    • 评论:75条
    最新评论