我需要在應用程序中播放短暫的聲音。我寫了下面的代碼,但我的三星手機上沒有出現聲音和奇怪的振動。但在同一時間,這段代碼在我的android模擬器上運行良好。我的代碼是:使用SoundPool播放聲音
package com.samplers;
import android.app.Activity;
import android.media.SoundPool;
import android.media.AudioManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class FixVibroActivity extends Activity {
/** Called when the activity is first created. */
private Button white;
private SoundPool spool;
private int soundID;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
spool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
soundID = spool.load(this, R.raw.error, 1);
white = (Button)findViewById(R.id.whiteBtn);
white.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Sound();
}
});
}
public void Sound(){
AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
float volume = (float) audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
spool.play(soundID, volume, volume, 1, 0, 1f);
};
}
請幫我解決這個問題,請!先謝謝你! :)
你可以檢查logcat是否打印任何有趣的錯誤? – FeatureCreep