【问题标题】:Play silent videos alongwith the Music App playing songs播放无声视频以及音乐应用程序播放歌曲
【发布时间】:2012-09-17 07:49:57
【问题描述】:

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

另外,在练习开始时,我会显示设备音乐播放器中的所有 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];

【问题讨论】:

    标签: iphone ios mpmovieplayercontroller avplayer mpmusicplayercontroller


    【解决方案1】:

    终于找到答案了……

    适用于 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);    
    

    从这里得到答案。

    App with AVPlayer plays mp4 interrupt iPod music after launched

    【讨论】:

      【解决方案2】:

      您可以为此目的使用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];
      }
      

      这只是一个演示。希望对您有所帮助...

      【讨论】:

      • 感谢Maulik的代码!但我需要的是播放 iPod 音乐。设备上的 mp3,我可能无法使用 AudioServicesPlaySystemSound 功能播放。有什么办法吗?
      • 实际上我需要让用户在用户开始练习时选择一个播放列表或一些歌曲。该音乐将来自设备的音乐应用程序。我只会处理当前的曲目信息和下一个/上一个动作。这一切都很好,直到视频开始。
      • 另外,AudioServicesPlaySystemSound 最多只能播放 30 秒的声音!
      【解决方案3】:

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

        _audioSession = [AVAudioSession sharedInstance];
        [_audioSession setCategory:AVAudioSessionCategoryAmbient error:&theError];
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-06-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多