在IOS中同时播放音频和视频 [英] Play audio and video at the same time in IOS

查看:2235
本文介绍了在IOS中同时播放音频和视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试同时播放两个不同的文件.

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

我尝试使用AVFoundationMediaPlayer创建多个播放器,但是效果不佳.同时播放音频文件和视频的最佳方法是什么?

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?

我要分离文件的原因是为了节省空间,因为该应用程序将本地化,每种语言只有26个音频文件,而不是26个视频,可以节省空间.这很重要,因为Apple不允许下载大小超过100 MB的应用.

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.

推荐答案

我建议结合使用AVAudioPlayerMPMoviePlayerController

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

For the AVAudioPlayer, you set it to an ambient session, this will allow for another media source to play simultaneously. I have posted some code below. This is pulled from a side project I did that can be found on my 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加载到同一音频播放器,就可以了.

If you want this to be changeable, you just load in a different audio URL to the same audio player and you should be good to go.

这篇关于在IOS中同时播放音频和视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆