好的,所以我有一个应用程序,其中有许多练习视频,根本没有任何声音。
此外,在练习开始时,我会显示设备音乐播放器中的所有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];
答案 0 :(得分: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);
从这里得到答案。
App with AVPlayer plays mp4 interrupt iPod music after launched
答案 1 :(得分: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];
}
它只是一个演示。希望它有助于进一步......
答案 2 :(得分:0)
不幸的是,AudioSessionInitialize和朋友不推荐使用iOS7。现在使用AVAudioSession更容易混合。我使用audioSession对象作为全局 - 不确定如果它被解除分配会发生什么。如果它在堆栈中声明,AVPlayer肯定会匆忙放弃 - 所以要小心那个问题。
_audioSession = [AVAudioSession sharedInstance];
[_audioSession setCategory:AVAudioSessionCategoryAmbient error:&theError];