开发者

iPhone: Play audio and video simultaneously

开发者 https://www.devze.com 2023-03-21 00:39 出处:网络
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

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 = 开发者_StackOverflow中文版newPlayer;
[newPlayer release];

[appSoundPlayer prepareToPlay];

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

Thanks.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消