Android アプリ作成(タッチ処理編)その4

Android アプリ作成(タッチ処理編)その3
の続きになります
まず、
public class test2Activity extends Activity {
の下に
SoundPoll sp;
int sound;
を記述し、
赤いラインがでているので、
SoundPoll のところへカーソルを持っていって
import SoundPoll (Android widget)
をクリックし、インポートします
int sound は音声ファイルの読み込みに使うための変数です
次に、
sp = new SoundPool(1,AudioManager.STREAM_MUSIC, 0);
を記述します
例のごとく、自動的に候補がでますので
実際にはかなり楽に書き込めます
続いて
sound = sp.loa
まで書き込むと候補がでてきますので
load(Context context, int resid, int priolity); int – SOundPool
を選択すれば自動的に書き込まれます
context を this へ、
resid を R.raw.dummy-message
と指定しようとしたのですが
[2011-02-19 00:07:17 – Test2] res/raw/dummy-message.wav: Invalid file name: must contain only [a-z0-9_.]
となるため、ファイル名を変更します
Eclipse の raw > dummy-message.wav
を右クリックし
Refactor > Rename 
もしくは
Shift + Alt + R
を押してファイル名を変更します
ファイル名を dum.wav
にして
resid を R.raw.dum
pliority を 1
へ変更します
そして
Button button = (Button)findViewById(R.id.Button02);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sp.play(sound, 1.0f, 1.0f, 0, 0, 1.0f);
}
});
を追加します
sp.play(sound, 1.0f, 1.0f, 0, 0, 1.0f);
は、音声再生の設定です
play()
の中の数値ですが、左から
サウンドの番号
左スピーカーの音量 (0.0~1.0)
右スピーカーの音量 (0.0~1.0)
優先度 (0が最優先数値になる)
再生速度 (1が標準。2で倍速、0.5で半分)
となります
かなりわかりにくくなったので、以下が
main.xml の全文になります
-------------------------------------------------------
package jp.co.nikkeibp.test2;
import android.app.Activity;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class test2Activity extends Activity {
SoundPool sp;
int sound;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
sp = new SoundPool(1,AudioManager.STREAM_MUSIC, 0);
sound = sp.load(this, R.raw.dum, 1);
Button button = (Button)findViewById(R.id.Button02);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sp.play(sound, 1.0f, 1.0f, 0, 0, 1.0f);
}
});
}
}

--------------------------------------------------—-

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です