应用程序与AVPlayer播放mp4中断iPod音乐启动后

我的应用程序使用AVPlayer播放mp4,当我的应用程序完成启动时,它会中断iPod音乐,尽pipe我已经设置了audio会话以允许与其他人混音

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { AudioSessionInitialize(NULL, NULL, NULL, NULL); AudioSessionSetActive(true); UInt32 sessionCategory = kAudioSessionCategory_AmbientSound; AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(sessionCategory), &sessionCategory); UInt32 allowMixWithOthers = true; AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryMixWithOthers, sizeof(allowMixWithOthers), &allowMixWithOthers); 

视图控制器确实出现后,我重新启动iPod音乐,它可以正常工作与我的应用程序没有中断,我的应用程序不会中断音乐了。

有谁知道问题是否可以解决? 我也检查了myapp-info.plist ,发现没有属性来防止打断iPod。

所有AudioSession方法都不返回错误。

这里是iPhoneConfigureUtility中的日志:

 Aug 20 10:55:54 nova-teki-iPhone audiotest[3510] <Warning>: AudioSessionInitialize status = 0 Aug 20 10:55:55 nova-teki-iPhone kernel[0] <Debug>: ALS: kIOHIDDisplayBrightnessSliderPositionKey=69% (0xb226) Aug 20 10:55:55 nova-teki-iPhone audiotest[3510] <Warning>: AudioSessionSetActive status = 0 Aug 20 10:55:55 nova-teki-iPhone audiotest[3510] <Warning>: kAudioSessionProperty_AudioCategory status = 0 Aug 20 10:55:55 nova-teki-iPhone audiotest[3510] <Warning>: kAudioSessionProperty_OverrideCategoryMixWithOthers status = 0 Aug 20 10:55:56 nova-teki-iPhone audiotest[3510] <Error>: [10:55:56.005] FigSubtitleSampleCreateFromPropertyList signalled err=50 (kFigCFBadPropertyListErr) (NULL or bad plist) at /SourceCache/EmbeddedCoreMedia/EmbeddedCoreMedia-1033.6/BuildSystem/XcodeProjects/MediaToolbox/../../../Sources/../Prototypes/ClosedCaptions/FigCaptionCommand.c line 762 Aug 20 10:55:56 nova-teki-iPhone audiotest[3510] <Warning>: Application windows are expected to have a root view controller at the end of application launch 

以下是我的testing程序:

 OSStatus status = AudioSessionInitialize(NULL, NULL, NULL, NULL); status = AudioSessionSetActive(true); UInt32 sessionCategory = kAudioSessionCategory_AmbientSound; status = AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(sessionCategory), &sessionCategory); UInt32 allowMixWithOthers = true; status = AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryMixWithOthers, sizeof(allowMixWithOthers), &allowMixWithOthers); [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(itemDidPlayToEndTime:) name:AVPlayerItemDidPlayToEndTimeNotification object:nil]; self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; // Override point for customization after application launch. playerView = [[MoviePlayerView alloc] initWithFrame:self.window.bounds]; AVPlayer *player = [AVPlayer playerWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"sunny" ofType:@"mp4"]]]; player.actionAtItemEnd = AVPlayerActionAtItemEndNone; [player play]; [(AVPlayerLayer *)playerView.layer setPlayer:player]; [self.window addSubview:playerView]; [playerView release]; self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible]; return YES; 

在iOS 6/7中,您可以使用AVAudioSession,因为AudioSessionSetProperty已被弃用。

 [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionMixWithOthers error:nil]; 

最后,我弄清楚什么是错的。

 AudioSessionInitialize(NULL, NULL, NULL, NULL); UInt32 sessionCategory = kAudioSessionCategory_AmbientSound; AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(sessionCategory), &sessionCategory); UInt32 allowMixWithOthers = true; AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryMixWithOthers, sizeof(allowMixWithOthers), &allowMixWithOthers); AudioSessionSetActive(true); 

AudioSessionSetActive必须在AudioSessionSetProperty之后被调用,它现在工作正常。