
Androidで音を鳴らすサンプル#trySound00

音を鳴らす。とにかく何でもいいから音が出る様に。
MediaPlayer使うと再生に時間がかかる
SoundPoolをは再生に時間がかからない。
res内にrawフォルダを用意してその中にサウンドデータをつっこむ。
コンパイルを通したくないデータをここに入れるらしい。
Layoutのプロパティの「on click」にクリックされたときに呼び出される関数を入れると、わざわざfindViewByIdしてリスナーを登録しなくても呼び出しがかかるらしい。
●検索した事
android oggファイル
Android 音を鳴らす
Android 音 リソース
Android 音 リソース 位置
Android Layout onClick プロパティ
●開発環境
Eclipse IDE バージョン: 3.7 Indigo Service Release 2
ターゲットプラットフォーム: 2.1
API レベル: 7
package trial.sample.trysound00; import android.app.Activity; import android.media.AudioManager; import android.media.MediaPlayer; import android.media.SoundPool; import android.os.Bundle; import android.view.View; public class TrySound00Activity extends Activity { // SoundPool private SoundPool mSoundPool; private int mSoundID; // MediaPlayer private MediaPlayer mMediaPlayer; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // サウンドプールに音をロードする // ロードが間に合わない場合は音が再生されない this.mSoundPool = new SoundPool( 1, AudioManager.STREAM_MUSIC, 0); this.mSoundID = this.mSoundPool.load(this, R.raw.pan, 1); // MediaPlayerに音をロードする this.mMediaPlayer = MediaPlayer.create(this, R.raw.pan); } // //////////////////////////////////////////////////////////// // SoundPoolで再生ボタン(layoutの「On click」にこの関数の名前を書く) public void onClickButtonPlaySoundPool(View view) { this.mSoundPool.play( this.mSoundID, 1.0f, 1.0f, 0, 0, 1.0f); } // //////////////////////////////////////////////////////////// // MediaPlayerで再生 public void onClickButtonPlayMediaPlayer(View view) { // 再生中の場合は再生位置を最初に戻す if (this.mMediaPlayer.isPlaying()) { this.mMediaPlayer.seekTo(0); // 止まってるときは普通に再生 } else { this.mMediaPlayer.start(); } } }
タッチパネルを触った強さを取るサンプル#tryTouch00 AndroidのOpenGLで3D空間上のオブジェクトを指でドラッグするサンプル#try...
お願いします