0

I have an array of sound effect instances in a MonoGame project that I'm testing on an API 19 Android emulator. The sound effects are loaded from a folder called "Sound" in the "Assets/Content" folder. They are all in the ".wav" format. I can't figure out how to make the sound effect play properly. The volume is set to full on the emulator and I've tested the code using XNA 4.0 for Windows Phone with no issues. How can I fix this problem?

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

Note: Even if I use the "SoundEffect" type instead, no sound is produced.

SoundEffect[] soundEffects = new SoundEffect[5];
soundEffects[0] = Content.Load<SoundEffect>("Sound/1");
soundEffects[0].Play();
Ben
  • 1,299
  • 3
  • 17
  • 37

1 Answers1

0

Are you loading them as WAV, or are you compiling them to xnb files first? I am using the MonoGame content compiler to compile my wav files to xnb, and I can get them to play just fine on Android.

Here's how I play my audio files:

// 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;
}

Keep in mind, this audio file is being compiled to xnb before being deployed to the device. Logically I don't see why MonoGame for Android shouldn't be able to run wav files, but even then, compile them to xnb. That way you'll always stay up-to-date with any changes being made to the xnb compiler/loader (however unlikely it may be that it actually changes).

Falgantil
  • 1,290
  • 12
  • 27
  • I tried this, but the sound effects still won't play. I copied the code in your answer into a class and used some XNB files from an XNA 4.0 Windows Phone project for testing, but the emulator still won't play the sounds. I'd test the app on a physical Android phone, but I don't have access to one right now. – Ben Feb 04 '16 at 21:23
  • Are you certain the emulator you're using can actually play sounds through your PC? For example, have you tried playing a youtube video in its browser? – Falgantil Feb 05 '16 at 09:47
  • I just checked. It can definitely play sound through YouTube. – Ben Feb 05 '16 at 11:40
  • Update: I was using the XNB files compiled for the Windows Phone project. They weren't compiled specifically for iOS. How can I compile the ".wav" files to iOS XNB? – Ben Feb 05 '16 at 23:25
  • Never mind. I installed MonoGame Pipeline, which allowed me to convert the ".wav" files to XNB for iOS. Blimey, that took some doing. – Ben Feb 06 '16 at 01:17