我的Mac上的Android模拟器听不到任何声音。有时候,真的很少时间我能听到。我给你我的声音管理器的代码。但我认为它的配置问题
我从一个页面上复制了代码,他们说它可以工作。我已经尝试了大约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;
}
}发布于 2012-02-04 01:02:04
我解决了它,
只需重新安装所有的eclipse和SDK即可。我在另一台电脑上试了试,工作正常。相同的代码
发布于 2012-05-17 02:54:47
基本上,您必须在配置设置中设置一个标记,以告诉它要输出哪种类型的音频,以及输出哪种设备。这是一种痛苦。我在这里写道:
http://www.banane.com/2012/01/11/joy-of-android-playing-sound/
这在我的旧MacBook上起作用了。刚刚升级到Mac Air,解决方案已经改变,不确定,但-audio是现在的解决方案。当我让它工作时,我会在这里发帖。
https://stackoverflow.com/questions/8176403
复制相似问题