iphone:播放音频播放列表中的背景是什么? [英] iphone: playing audio playlist in the background?

查看:153
本文介绍了iphone:播放音频播放列表中的背景是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用AVAudioPlayer后台播放的音频文件序列。当应用程序是不是在后台起着很好,但是当应用程序转到后台也不会播放第二首歌曲。

I'm trying to play sequence of audio files in the background using AVAudioPlayer. When the app is not in the background it plays well, but when the app goes to the background it won't play the second song.

下面是我的code:

-(void)playAudio:(NSString *)path{
    NSURL *url = [NSURL fileURLWithPath:path];

    NSError *error;
    AVAudioPlayer *ap = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
    [[AVAudioSession sharedInstance] setActive: YES error: nil];
    ap.delegate = self;

    mainController.audioPlayer = ap;
    [ap release];


    if (mainController.audioPlayer == nil)
        NSLog([error description]);
    else{


        UIBackgroundTaskIdentifier newTaskId = UIBackgroundTaskInvalid;

        [mainController.audioPlayer prepareToPlay];

        if([mainController.audioPlayer play]){

            newTaskId = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:NULL];

        }

        if (newTaskId != UIBackgroundTaskInvalid && bgTaskId != UIBackgroundTaskInvalid)
            [[UIApplication sharedApplication] endBackgroundTask: bgTaskId];

        bgTaskId = newTaskId;
    }

}

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{
    NSLog(@"finished now next...");
    [self playAudio:[self getNextAudio]];

}

我调试的应用程序,它似乎是当它即将播放第二首歌曲[mainController.audioPlayer玩]返回NO,这意味着它不能播放。
所以,你觉得呢?

I debugged the app and it seems that when it's about to play the second song [mainController.audioPlayer play] returns NO, which means it can't play. So what do you think?

更新

经过一些测试,似乎它并继续发挥正常只有当设备锁,但如果用户presses home键和应用程序转到问题仍然存在后台

After some testing it seems that it does continue to play properly only if the device locks, but if the user presses the home button and the app goes to the background the problem still remains

推荐答案

的解

只需添加[[UIApplication的sharedApplication] beginReceivingRemoteControlEvents]和其他一些调整。这一切都在这里
https://devforums.apple.com/message/264397

Just add [[UIApplication sharedApplication] beginReceivingRemoteControlEvents]; and some other tweaks. It's all here https://devforums.apple.com/message/264397

这篇关于iphone:播放音频播放列表中的背景是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆