Objective-C:如何使AVPlayer在后台保持播放

4

我知道像我这样的问题有很多,但是没有一个能对我起作用。

我正在尝试让AVPlayer在退出应用程序时继续播放,我已经实现了以下的Singleton Class

.h 文件:

#import <AVFoundation/AVFoundation.h>
#import <Foundation/Foundation.h>

@interface LiveStreamSingleton : NSObject<AVAudioPlayerDelegate>{

}

+(LiveStreamSingleton *)sharedInstance;
-(void)playStream;
-(void)stopStream;
-(bool)status;
@end

.m文件:

#import "LiveStreamSingleton.h"

static LiveStreamSingleton *sharedInstance = nil;
@interface LiveStreamSingleton (){
    AVPlayer *audioPlayer;

}

@end
@implementation LiveStreamSingleton

+ (LiveStreamSingleton*) sharedInstance {
    static dispatch_once_t _singletonPredicate;
    static LiveStreamSingleton *_singleton = nil;

    dispatch_once(&_singletonPredicate, ^{
        _singleton = [[super allocWithZone:nil] init];
    });

    return _singleton;
}

+ (id) allocWithZone:(NSZone *)zone {
    return [self sharedInstance];
}

-(void)playStream{

    //NSError *error = nil;
    NSURL *urlStream;
    NSString *urlAddress = @"http://198.178.123.23:8662/stream/1/;listen.mp3";
    urlStream = [[NSURL alloc] initWithString:urlAddress];
    AVURLAsset *avAsset = [AVURLAsset URLAssetWithURL:urlStream options:nil];
    AVPlayerItem *playerItem = [AVPlayerItem playerItemWithAsset:avAsset];
    audioPlayer = [AVPlayer playerWithPlayerItem:playerItem];
    //This enables background music playing
    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
    audioPlayer = [AVPlayer playerWithURL:urlStream];
    if(!audioPlayer.error){
        NSLog(@"Trying to play from singleton!");
        [audioPlayer play];
        NSLog(@"rate: %f",audioPlayer.rate);
    }
}

-(void)stopStream{
    NSLog(@"Trying to stop from singleton!");
    [audioPlayer pause];
}


-(bool)status{
    bool stat;
    if(audioPlayer.rate > 0){
        stat = true;
    }else{
        stat = false;
    }
    return stat;
}

@end

我已经设置了[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];来使应用在后台播放,但是我在真实的iPad上尝试时,它并没有起作用。 有什么想法吗?

3
https://dev59.com/Vn3aa4cB1Zd3GeqPjuta#22777813 - luk2302
1个回答

10

在属性列表(.plist)文件中添加一个名为所需背景模式的键,如下图所示:

enter image description here

然后在以下位置添加以下代码:

Objective-C

AppDelegate.h

#import <AVFoundation/AVFoundation.h>
#import <AudioToolbox/AudioToolbox.h>

AppDelegate.m

application:didFinishLaunchingWithOptions: 方法中

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive:YES error:nil];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

快捷

import AVFoundation
import AudioToolbox
class AppDelegate: UIResponder, UIApplicationDelegate
{
    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool
    {

        do {
            try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
            try AVAudioSession.sharedInstance().setActive(true)
            UIApplication.sharedApplication().beginReceivingRemoteControlEvents()
        }
        catch {

        }
    }
}

希望它有所帮助。


太好了!非常感谢,只有一个问题,[[AVAudioSession sharedInstance] setDelegate:self]; selfDelegate已经被弃用了,我该如何解决这个问题? - Fadi Obaji
@FadiObaji 我认为不需要它。您只需要使用SetActive。删除那行代码 (Y) - Ashish Kakkad
@FadiObaji 很棒! :) - Ashish Kakkad

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接