App with AVPlayer plays mp4 interrupt iPod music after launched

删除回忆录丶 提交于 2020-01-11 06:13:49

问题


My App plays mp4 using AVPlayer, when my application finish launching, it interrupt the iPod music, although I have set the audio session to allow mix with others in

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

After view controller did appear, I restart the iPod music, it works fine with my app without interruption and my app won't interrupt the music anymore.

Does anyone know whether the problem can be solved or not? I also checked the myapp-info.plist, find no property to prevent interrupting iPod.

All the AudioSession Methods return no error.

here are the logs in 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

following is my test program:

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;

回答1:


In iOS 6/7 you can use AVAudioSession because AudioSessionSetProperty is deprecated.

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



回答2:


Finally, I figure out what's wrong.

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 must be called after AudioSessionSetProperty, it works fine now.



来源:https://stackoverflow.com/questions/12016770/app-with-avplayer-plays-mp4-interrupt-ipod-music-after-launched

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!