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

来源:互联网 发布:润和软件同花顺 编辑:程序博客网 时间:2024/05/14 23:44

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执行自定义方法

[selfinitBackGroundPlay];



/** @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中,执行自定义方法

[selfconfigNowPlayingInfoCenter];



#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
原创粉丝点击