Signing&Capabilites開啟Audio绢记,AirPlay,and Picture in Picture模式
應(yīng)用在后臺(tái)時(shí)播放聲音信息正卧,可以利用此模式播放無聲音頻蠢熄,APP進(jìn)入后臺(tái)播放無聲音頻,可以實(shí)現(xiàn)APP長(zhǎng)時(shí)間甭酰活
代碼如下
編寫音樂播放類
#import <Foundation/Foundation.h>
#import<AVFoundation/AVFoundation.h>
@interface BackgroundPlayer : NSObject<AVAudioPlayerDelegate>
@property(nonatomic,strong)AVAudioPlayer* player;
- (void)startPlayer;
- (void)stopPlayer;
@end
#import "BackgroundPlayer.h"
@implementation BackgroundPlayer
- (void)startPlayer
{
? ? if(_player&& [_playerisPlaying]) {
? ? ? ? return;
? ? }
? ? AVAudioSession *session = [AVAudioSession sharedInstance];
? ? [[AVAudioSession sharedInstance] setMode:AVAudioSessionModeDefault error:nil];
? ? NSString* route = [[[[[AVAudioSession sharedInstance] currentRoute] outputs] objectAtIndex:0] portType];
? ? if ([route isEqualToString:AVAudioSessionPortHeadphones] || [route isEqualToString:AVAudioSessionPortBluetoothA2DP] || [route isEqualToString:AVAudioSessionPortBluetoothLE] || [route isEqualToString:AVAudioSessionPortBluetoothHFP]) {
? ? ? ? if(@available(iOS10.0, *)) {
? ? ? ? ? ? [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? withOptions:(AVAudioSessionCategoryOptionMixWithOthers | AVAudioSessionCategoryOptionAllowBluetooth | AVAudioSessionCategoryOptionAllowBluetoothA2DP)
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? error:nil];
? ? ? ? }else{
? ? ? ? ? ? // Fallback on earlier versions
? ? ? ? }
? ? }else{
? ? ? ? [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? withOptions:(AVAudioSessionCategoryOptionMixWithOthers | AVAudioSessionCategoryOptionDefaultToSpeaker)
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? error:nil];
? ? }
? ? [session setActive:YESerror:nil];
? ? NSString *filePath = [[NSBundle mainBundle] pathForResource:@"payment" ofType:@"mp3"];
? ? NSURL*fileURL = [[NSURLalloc]initFileURLWithPath:filePath];
? ? if(!fileURL) {
? ? ? ? NSLog(@"找不到播放文件");
? ? }
? ? _player = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];
? ? [_player prepareToPlay];
? ? [_player setDelegate:self];
? ? _player.volume=0.0;
? ? _player.numberOfLoops = -1;
? ? BOOLret = [_playerplay];
? ? if(!ret) {
? ? ? ? NSLog(@"播放失敗play failed,please turn on audio background mode");
? ? }
}
- (void)stopPlayer
{
? ? if(_player) {
? ? ? ? [_playerstop];
? ? ? ? _player=nil;
? ? ? ? AVAudioSession *session = [AVAudioSession sharedInstance];
? ? ? ? [session setActive:NOerror:nil];
? ? ? ? NSLog(@"播放暫停stop in play background success");
? ? }
}
@end
在AppDelegate.m中添加監(jiān)聽
UIApplicationWillEnterForegroundNotification(應(yīng)用進(jìn)入前臺(tái)通知)UIApplicationDidEnterBackgroundNotification(應(yīng)用進(jìn)入后臺(tái)通知)
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appWillEnterForeground) name:UIApplicationWillEnterForegroundNotification object:nil];
? ? [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appDidEnterBackground) name:UIApplicationDidEnterBackgroundNotification object:nil];
@property(nonatomic,strong)BackgroundPlayer*player;
-(void)appWillEnterForeground{
if(self.player){
[self.player stopPlayBackgroundAlive];
}
}
-(void)appDidEnterBackground{
if(_player==nil){
_player=[[BackgroundPlayer alloc]init];
}
[self.player startPlayer];
}