0

我在 API 19 Android 模拟器上测试的 MonoGame 项目中有一系列音效实例。音效从“Assets/Content”文件夹中名为“Sound”的文件夹加载。它们都是“.wav”格式。我不知道如何使音效正常播放。模拟器上的音量设置为满,我已经使用 XNA 4.0 for Windows Phone 测试了代码,没有任何问题。我该如何解决这个问题?

SoundEffectInstance[] soundEffects = new SoundEffectInstance[5];
soundEffects[0] = Content.Load<SoundEffect>("Sound/1").CreateInstance();
soundEffects[0].Play(); //This should play the sound effect, but no sound comes from the emulator

注意:即使我改用“SoundEffect”类型,也不会产生声音。

SoundEffect[] soundEffects = new SoundEffect[5];
soundEffects[0] = Content.Load<SoundEffect>("Sound/1");
soundEffects[0].Play();
4

1 回答 1

0

您是将它们加载为 WAV,还是先将它们编译为 xnb 文件?我正在使用 MonoGame 内容编译器将我的 wav 文件编译为 xnb,我可以让它们在 Android 上正常播放。

以下是我播放音频文件的方式:

// Loading the sound effect
this.SoundEffect = this.ContentManager.Load<SoundEffect>(this.assetName);

// Playing the audio
public virtual void Play(bool loop = false)
{
    if (this.SoundEffect == null)
    {
        throw new ArgumentNullException(
            "SoundEffect",
            "You cannot play a sound effect that has nothing loaded!");
    }

    this.SoundEffectInstance = this.SoundEffect.CreateInstance();
    this.SoundEffectInstance.IsLooped = loop;
    this.SoundEffectInstance.Pitch = this.Pitch;
    this.SoundEffectInstance.Volume = this.Volume;
    this.SoundEffectInstance.Play();
    this.IsPlaying = true;
}

请记住,此音频文件在部署到设备之前会被编译为 xnb。从逻辑上讲,我不明白为什么 MonoGame for Android 不应该能够运行 wav 文件,但即便如此,将它们编译为 xnb。这样,您将始终了解对 xnb 编译器/加载器所做的任何更改(但它实际上不太可能发生更改)。

于 2016-02-04T08:52:13.637 回答