防止MPMoviePlayerController中断设备的主音频 [英] Prevent MPMoviePlayerController from Interrupting Device's Main Audio

查看:129
本文介绍了防止MPMoviePlayerController中断设备的主音频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用MPMoviePlayerController为我的应用程序添加了介绍性视频。视频按预期播放。然而,视频没有 - 也不应该 - 有声音但是,如果用户正在他们的设备上听音乐(例如)然后打开我的应用程序,音乐会在我的视频开始播放时停止播放。由于我的视频没有声音,我希望我的视频播放不会中断设备上的主音频。

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.

我查看了MPMoviePlayerController类,并且没有看到任何线索。有没有办法做到这一点?这是我的代码:

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;

}


推荐答案

回答我自己的问题:

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

- (void)viewDidLoad
{
  [super viewDidLoad];

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

  //do other stuff
}

这篇关于防止MPMoviePlayerController中断设备的主音频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆