Created
August 27, 2012 04:46
-
-
Save kojiokb/3485641 to your computer and use it in GitHub Desktop.
SoundPoolの使い方
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.app.Activity; | |
import android.content.Context; | |
import android.media.AudioManager; | |
import android.media.SoundPool; | |
import android.os.Bundle; | |
import android.view.View; | |
public class MainActivity extends Activity implements View.OnClickListener { | |
private SoundPool mSoundPool; | |
private int mSoundId; | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
findViewById(R.id.button1).setOnClickListener(this); | |
} | |
@Override | |
protected void onResume() { | |
super.onResume(); | |
int maxStreams = 1; // プールする数 | |
int streamType = AudioManager.STREAM_MUSIC; // Streamの種類 | |
int srcQuality = 0; // サンプリングレートのクオリティ(デフォルトは0) | |
mSoundPool = new SoundPool(maxStreams, streamType, srcQuality); | |
Context context = getApplicationContext(); | |
int resId = R.raw.sound; // リソースID | |
int priority = 1; // 現在は利用されていないようですが、ドキュメントには将来の互換性のため1を指定するように記述されています。 | |
mSoundId = mSoundPool.load(context, resId, priority); | |
} | |
@Override | |
protected void onPause() { | |
super.onPause(); | |
mSoundPool.release(); | |
} | |
@Override | |
public void onClick(View v) { | |
float leftVolume = 1.0f; // 左のスピーカーからの再生音量。(0.0〜1.0) | |
float rightVolume = 1.0f; // 右のスピーカーからの再生音量。(0.0〜1.0) | |
int priority = 0; // プライオリティ(0が一番優先度が高い) | |
int loop = 0; // ループ回数(-1の場合は無限にループ、0の場合はループしない) | |
float rate = 1.0f; // 再生速度(0.5〜2.0:0.5倍から2倍の速度まで設定できる) | |
mSoundPool.play(mSoundId, leftVolume, rightVolume, priority, loop, rate); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment