2016-02-03 31 views
0

我在我正在API 19 Android模擬器上測試的MonoGame項目中有一個聲音效果實例數組。聲音效果從「Assets/Content」文件夾中名爲「Sound」的文件夾中加載。他們都是「.wav」格式。我無法弄清楚如何使音效正常播放。音量在模擬器上設置爲完整,我已經使用XNA 4.0 for Windows Phone測試了代碼,沒有任何問題。我該如何解決這個問題?如何在Android的MonoGame上播放音效?

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

回答

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編譯器/加載器所做的任何更改的最新狀態(但不太可能它實際上可能會更改)。

+0

我試過了,但聲音效果依然不會播放。我將答案中的代碼複製到一個類中,並使用XNA 4.0 Windows Phone項目中的一些XNB文件進行測試,但模擬器仍然不會播放聲音。我會在Android手機上測試該應用,但現在我無法訪問該應用。 – Ben

+0

你確定你正在使用的模擬器實際上可以通過你的電腦播放聲音嗎?例如,您是否嘗試過在其瀏覽器中播放YouTube視頻? – Falgantil

+0

我剛查過。它絕對可以通過YouTube播放聲音。 – Ben