4

Hi I'm trying to play two different files at the same time.

I tried to create several players using AVFoundation and MediaPlayer, but it doesn't work well. What's the best way to play audio file and video at the same time?

The reason I'm taking separate files is to save space, because the app will be localized, having only 26 audio files for each language instead of having 26 videos saves space. That's important because apple doesn't allow the download of app size above 100 MB.

4

1 回答 1

1

我建议AVAudioPlayer结合使用MPMoviePlayerController

对于AVAudioPlayer,您将其设置为环境会话,这将允许另一个媒体源同时播放。我在下面发布了一些代码。这是从我做的一个项目中提取的,可以在我的GitHub 上找到

AVAudioPlayer audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryAmbient error: nil];

NSURL *movieURL = [NSURL fileURLWithPath:
                   [[NSBundle mainBundle] pathForResource:@"movie"
                                                   ofType:@"mp4"]];

MPMoviePlayerController moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
[[moviePlayer view] setFrame:someFrame];
[self.view addSubview:moviePlayer.view];
[moviePlayer prepareToPlay];
[moviePlayer setScalingMode:MPMovieScalingModeAspectFill];
[moviePlayer setRepeatMode:MPMovieRepeatModeOne];
[moviePlayer setControlStyle:MPMovieControlStyleNone];
[moviePlayer play];

如果您希望它是可更改的,您只需将不同的音频 URL 加载到同一个音频播放器,就可以了。

于 2013-11-13T12:15:42.927 回答