[英]Play audio and video at the same time in IOS
嗨,我正在嘗試同時播放兩個不同的文件。
我試圖使用AVFoundation
和MediaPlayer
創建幾個玩家,但它不能很好地工作。 什么是同時播放音頻文件和視頻的最佳方式?
我采用單獨文件的原因是為了節省空間,因為應用程序將被本地化,每種語言只有26個音頻文件,而不是26個視頻節省空間。 這很重要,因為蘋果不允許下載超過100 MB的應用程序大小。
我建議將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到同一個音頻播放器,你應該很高興。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.