iPhone:同时播放音频和视频

发布于 11-24 07:11 字数 889 浏览 1 评论 0原文

我正在构建一个游戏应用程序,我需要在其中播放动画视频及其相应的声音(根据情况可能会有所不同)。我已准备好所有视频和声音文件,但无法同时播放它们。我使用 MPMoviePlayer 播放视频,使用 AVFoundation 框架播放音频。有没有办法同时运行这两个?

对于视频:

        MPMoviePlayerViewController *videoController = [[[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:[cellDetails objectForKey:@"AVFile"]]] autorelease];
        [self presentMoviePlayerViewControllerAnimated:videoController];

对于声音:

 // Instantiates the AVAudioPlayer object, initializing it with the sound
AVAudioPlayer *newPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL: soundFileURL error: nil];
self.appSoundPlayer = newPlayer;
[newPlayer release];

[appSoundPlayer prepareToPlay];

appSoundPlayer.numberOfLoops = -1;
[appSoundPlayer setVolume: self.soundLevel];
[appSoundPlayer setDelegate: self];
[appSoundPlayer play];

谢谢。

I am building a game application where I need to play animation videos and their corresponding sounds(may be different depending on situation). I have all videos and sounds file are ready but I am not able to play them simultaneously. I am using MPMoviePlayer for video and AVFoundation framework to play audio. Is there anyway to run these both simultaneously?

For Video:

        MPMoviePlayerViewController *videoController = [[[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:[cellDetails objectForKey:@"AVFile"]]] autorelease];
        [self presentMoviePlayerViewControllerAnimated:videoController];

For Sound:

 // Instantiates the AVAudioPlayer object, initializing it with the sound
AVAudioPlayer *newPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL: soundFileURL error: nil];
self.appSoundPlayer = newPlayer;
[newPlayer release];

[appSoundPlayer prepareToPlay];

appSoundPlayer.numberOfLoops = -1;
[appSoundPlayer setVolume: self.soundLevel];
[appSoundPlayer setDelegate: self];
[appSoundPlayer play];

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

罪#恶を代价2024-12-01 07:11:11

我个人会走 AVPlayer 路线。我知道您可能想要使用 MPMoviePlayerViewController 和 AVAudioPlayer,但如果您只需在带有 AVPlayer 的 AVMutableComposition 上投入更多时间,您就可以完成您正在尝试的所有操作,甚至更多。这是非常值得的投资。

简而言之,您将设置一个带有轨道的 AVMutableComposition(也被视为“资产”)。这些轨道对应于音频和视频轨道。可以从其他文件的全部或部分中提取轨道,也可以根据需要插入空的音频/视频轨道部分。您可以将视频放在视频轨道上,并将任意数量的音频文件放在单独的音轨上。然后,您使用 AVMutableComposition 创建一个 AVPlayer 对象并调用“播放”消息。它非常精确地为您完成所有同步。

这篇文章实际上有一些很好的示例代码但我不确定他是否能正常工作。

I personally would go the AVPlayer route. I know you probably want to go with MPMoviePlayerViewController and AVAudioPlayer, but if you just invest a little bit more time into AVMutableComposition with an AVPlayer, you can do everything you're trying and more. It's well worth the investment.

In short, you'll setup an AVMutableComposition (which is also considered an "asset"), with tracks. These tracks correspond to audio and video tracks. The tracks can be pulled from all or portions of other files or you can also insert empty audio/video track portions as you wish. You'll put a video on a video tracks and any number of audio files on separate audio tracks. Then you create an AVPlayer object using the AVMutableComposition and call the "play" message. It does all of the syncing for you very precisely.

This post actually has some good sample code but I'm not sure if he's got it working correctly yet.

长伴2024-12-01 07:11:11

您可以尝试使用 AVMutableVideoComposition。尝试查看 这件事,如果您需要任何帮助,请告诉我。我很乐意提供帮助。

You can try using an AVMutableVideoComposition. Try taking a look at this thing and if you need any help with it let me know. I would gladly help.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文