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

2 回答 2

9

在 iOS 6/7 中,您可以使用 AVAudioSession,因为不推荐使用 AudioSessionSetProperty。

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionMixWithOthers error:nil];
于 2014-01-26T10:23:49.423 回答
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);

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

于 2012-08-20T03:21:26.533 回答