【问题标题】:How do I play a sound effect on MonoGame for Android?如何在 MonoGame for Android 上播放音效?
【发布时间】:2016-02-04 16:52:13
【问题描述】:

我在 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();

【问题讨论】:

    标签: android audio xna soundeffect soundeffectinstance


    【解决方案1】:

    您是将它们加载为 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 编译器/加载器所做的任何更改(但它实际上不太可能发生更改)。

    【讨论】:

    • 我试过了,但声音仍然无法播放。我将您答案中的代码复制到一个类中,并使用 XNA 4.0 Windows Phone 项目中的一些 XNB 文件进行测试,但模拟器仍然不会播放声音。我会在实体 Android 手机上测试该应用,但我现在无法访问。
    • 您确定您使用的模拟器可以通过您的 PC 播放声音吗?例如,您是否尝试过在其浏览器中播放 youtube 视频?
    • 我刚刚查过了。它绝对可以通过 YouTube 播放声音。
    • 更新:我使用的是为 Windows Phone 项目编译的 XNB 文件。它们不是专门为 iOS 编译的。如何将“.wav”文件编译为 iOS XNB?
    • 没关系。我安装了 MonoGame Pipeline,它允许我将“.wav”文件转换为适用于 iOS 的 XNB。天哪,这需要一些努力。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-07
    • 2011-10-19
    • 1970-01-01
    • 1970-01-01
    • 2014-12-27
    • 1970-01-01
    相关资源
    最近更新 更多