码迷,mamicode.com
首页 > 其他好文 > 详细

音乐锁屏播放

时间:2015-09-18 00:48:01      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:

1、首先要音乐支持后台播放

(1)在info.plist中添加

Required background modes 

item 0 : App plays audio or streams audio/video using AirPlay
(2)设置AVAudioSession:
  AVAudioSession *session = [AVAudioSession sharedInstance];
    [session setActive:YES error:nil];
    [session setCategory:AVAudioSessionCategoryPlayback error:nil];

(3)在后台注册事件:

  [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

2、锁屏设置

判断锁屏事件,进行事件处理

-(void)remoteControlReceivedWithEvent:(UIEvent *)event
{
    switch (event.subtype) {
        case UIEventSubtypeRemoteControlPause:
            [self.currentAudioPlayer pause];
            break;
        case UIEventSubtypeRemoteControlNextTrack:
            if (self.rotateCell) {
                [self.rotateCell stopRotate];
                
                NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
               SWTMusic *music = self.musicArray[indexPath.item];
                [SWTMusicTool stopMusic:music.filename];
                int index = indexPath.item + 1;
                if (index >= self.musicArray.count) {
                    index = 0;
                }
                NSIndexPath *newIndexPath = [NSIndexPath indexPathForItem:index inSection:0];
                [self.tableView selectRowAtIndexPath:newIndexPath animated:YES scrollPosition:UITableViewScrollPositionTop];
                SWTMusicCell *cell = [self.tableView cellForRowAtIndexPath:newIndexPath];
                self.rotateCell = cell;
                [cell startRotate];
                SWTMusic *nextMusic = self.musicArray[index];
                [self showInfoInLockedScreen:nextMusic];
                self.currentAudioPlayer = [SWTMusicTool playMusic:nextMusic.filename];
                self.currentAudioPlayer.delegate = self;
                
            }

            break;
        case UIEventSubtypeRemoteControlPreviousTrack:
            if (self.rotateCell) {
                [self.rotateCell stopRotate];
                
                NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
                int index = indexPath.item - 1;
                if (index < 0) {
                    index = self.musicArray.count-1;
                }
                NSIndexPath *newIndexPath = [NSIndexPath indexPathForItem:index inSection:0];
                SWTMusic *music = self.musicArray[indexPath.item];
                [SWTMusicTool stopMusic:music.filename];
                
                [self.tableView selectRowAtIndexPath:newIndexPath animated:YES scrollPosition:UITableViewScrollPositionTop];
                SWTMusicCell *cell = [self.tableView cellForRowAtIndexPath:newIndexPath];
                self.rotateCell = cell;
                [cell startRotate];
                SWTMusic *nextMusic = self.musicArray[index];
                [self showInfoInLockedScreen:nextMusic];
                self.currentAudioPlayer = [SWTMusicTool playMusic:nextMusic.filename];
                self.currentAudioPlayer.delegate = self;
                
            }

            break;
        case UIEventSubtypeRemoteControlPlay:
           [self.currentAudioPlayer play];
            break;
        default:
            break;
    }
}

4、锁屏信息

-(void)showInfoInLockedScreen:(SWTMusic*)music
{
//  这种方式,不能将信息正确显示在锁屏上
//    NSMutableDictionary *info = [NSMutableDictionary dictionary];
//    info[MPMediaItemPropertyTitle] = music.name;
//    info[MPMediaItemPropertyArtist] = music.singer;
//    info[MPMediaItemPropertyArtwork] = [[MPMediaItemArtwork alloc] initWithImage:[UIImage imageNamed:music.icon]];
//    
//    [[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:info];
    

    NSMutableDictionary *songInfo = [ [NSMutableDictionary alloc] init];
       
    MPMediaItemArtwork *albumArt = [ [MPMediaItemArtwork alloc] initWithImage: [UIImage imageNamed:music.icon]];
    //设置播放进度条
    double currentTime = self.currentAudioPlayer.currentTime;
    double duration = self.currentAudioPlayer.duration ;

    [ songInfo setObject:music.name forKey:MPMediaItemPropertyTitle ];
    [ songInfo setObject:music.singer forKey:MPMediaItemPropertyArtist ];
    [ songInfo setObject:music.singer forKey:MPMediaItemPropertyAlbumTitle ];
    [ songInfo setObject:albumArt forKey:MPMediaItemPropertyArtwork ];

[songInfo setObject:@(currentTime) forKey:MPNowPlayingInfoPropertyElapsedPlaybackTime];
    [songInfo setObject:@(duration) forKey:MPMediaItemPropertyPlaybackDuration];
    [ [MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:songInfo ];


}

 

音乐锁屏播放

标签:

原文地址:http://www.cnblogs.com/aminopeptidase/p/4817954.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!