Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@mryp
Last active August 29, 2015 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mryp/c87036b6d0a9cebe28ed to your computer and use it in GitHub Desktop.
Save mryp/c87036b6d0a9cebe28ed to your computer and use it in GitHub Desktop.
効果音再生(SoundPool)
import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;
public class SoundPlayer {
//フィールド
//-------------------------------------------------
private SoundPool m_soundPool; //音再生クラス
private int m_soundId; //再生音ID
private boolean m_isReady = false; //再生準備完了かどうか
//メソッド
//-------------------------------------------------
/**
* コンストラクタ
* @param context 画面コンテキスト
* @param resId 再生リソースID
*/
public SoundPlayer(Context context, int resId) {
m_soundPool = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
m_soundId = m_soundPool.load(context, resId, 0);
m_soundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
@Override
public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
m_isReady = true;
}
});
}
/**
* 再生開始
* @return 再生成功時はtrue
*/
public boolean play() {
if (m_isReady)
{
if (m_soundPool.play(m_soundId, 1.0F, 1.0F, 0, 0, 1.0F) != 0)
{
return true;
}
}
return false; //再生失敗
}
/**
* リソース解放
*/
public void release() {
m_soundPool.release();
m_soundPool = null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment