【问题标题】:Prevent MPMoviePlayerController from Interrupting Device's Main Audio防止 MPMoviePlayerController 中断设备的主音频
【发布时间】:2014-02-27 15:43:41
【问题描述】:

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

我查看了 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;

}

【问题讨论】:

    标签: ios mpmovieplayercontroller


    【解决方案1】:

    回答了我自己的问题:

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

    【讨论】:

      猜你喜欢
      • 2013-09-19
      • 2012-11-17
      • 1970-01-01
      • 2022-12-18
      • 1970-01-01
      • 2011-05-31
      • 2021-06-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多