4

好的,所以我有一个应用程序,其中有很多练习视频,根本没有任何声音。

此外,在练习开始时,我会显示设备音乐播放器中的所有 mp3 文件,供用户选择音频文件。

但是,每当视频开始播放时,音乐播放器就会暂停。如何使其以音乐播放器继续播放和视频同时播放的方式工作。

对于视频,使用类 -- MPMoviePlayerViewController

添加如下:

self.movieplayerController = [[MPMoviePlayerViewController alloc] initWithContentURL:urlStr];
self.movieplayerController.moviePlayer.controlStyle =  MPMovieControlStyleFullscreen;
self.movieplayerController.moviePlayer.fullscreen = YES;
self.movieplayerController.moviePlayer.view.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(movieFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
[self presentModalViewController:movieplayerController animated:YES];

对于音乐播放器,类是 --- MPMusicPlayerController。

音频被选择和播放如下:

 - (void)mediaPicker: (MPMediaPickerController *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection
{
      [self dismissModalViewControllerAnimated: YES];

      [self.musicPlayer stop];

      [self.musicPlayer setQueueWithItemCollection:mediaItemCollection];

      [self.musicPlayer play];
}

编辑

另外,尝试使用 AVPlayer 播放视频,但没有成功!

代码如下:

AVPlayerItem *playerItem = [[AVPlayerItem alloc] initWithURL:urlStr];
AVPlayer *player = [[AVPlayer alloc] initWithPlayerItem:playerItem];

AVPlayerLayer *avPlayerLayer = [AVPlayerLayer playerLayerWithPlayer:player];

avPlayerLayer.frame = self.view.frame;
[self.view.layer addSublayer:avPlayerLayer];
[player play];
4

3 回答 3

5

终于有答案了......

与 AVPlayer 一起使用。

如上所述在问题中初始化它。

AVPlayerItem *playerItem = [[AVPlayerItem alloc] initWithURL:urlStr];
AVPlayer *player = [[AVPlayer alloc] initWithPlayerItem:playerItem];

AVPlayerLayer *avPlayerLayer = [AVPlayerLayer playerLayerWithPlayer:player];

avPlayerLayer.frame = self.view.frame;
[self.view.layer addSublayer:avPlayerLayer];
[player play];

但在此之前,在 AppDelegate 中,创建一个允许混音的 AudioSession。

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

从这里得到了答案。

带有 AVPlayer 的应用程序在启动后播放 mp4 中断 iPod 音乐

于 2012-09-17T12:22:07.900 回答
1

您可以AudioToolbox用于此目的。我开发了一个小型演示(在阅读了您的问题之后)及其工作。

从这里下载演示MPMoviePlayerViewController:- http://mobile.tutsplus.com/tutorials/iphone/mediaplayer-framework_mpmovieplayercontroller_ios4/

现在将任何.wav文件添加到您的包中。

现在修改这个方法:

-(IBAction)playMovie:(id)sender
{
    UIButton *playButton = (UIButton *) sender; 

    NSString *filepath   =   [[NSBundle mainBundle] pathForResource:@"big-buck-bunny-clip" ofType:@"m4v"];
    NSURL    *fileURL    =   [NSURL fileURLWithPath:filepath];
    MPMoviePlayerViewController *moviePlayerController = [[MPMoviePlayerViewController alloc] initWithContentURL:fileURL];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(moviePlaybackComplete:)
                                                 name:MPMoviePlayerPlaybackDidFinishNotification
                                               object:moviePlayerController];

    [moviePlayerController.view setFrame:CGRectMake(playButton.frame.origin.x, 
                                                    playButton.frame.origin.y, 
                                                    playButton.frame.size.width, 
                                                    playButton.frame.size.height)];

    [self.view addSubview:moviePlayerController.view];
    //moviePlayerController.fullscreen = YES;

    //moviePlayerController.scalingMode = MPMovieScalingModeFill;

    [moviePlayerController.moviePlayer play];

    NSString *soundPath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"wav"];

    SystemSoundID soundID;

    AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath: soundPath], &soundID);

    AudioServicesPlaySystemSound (soundID);

    [soundPath release];
}

它只是一个演示。希望它进一步帮助...

于 2012-09-17T09:34:00.067 回答
0

不幸的是,iOS7 弃用了 AudioSessionInitialize 和朋友。它现在更容易与 AVAudioSession 混合。我将 audioSession 对象用作全局对象 - 不确定如果它被释放会发生什么。如果在堆栈上声明 AVPlayer 肯定会匆忙放弃 - 所以要小心那个陷阱。

  _audioSession = [AVAudioSession sharedInstance];
  [_audioSession setCategory:AVAudioSessionCategoryAmbient error:&theError];
于 2016-06-29T16:33:41.307 回答