3

好吧,所以我有一个应用程序,其中有许多练习视频,根本没有任何声音。随音乐应用程序播放无声视频播放歌曲

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

得到的答案终于....

工程与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

+1

对于Swift 3,你可以使用[这个答案](http://stackoverflow.com/a/42969322/67667) – dchakarov

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]; 
} 

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

+0

感谢您的代码Maulik!但我需要的是播放iPod音乐。设备上的mp3,我可能无法使用AudioServicesPlaySystemSound功能播放。有什么办法吗? – mayuur

+0

此外,你可以得到iPod音乐文件的路径和播放它.. – Maulik

+0

其实我需要让用户选择播放列表或一些歌曲,而用户开始练习。该音乐将来自设备的音乐应用程序。我只会处理当前的曲目信息和Next/Previous Actions。这一切都很好,直到视频开始。 – mayuur

0

不幸的是,AudioSessionInitialize和朋友iOS7被弃用。现在更容易与AVAudioSession混合。我使用audioSession对象作为全局 - 不知道如果它被释放会发生什么。如果AVPlayer在堆栈中声明,它肯定会放弃 - 所以要小心这个问题。

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