日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【必看】AVAudioPlayer播放声音时加入了后台播放功能,看懂了吗?

發布時間:2023/12/31 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【必看】AVAudioPlayer播放声音时加入了后台播放功能,看懂了吗? 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

-(void)setAudioPlayer{

? ? ? ? //加入播放按鈕 ? ? ? ?

? ? ? ? if (playButton==nil) {

? ? ? ? ? ? ? ? playButton = [UIButton buttonWithType:UIButtonTypeCustom];

? ? ? ? ? ? ? ? playButton.frame = CGRectMake(110, 120, 80, 80);

? ? ? ? ? ? ? ? [playButton setBackgroundImage:[UIImage imageNamed:@"play.png"] forState:UIControlStateNormal];

? ? ? ? ? ? ? ? playButton.alpha = 0.5;

? ? ? ? ? ? ? ? [firstPage addSubview:playButton];

? ? ? ? }

?? ? ? ?

? ? ? ? NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];

? ? ? ? if (soundPath!=nil) {

? ? ? ? ? ? ? ? float volumn = [bookSetting getUpdatedVolumn];

? ? ? ? ? ? ? ? player = [[SoundPlayer alloc]initWithPath:soundPath bookName:bookName volumn:volumn];

? ? ? ? }

? ? ? ? [pool release];

}

-(void)setPlayInBackground{

? ? ? ? if (soundPath!=nil) {

? ? ? ? ? ? ? ? //后臺播放

? ? ? ? ? ? ? ? [self registerForBackgroundNotifications];

? ? ? ? ? ? ? ? OSStatus result = AudioSessionInitialize(NULL, NULL, NULL, NULL);

? ? ? ? ? ? ? ? if (result)

? ? ? ? ? ? ? ? ? ? ? ? NSLog(@"Error initializing audio session! %d", result);

?? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ? [[AVAudioSession sharedInstance] setDelegate: self];

? ? ? ? ? ? ? ? NSError *setCategoryError = nil;

? ? ? ? ? ? ? ? [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: &setCategoryError];

? ? ? ? ? ? ? ? if (setCategoryError)

? ? ? ? ? ? ? ? ? ? ? ? NSLog(@"Error setting category! %d", setCategoryError);

?? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ? result = AudioSessionAddPropertyListener (kAudioSessionProperty_AudioRouteChange, RouteChangeListener, self);

? ? ? ? ? ? ? ? if (result)?

? ? ? ? ? ? ? ? ? ? ? ? NSLog(@"Could not add property listener! %d", result);

? ? ? ? }

}

#pragma mark AudioSession handlers


void RouteChangeListener(void * inClientData,

?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? AudioSessionPropertyID? ? ? ? inID,

?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? UInt32 inDataSize,

?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? const void * inData){

? ? ? ? BookContentView2* This = (BookContentView2*)inClientData;

?? ? ? ?

? ? ? ? if (inID == kAudioSessionProperty_AudioRouteChange) {

?? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ? CFDictionaryRef routeDict = (CFDictionaryRef)inData;

? ? ? ? ? ? ? ? NSNumber* reasonValue = (NSNumber*)CFDictionaryGetValue(routeDict, CFSTR(kAudioSession_AudioRouteChangeKey_Reason));

?? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ? int reason = [reasonValue intValue];

?? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ? if (reason == kAudioSessionRouteChangeReason_OldDeviceUnavailable) {

? ? ? ? ? ? ? ? ? ? ? ? [This.player stop];

? ? ? ? ? ? ? ? }

? ? ? ? }

}


- (void)audioPlayerBeginInterruption:(AVAudioPlayer *)p

{

? ? ? ? NSLog(@"Interruption begin. Updating UI for new state");

? ? ? ? // the object has already been paused,? ? ? ? we just need to update UI

? ? ? ? if (inBackground)

? ? ? ? {

? ? ? ? ? ? ? ? [self updateViewForPlayerStateInBackground:p];

? ? ? ? }

? ? ? ? else

? ? ? ? {

? ? ? ? ? ? ? ? [self updateViewForPlayerState:p];

? ? ? ? }

}


- (void)audioPlayerEndInterruption:(AVAudioPlayer *)p

{

? ? ? ? NSLog(@"Interruption ended. Resuming playback");

? ? ? ? [self startPlaybackForPlayer:p];

}


#pragma mark background notifications

- (void)registerForBackgroundNotifications

{

? ? ? ? [[NSNotificationCenter defaultCenter] addObserver:self

?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? selector:@selector(setInBackgroundFlag)

?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? name:UIApplicationWillResignActiveNotification

?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? object:nil];

?? ? ? ?

? ? ? ? [[NSNotificationCenter defaultCenter] addObserver:self

?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? selector:@selector(clearInBackgroundFlag)

?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? name:UIApplicationWillEnterForegroundNotification

?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? object:nil];

}


- (void)setInBackgroundFlag

{

? ? ? ? inBackground = YES;

}


- (void)clearInBackgroundFlag

{

? ? ? ? inBackground = NO;

}

轉載于:https://my.oschina.net/u/1759686/blog/261189

總結

以上是生活随笔為你收集整理的【必看】AVAudioPlayer播放声音时加入了后台播放功能,看懂了吗?的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。