IOS的后台模式播放音乐,显示专辑和图片信息

http://blog.sina.com.cn/s/blog_a5243c7f0101qdy8.html

IOS的后台模式播放音乐

引用库:


AudioToolBox.framework

MediaPlayer.framework

CoreMedia.framework

AVFoundation.framework


1. 找到工程中的*.plist文件,例如:PRemoteDemo-Info.plist。

在其中增加一个[Required background modes]配置项,

并增加子级Item,[App plays audio]


注:子级Item有三个,我们只要添加[App plays audio]即可。


2. 在工程的AppDelegate.m文件的didFinishLaunchingWithOptions方法中加入如下代码:

(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

    //这种方式后台,可以连续播放非网络请求歌曲。遇到网络请求歌曲就废,需要后台申请task

    

    AVAudioSession *session [AVAudioSession sharedInstance];

    NSError *setCategoryError nil;

    [session setCategory:AVAudioSessionCategoryPlayback error:&setCategoryError];

    NSError *activationError nil;

    [session setActive:YES error:&activationError];

    

    

    self.window [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];

    // Override point for customization after application launch.

    self.viewController [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease];

    self.window.rootViewController self.viewController;

    [self.window makeKeyAndVisible];

    return YES;

}


3. 播放本地歌曲,不多说,代码如下:


-(IBAction)doPlay:(id)sender{

    

    NSString *filepath [[NSBundle mainBundle] pathForResource:@"qhc" ofType:@"caf"];

    BOOL fileexit [[NSFileManager defaultManager] fileExistsAtPath:filepath];

    if (fileexit) {

        

        if (_player && [_player isPlaying]) {

            return;

        }

        

        [_player release];

        _player nil;

        _player [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL URLWithString:filepath] error:nil];

        [_player play];

        

    }

    

}


经过上面简单的处理后,已经可以在后台播放歌曲了。但是使用过qq,酷狗的播放器后,就会发现一个很炫的功能,就是在锁屏的状态下,可以调用播放器,并切换歌曲,而且还可以显示歌曲对应的图片。

之前没搞过播放器,一直不理解实现原理,百度了很多次,使用的关键字为ios播放器,百度总是找给我一批垃圾资料。

后来同事帮忙找了份资料,然后我换歌关键字,终于找到我需要的资料了,现在也自己整理下。


iOS播放器特效,锁屏状态下显示专辑图片和信息


1. 检查你的AppDelegate是不是继承于UIResponder,有的是自动生成,有的人习惯手写,如果是继承于NSObject请改为 AppDelegate UIResponder。


2. 设置锁屏状态下音乐播放的信息(歌曲信息和图片)

//设置锁屏状态,显示的歌曲信息

-(void)configNowPlayingInfoCenter{

    

    if (NSClassFromString(@"MPNowPlayingInfoCenter")) {

        

        NSMutableDictionary *dict [[NSMutableDictionary alloc] init];

        [dict setObject:@"name" forKey:MPMediaItemPropertyTitle];

        [dict setObject:@"singer" forKey:MPMediaItemPropertyArtist];

        [dict setObject:@"album" forKey:MPMediaItemPropertyAlbumTitle];

        

        UIImage *image [UIImage imageNamed:@"test.jpg"];

        MPMediaItemArtwork *artwork [[MPMediaItemArtwork alloc] initWithImage:image];

        [dict setObject:artwork forKey:MPMediaItemPropertyArtwork];

        

        [[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:dict];

        

    }

    

}


上面的if (NSClassFromString(@"MPNowPlayingInfoCenter"))语句,说是为了避免了版本兼容问题。

这个API貌似只出现在5里面。现在你可以发现,上面的代码可能不起作用,而且待机屏的播放暂停也不会起作用。


3. 在对应的页面加入控制语句。

-(void)viewDidAppear:(BOOL)animated{

    

    [super viewDidAppear:animated];

    

    //Once the view has loaded then we can register to begin recieving controls and we can become the first responder

    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

    [self becomeFirstResponder];

    

    [self configNowPlayingInfoCenter];

    

}


-(void)viewWillDisappear:(BOOL)animated{

    

    [super viewWillDisappear:animated];

    

    //End recieving events

    [[UIApplication sharedApplication] endReceivingRemoteControlEvents];

    [self resignFirstResponder];

    

}


本人是在单一的viewcontroller里面的调试的,所以如上面的语句添加。也可以自行调整位置.

完成以上的方法后,是不是觉得程序也没做什么调整处理嘛,运行上面的代码,会发现只有信息出来,但是播放控制还是不起作用。


4. 重写控制方法.

-(BOOL)canBecomeFirstResponder{

    return YES;

}


5. 加上控制逻辑的监听

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

    

    //if it is remote control event handle it correctly

    if (event.type == UIEventTypeRemoteControl{

        switch (event.subtype) {

            case UIEventSubtypeRemoteControlTogglePlayPause:

            {

                NSLog(@"UIEventSubtypeRemoteControlTogglePlayPause...");

                [self pauseOrPlay];

                break;

            }

            case UIEventSubtypeRemoteControlPlay:

            {

                NSLog(@"UIEventSubtypeRemoteControlPlay...");

                break;

            }

            case UIEventSubtypeRemoteControlPause:

            {

                NSLog(@"UIEventSubtypeRemoteControlPause...");

                break;

            }

            case UIEventSubtypeRemoteControlStop:

            {

                NSLog(@"UIEventSubtypeRemoteControlStop...");

                break;

            }

            case UIEventSubtypeRemoteControlNextTrack:

            {

                NSLog(@"UIEventSubtypeRemoteControlNextTrack...");

                break;

            }

            case UIEventSubtypeRemoteControlPreviousTrack:

            {

                NSLog(@"UIEventSubtypeRemoteControlPreviousTrack...");

                break;

            }

                

            default:

                break;

        }

    }

    

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值