防止MPMoviePlayerController中断设备的主audio

我已经使用MPMoviePlayerController向我的应用程序添加了一个介绍性video。 video按预期播放。 但是,video不会 – 也不应该 – 有声音,但是,如果用户正在他们的设备上听音乐(例如),然后打开我的应用程序,音乐停止播放,因为我的video开始播放。 由于我的video没有声音,所以我希望播放video不要中断设备上的主audio。

我已经通过MPMoviePlayerController类看,并没有看到任何线索。 有没有办法做到这一点? 这是我的代码:

- (void)setUpVideoPlayer { NSString *videoFilePath = [[NSBundle mainBundle] pathForResource:@"introVideo" ofType:@"MP4"]; NSURL *videoUrl = [NSURL fileURLWithPath:videoFilePath]; MPMoviePlayerController *player = [[MPMoviePlayerController alloc]initWithContentURL:videoUrl]; player.backgroundView.hidden = YES; player.movieSourceType = MPMovieSourceTypeFile; [player prepareToPlay]; [player setShouldAutoplay:YES]; [player setRepeatMode:MPMovieRepeatModeOne]; [player setFullscreen:YES]; [player setControlStyle:MPMovieControlStyleNone]; [player setScalingMode:MPMovieScalingModeAspectFill]; [player play]; player.view.translatesAutoresizingMaskIntoConstraints = NO; NSLayoutConstraint *playerWidth = [NSLayoutConstraint constraintWithItem:player.view attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeWidth multiplier:1.0f constant:0.0f]; NSLayoutConstraint *playerHeight = [NSLayoutConstraint constraintWithItem:player.view attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeHeight multiplier:1.0f constant:0.0f]; NSLayoutConstraint *playerTop = [NSLayoutConstraint constraintWithItem:player.view attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0f constant:0.0f]; NSLayoutConstraint *playerLeft = [NSLayoutConstraint constraintWithItem:player.view attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1.0f constant:0.0f]; [self.view addSubview:player.view]; [self.view addConstraints:@[playerWidth, playerHeight, playerTop, playerLeft]]; _player = player; } 

回答我自己的问题:

 #import <AVFoundation/AVAudioSession.h> #import <AudioToolbox/AudioSession.h> - (void)viewDidLoad { [super viewDidLoad]; [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryAmbient error: nil]; //do other stuff }