Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
559 views
in Technique[技术] by (71.8m points)

ios - Prevent MPMoviePlayerController from Interrupting Device's Main Audio

I've added an introductory video to my application using MPMoviePlayerController. The video plays as expected. However, the video does not - and should not - have sound BUT, if the user is listening to music (for example) on their device and then opens my application, the music stops playing as my video starts playing. Since my video has no sound, I'd like the playing of my video to NOT interrupt the main audio on the device.

I've looked through MPMoviePlayerController class, and don't see any clues. Is there a way to do this? Here is my code:

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

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Answered my own question:

#import <AVFoundation/AVAudioSession.h>
#import <AudioToolbox/AudioSession.h>

- (void)viewDidLoad
{
  [super viewDidLoad];

  [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryAmbient error: nil];

  //do other stuff
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...