启动后使用AVPlayer播放mp4的应用会打断iPod音乐播放

4

我的应用使用AVPlayer播放mp4,在启动完成后,它会打断iPod的音乐,尽管我已经设置了允许与其他应用混合的音频会话。

 - (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

以下是我的测试程序:
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;

我假设那些 AudioSessionSetProperty 方法返回的是 YES? - Adam B
感谢您的回复,@AdamB。我检查了所有方法的OSStatus,它们都是kAudioSessionNoError。 - nova
谢谢@AdamB,在测试后我找到了错处。 - nova
2个回答

9

iOS 6/7支持使用 AVAudioSession,因为 AudioSessionSetProperty 已经被废弃。

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

完美,仍然在iOS 14中工作,谢谢。 - Supertecnoboff

5

最终,我找出了问题所在。

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);

在AudioSessionSetProperty之后必须调用AudioSessionSetActive,现在已经可以正常工作。


很好的发现。感谢您为其他人添加答案。 - Adam B

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接