Objective-C: How to get AVPlayer to continue playing in the background

I know that there are many questions, such as mine, but nothing works for me.

I try to AVPlayerkeep playing when I exit the application, I implemented the following Singleton Class:

.h file:

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

@interface LiveStreamSingleton : NSObject<AVAudioPlayerDelegate>{

}

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

.m file:

#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

I installed [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];to play in the background, but I tried it on my real ipad, it does not work.

Any ideas?

+4
source share
1 answer

add a key with the name Required background modes in the properties file (.plist) ..

as shown below.

enter image description here

and add the following code to

Objective-c

Appdelegate.h

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

AppDelegate.m

in the didFinishLaunchingWithOptions application

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

Swift

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 {

        }
    }
}

Hope this helps.

+10
source

All Articles