PDA

View Full Version : کار با صوت ( soundPool )



big lost
جمعه 24 آبان 1392, 18:06 عصر
این کد button1 رو تنظیم کرد تا وقتی روش کلیک میشه آهنگی که در اینجا اسمش رو tabl گذاشتم به صدا در بیاد

اینم کدش :



import android.app.Activity;
import android.media.AudioManager;
import android.media.SoundPool;
import android.media.SoundPool.OnLoadCompleteListener;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

public class MainActivity extends Activity implements OnTouchListener {
private SoundPool soundPool;
private int soundID1;
boolean loaded = false;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View view = findViewById(R.id.button1);
view.setOnTouchListener(this);
// Set the hardware buttons to control the music
this.setVolumeControlStream(AudioManager.STREAM_MU SIC);
// Load the sound
soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
@Override
public void onLoadComplete(SoundPool soundPool, int sampleId,
int status) {
loaded = true;
}
});
soundID1 = soundPool.load(this, R.raw.tabl, 1);


}


@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
// Getting the user sound settings
AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
float actualVolume = (float) audioManager
.getStreamVolume(AudioManager.STREAM_MUSIC);
float maxVolume = (float) audioManager
.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
float volume = actualVolume / maxVolume;
// Is the sound loaded already?
if (loaded) {
soundPool.play(soundID1, volume, volume, 1, 0, 1f);
Log.e("Test", "Played sound");
}
}
return false;
}

}

حالا یه مشکل دارم اگه بخوام button2 رو هم بهش اضافه کنم باید کدش رو چجوری بنویسم ؟؟؟