我需要在我的应用程序中播放一个短音频。我编写了以下代码,但是没有任何声音,并且在我的三星手机上出现了奇怪的震动。但与此同时,此代码在我的Android模拟器上运行良好。我的代码如下:
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);
};
}
请帮我解决这个问题,谢谢!:)