好的,所以我有一个应用程序,其中有很多练习视频,根本没有任何声音。
此外,在练习开始时,我会显示设备音乐播放器中的所有 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];