ios - 带有 AVPlayer 的应用程序在启动后播放 mp4 中断 iPod 音乐

标签 ios avplayer audiosession interruption

我的应用程序使用 AVPlayer 播放 mp4,当我的应用程序完成启动时,它会中断 iPod 音乐,尽管我已将 Audio Session 设置为允许与其他人混合

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

在 View Controller 确实出现后,我重新启动 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;

最佳答案

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

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

关于ios - 带有 AVPlayer 的应用程序在启动后播放 mp4 中断 iPod 音乐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12016770/

相关文章:

ios - 过滤/搜索 NSMutableDictionary

iphone - AVPlayer 缓冲、暂停通知和海报帧

ios - MP3 队列播放器 - 在后台线程中加载?

iphone - 我可以从 kAudioSessionProperty_AudioRoute 属性返回什么样的路由?

ios - MPMoviePlayerController 不响应设备音量控制

ios - setPreferredInput WithBlueTooth 不起作用

ios - 我们可以只向部分用户发布 iOS 应用程序的更新吗?

ios - 在 Parse 上存储数据以供 inApp 购买

ios - 如何在 Objective C 中检查 UIView 是否为零

xcode - 视频播放结束后如何返回到 tvOS 中的 Main.storyboard?