开发者

XNA variable audio speed

开发者 https://www.devze.com 2023-01-24 14:08 出处:网络
I\'m trying to sync an external audio file with a video that is played back at a variable framerate. When the video plays slowly, the audio should slow down, and when speeding up it should speed up.

I'm trying to sync an external audio file with a video that is played back at a variable framerate. When the video plays slowly, the audio should slow down, and when speeding up it should speed up.

I got the video playback sorted out, but the audio is still troubling me. How to sync the video to the audio this way?

I know exactly which video frame is playing at any moment, so I was hoping to use this information somehow to 开发者_JAVA百科change the audio speed. But, still puzzled about how to.

It does not matter if the sound is distorted in my case, it just needs to keep in sync with video at all times. The video playback speed is controlled in realtime by the user, and cannot be determined in advance.


NOTE: I'm assuming you're using Visual Studio 2008/2010, but the answer should apply if you're using XNA Game Studio as well.

You could use the FMOD library to play your audio, it'll do exactly what you need.

Initial Setup
You'd download the windows 32bit API. From there, after you've installed it, you'd go to "C:\Program Files(x86)\FMOD Sound System\FMOD Programmers API Win32\API", copy fmodex.dll and fmodexL.dll into your project directory, along with the contents of the Csharp folder there as well.

From your project, add the 2 DLL files, and the 4 class files into your project by right clicking on it in the solution explorer, and clicking "Add > Existing Item..."; once these are added to your project, select those files in the solution explorer, and make sure their "Copy to output Directory" property is set to "Copy Always". You're now ready to use the FMOD system.

Using FMOD
Inside your main class, add the following using statement using FMOD;, and add the following variables:

private FMOD.System sndSystem;
private FMOD.Channel sndChannel = new FMOD.Channel();
private FMOD.Sound sndSound = new FMOD.Sound();

Within your LoadContent()

FMOD.Factory.Create_System(ref sndSystem);
sndSystem.Init(1, INITFLAGS.NORMAL, (IntPtr)null);
sndSystem.CreateSound("**<Path To Your Audio>**", MODE.HARDWARE, ref sndSound);

Now whereever you want to start to play your sound, you just put in:

sndSystem.PlaySound(CHANNELINDEX.FREE, sndSound, false, ref sndChannel);

and to stop it:

sndChannel.stop();

For your purposes, while the audio was playing with the video, you'd determine how fast the video was going at that point, (1.0f for normal speed, 2.0f for double speed, .5f for half speed, etc) and pass that value to the sndSound.setMusicSpeed(<speedvalue>);.


The road to success for your request is potentially long and arduous. It seems that it's possible to do what you're asking, but because the PlayPosition property of the MediaPlayer class is readonly it's rather difficult. Here is a link that may help inspire you.

0

精彩评论

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

关注公众号