私は理解できない非常に奇妙な問題を抱えています。単一のサウンド(非常に短い「クリック」サウンド)で初期化されたサウンドプールがあります。アクティビティボタンのonClickハンドラーでSoundManager.playSound(1, 0);
、ハッシュマップでサウンドを検索してサウンドを再生する単純な操作を行います。クリックごとにまったく同じplaySound呼び出しが使用されます。それは常に音を再生し、誤りはありません。
ここに面白い部分があります。クリック音の音量は、ボタンを押すごとに変わります。一度押すとかすかに聞こえます。叩いてもう一度押すと大きな音がします。韻や理由が見つかりません。負荷が原因でめちゃくちゃになっているのではないかと思ったので(クレイジーだとは思いますが、ここでは途方に暮れています)、別のスレッドでサウンドを開始しました。それでも同じ結果。サウンドマネージャをもう一度初期化したためかもしれないと思ったので、それが起こらないようにチェックを追加しました....同じ結果です。
私はウェブ上でいくつかの場所を見つけたサウンドマネージャークラスを使用しました。以下です。
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;
public static boolean isInitialized = false;
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
*/
@SuppressLint("UseSparseArrays")
public static void initSounds(Context theContext)
{
mContext = theContext;
mSoundPool = new SoundPool(4, AudioManager.STREAM_NOTIFICATION, 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 hardcoded but could easily be changed to be flexible.
*/
public static void loadSounds()
{
if (!isInitialized)
{
mSoundPoolMap.put(1, mSoundPool.load(mContext, R.raw.click, 1));
isInitialized = true;
}
}
/**
* 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_NOTIFICATION);
streamVolume = streamVolume / mAudioManager.getStreamMaxVolume(AudioManager.STREAM_NOTIFICATION);
mSoundPool.play(mSoundPoolMap.get(index), 1, 1, 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));
}
/**
* Deallocates the resources and Instance of SoundManager
*/
public static void cleanup()
{
mSoundPool.release();
mSoundPool = null;
mSoundPoolMap.clear();
mAudioManager.unloadSoundEffects();
_instance = null;
}
}
PlaySoundメソッドで、左右のチャンネルの音量を最大値1にハードコーディングしたことがわかります。これは役に立ちませんでした。音の音量はまだ変動します。
また、ストリームに何らかの形で関連しているかどうかを確認するAudioManager.STREAM_NOTIFICATION
代わりに、使用しているストリームを更新しました。AudioManager.STREAM_MUSIC
同じ動作をするため、そうではないようです。
また、サウンドファイルをからに変換OGG
してWAV
、それが役立つかどうかを確認しました。変化なし。
私が使用しているのは非常に速い「クリック」ですが、よく聞いていると、途切れているようには聞こえません。そして、完全なファイルが再生されています。
これはハードウェアに関連している可能性がありますか?私が使用した唯一のテストデバイスは、古いHTCIncredibleです。そのハードウェアに関する既知の問題はありますか?
繰り返しますが...問題はありませんが、ボリュームが異なるのは面倒で、手放せません。他の誰かがこれを経験しましたか?