
我的中断听众打电话
NSError *activationError = nil;[[AVAudioSession sharedInstance] setActive:YES error:&activationError];
但是一旦闹钟响起,应用程序的音频会话就会消失.使用适当的开始和结束状态调用侦听器.
它在iOS 6上运行得很好.
我听说这是iOS 7中的bug并且有一种解决方法,但找不到它.
有没有人知道Apple的变通方法或技术说明的链接?
编辑:我发现我必须使用AVAudioSessioncategoryPlayback而不是kAudioSessioncategory_AmbIEntSound.现在它有效.但它不是我想要的类别.
解决方法 你可以在下面看到我的代码.但首先,您应该选择此位目标|背景模式|来自功能的音频,Airplay和画中画.
// VIEwController.m// AVAudioPlayer#import "VIEwController.h"@import AVFoundation;@import MediaPlayer;@interface VIEwController ()@property (strong,nonatomic) AVAudioPlayer *audioPlayer;@property (weak,nonatomic) IBOutlet UiSlider *volumeSlIDer;@property (weak,nonatomic) IBOutlet UiSlider *rateSlIDer;@end@implementation VIEwController- (voID)vIEwDIDLoad { [super vIEwDIDLoad]; // Do any additional setup after loading the vIEw,typically from a nib. [self setupAudio];}- (voID) setupAudio { NSError *error; [[AVAudioSession sharedInstance] setActive:YES error:&error]; if (error != nil) { NSAssert(error == nil,@""); } [[AVAudioSession sharedInstance] setcategory:AVAudioSessioncategoryPlayback error:&error]; if (error != nil) { NSAssert(error == nil,@""); } NSURL *soundURL = [[NSBundle mainBundle] URLForResource:@"SoManyTimes" withExtension:@"mp3"]; self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundURL error:&error]; if (error != nil) { NSAssert(error == nil,@""); } //[self.audioPlayer setVolume:0.8]; self.audioPlayer.enableRate = YES; [self.audioPlayer preparetoPlay]; [self.audioPlayer setVolume:self.volumeSlIDer.value]; [[NSNotificationCenter defaultCenter] addobserver:self selector:@selector(audioInterrupt:) name:AVAudioSessionInterruptionNotification object:nil]; [[NSNotificationCenter defaultCenter] addobserver:self selector:@selector(audioRouteChanged:) name:AVAudioSessionRouteChangeNotification object:nil]; MPRemoteCommandCenter *commandCenter = [MPRemoteCommandCenter sharedCommandCenter]; [commandCenter.playCommand addTarget:self action:@selector(playbuttonpressed:)]; [commandCenter.stopCommand addTarget:self action:@selector(stopbuttonpressed:)]; [commandCenter.pauseCommand addTarget:self action:@selector(stopbuttonpressed:)];}- (voID) audioRouteChanged:(NSNotification*)notification { NSNumber *reason = (NSNumber*)[notification.userInfo valueForKey:AVAudioSessionRouteChangeReasonKey]; switch ([reason integerValue]) { case AVAudioSessionRouteChangeReasonoldDeviceUnavailable: case AVAudioSessionRouteChangeReasonNoSuitableRouteForcategory: [self stopbuttonpressed:nil]; break; default: break; }}- (voID) audioInterrupt:(NSNotification*)notification { NSNumber *interruptionType = (NSNumber*)[notification.userInfo valueForKey:AVAudioSessionInterruptionTypeKey]; switch ([interruptionType integerValue]) { case AVAudioSessionInterruptionTypeBegan: [self stopbuttonpressed:nil]; break; case AVAudioSessionInterruptionTypeEnded: { if ([(NSNumber*)[notification.userInfo valueForKey:AVAudioSessionInterruptionoptionKey] intValue] == AVAudioSessionInterruptionoptionShouldResume) { [self playbuttonpressed:nil]; } break; } default: break; }}- (IBAction)playbuttonpressed:(ID)sender { BOol played = [self.audioPlayer play]; if (!played) { NSLog(@"Error"); } MPMediaItemArtwork *albumart = [[MPMediaItemArtwork alloc] initWithImage:[UIImage imagenamed:@"CoverArt"]]; NSDictionary *songInfo = @{ MPMediaItemPropertyTitle:@"So Many Times",MPMediaItemPropertyArtist:@"The Jellybricks",MPMediaItemPropertyAlbumTitle:@"Soap Opera",MPMediaItemPropertyArtwork:albumart,MPMediaItemPropertyPlaybackDuration:@(self.audioPlayer.duration),MPNowPlayingInfoPropertyElapsedplaybackTime:@(self.audioPlayer.currentTime),}; [[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:songInfo];}- (IBAction)stopbuttonpressed:(ID)sender { [self.audioPlayer stop];}- (IBAction)volumeSlIDerChanged:(UiSlider*)sender { [self.audioPlayer setVolume:sender.value];}- (IBAction)rateSlIDerChanged:(UiSlider*)sender { [self.audioPlayer setRate:sender.value];}- (voID)dIDReceiveMemoryWarning { [super dIDReceiveMemoryWarning]; // dispose of any resources that can be recreated.}- (voID) dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self];}@end 总结 以上是内存溢出为你收集整理的iphone – 如何在iOS 7音频中断后恢复音频会话?全部内容,希望文章能够帮你解决iphone – 如何在iOS 7音频中断后恢复音频会话?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)