2011-11-18 141 views
1

我無法在我的Mac的Android模擬器中聽到任何聲音。有時候,我真的很少聽到它。我給你我的聲音管理器代碼。但我認爲它的配置問題聽不到聲音Android模擬器Mac

我從他們說它的工作頁面複製代碼。而且我已經嘗試過5種不同的代碼並且沒有任何作用。我的聲音是WAV遊戲:100K 800K菜單

感謝

import java.util.HashMap; 

import android.content.Context; 
import android.media.AudioManager; 
import android.media.SoundPool; 


public class SoundManager { 

    static private SoundManager _instance; 
    private static SoundPool mSoundPool; 
    private static HashMap<Integer, Integer> mSoundPoolMap; 
    private static AudioManager mAudioManager; 
    private static Context mContext; 

    private SoundManager() 
    { 
    } 

    /** 
    * Requests the instance of the Sound Manager and creates it 
    * if it does not exist. 
    * 
    * @return Returns the single instance of the SoundManager 
    */ 
    static synchronized public SoundManager getInstance() 
    { 
     if (_instance == null) 
      _instance = new SoundManager(); 
     return _instance; 
    } 

    /** 
    * Initialises the storage for the sounds 
    * 
    * @param theContext The Application context 
    */ 
    public static void initSounds(Context theContext) 
    { 
     mContext = theContext; 
     mSoundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 0); 
     mSoundPoolMap = new HashMap<Integer, Integer>(); 
     mAudioManager = (AudioManager)mContext.getSystemService(Context.AUDIO_SERVICE);   
    } 

    /** 
    * Add a new Sound to the SoundPool 
    * 
    * @param Index - The Sound Index for Retrieval 
    * @param SoundID - The Android ID for the Sound asset. 
    */ 
    public static void addSound(int Index,int SoundID) 
    { 
     mSoundPoolMap.put(Index, mSoundPool.load(mContext, SoundID, 1)); 
    } 

    /** 
    * Loads the various sound assets 
    * Currently hard coded but could easily be changed to be flexible. 
    */ 
    public static void loadSounds() 
    { 
     mSoundPoolMap.put(1, mSoundPool.load(mContext, R.raw.menu, 1)); 
     mSoundPoolMap.put(2, mSoundPool.load(mContext, R.raw.game, 1));  
    } 

    /** 
    * Plays a Sound 
    * 
    * @param index - The Index of the Sound to be played 
    * @param speed - The Speed to play not, not currently used but included for compatibility 
    */ 
    public static void playSound(int index,float speed) 
    {  
      float streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC); 
      streamVolume = streamVolume/mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC); 
      mSoundPool.play(mSoundPoolMap.get(index), streamVolume, streamVolume, 1, 0, speed); 
    } 

    /** 
    * Stop a Sound 
    * @param index - index of the sound to be stopped 
    */ 
    public static void stopSound(int index) 
    { 
     mSoundPool.stop(mSoundPoolMap.get(index)); 
    } 

    public static void cleanup() 
    { 
     mSoundPool.release(); 
     mSoundPool = null; 
     mSoundPoolMap.clear(); 
     mAudioManager.unloadSoundEffects(); 
     _instance = null; 

    } 


} 

回答

0

我解決了它,

只需重新安裝所有的Eclipse和SDK。我嘗試在另一臺電腦上工作正常。相同的代碼

1

基本上,你必須在配置設置中設置一個標誌來告訴它輸出什麼樣的音頻以及什麼設備。這是一個痛苦。我在這裏寫一下吧:

http://www.banane.com/2012/01/11/joy-of-android-playing-sound/

這在我的舊的MacBook工作。剛剛升級到Mac Air並且解決方案已更改,但不確定,但現在 - 音頻是解決方案。當我得到它的工作時,我會在這裏發佈。

+0

嗨@Anna,我在Macbook Pro 2009和iMac 2011年底試用它沒有。但就像我說我只是重新安裝和工作 – Edig

+0

@Anna我添加了-useaudio開關,並嘗試包括'android.media.MediaPlayer'導入,但它一旦輸入';'就從IDE中消失結束該行。不知道這是否意味着它已經加載。無論哪種方式,它都不會改變模擬器無法播放聲音。任何時候我做一些需要聲音的東西,logcat都會吐出關於找不到揚聲器的消息。有任何想法嗎? – dbconfession

相關問題