问题描述
|
我需要在应用程序中播放简短的声音。我编写了以下代码,但是三星手机上没有声音和奇怪的振动。但与此同时,此代码也可以在我的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,1,1f);
};
}
请帮我解决这个问题!先感谢您! :)
解决方法
音量控制或声音文件播放可能有问题:如果将ѭ1更改为该功能,它会做什么?如果您的手机未正确处理
R.raw.error
文件格式,但仿真器正确执行了操作,那将非常奇怪。
public void Sound(){
AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
float volume = (float) audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
android.util.Log.v(\"SOUND\",\"[\"+volume+\"][\"+spool.play(soundID,volume,1,1f)+\"]\");
};