NFC で Android プロジェクトを実行しましたが、NFC の日付が成功した後に警告音を追加したいのですが、どうすればよいですか?
1 に答える
1
SoundPool クラスを使用します。音声ファイルを /res/raw/ に配置し、最初に読み込みを開始して (たとえば、OnCreate メソッドで)、選択した音声ファイルの再生を要求します。コード例は次のとおりです。
private SoundPool soundPool;
private boolean tiltSoundsLoaded = false;
private int tiltSoundID;
private int tiltFailureSoundID;
public void initTiltSounds(Context context) {
soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
@Override
public void onLoadComplete(SoundPool soundPool, int sampleId,
int status) {
tiltSoundsLoaded = true;
}
});
tiltSoundID = soundPool.load(context, R.raw.swosh_sound_effect, 1);
tiltFailureSoundID = soundPool.load(context, R.raw.fail_metallic, 1);
}
public void playTiltSound(AudioManager audioManager, boolean success) {
try{
int soundToPlay;
if(success)
soundToPlay = tiltSoundID;
else
soundToPlay = tiltFailureSoundID;
float actualVolume = (float) audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
if (tiltSoundsLoaded)
soundPool.play(soundToPlay, actualVolume, actualVolume, 1, 0, 1f);
else
Log.e(LOG_TAG, "Tilt Sound not loaded");
}catch(Exception e){
Log.e(LOG_TAG, "Could not play tilt sound");
}
}
于 2012-09-29T10:02:43.007 に答える