gpt4 book ai didi

java - 如何在 Android 中使用 Sound with Toast

转载 作者:行者123 更新时间:2023-12-02 04:44:06 25 4
gpt4 key购买 nike

我想在 Android 应用程序中弹出 toast 时播放声音。

这是我的主类中的 toast 代码:

private void workEndNotification() {
//Custom Toast notification for the Work End Reminder.
LayoutInflater inflaterWork = getLayoutInflater();
View toastLayout = inflaterWork.inflate(R.layout.work_notification_toast, (ViewGroup) findViewById(R.id.toast_root_view));
TextView header = (TextView) toastLayout.findViewById(R.id.toast_header);
header.setText("Work Day Over Soon!");
final Toast toastWork = new Toast(getApplicationContext());
toastWork.setGravity(Gravity.CENTER, 0, 0);
toastWork.setDuration(Toast.LENGTH_SHORT);
toastWork.setView(toastLayout);
toastWork.show();

myHandler.postDelayed(new Runnable() {
public void run() {
toastWork.cancel();
}
}, 1);
};

这是我的 SoundPool 类中的声音代码 ( http://www.vogella.com/tutorials/AndroidMedia/article.html ):

class PlaySound extends Activity implements OnTouchListener {
private SoundPool soundPool;
private int soundID;
boolean loaded = false;


/** Called when the activity is first created. */

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.work_notification_toast);
View view = findViewById(R.id.toast_header);
view.setOnTouchListener(this);
// Set the hardware buttons to control the music
this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
// 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;
}
});
soundID = soundPool.load(this, R.raw.sound1, 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(soundID, volume, volume, 1, 0, 1f);
Log.e("Test", "Played sound");
}
}
return false;
}
}

我知道我没有在我的 toast 方法中调用它,并且我知道我正在使用触摸 View 。我怎样才能做到只有当 toast 出现时才出现声音,即同时出现。我一直在努力,但没有成功。

我不想碰触什么的。只是为了简单地玩 toast 。

最佳答案

创建一个这样的函数

private void playSound(int resId){
mp = MediaPlayer.create(MainActivity.this, resId);
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
mediaPlayer.reset();
mediaPlayer.release();
}
});
mp.start();
}

并在显示 Toast 的位置调用它,如下所示

playSound(R.raw.sound);

您还需要替换资源,使其与您的资源匹配

编辑:您似乎想播放几种声音。您可以自己制作一个简单的声音池

像这样创建一个你想要播放的int数组或资源,并监听MediaPlayer何时完成播放,告诉他播放下一个,直到播放数组中的最后一个。

public class TestActivity extends Activity implements MediaPlayer.OnCompletionListener {


private final int[] soundResources = {R.raw.sound1, R.raw.sound2, R.raw.sound3};
private int counter = 0;
private MediaPlayer mp;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
playSound(counter);
}

private void playSound(int resId){
mp = MediaPlayer.create(TestActivity.this, resId);
mp.setOnCompletionListener(this);
mp.start();
}

@Override
public void onCompletion(MediaPlayer mediaPlayer) {
mp.reset();
mp.release();
if(counter < soundResources.length ) {
playSound(soundResources[++counter]);
}
}
}

关于java - 如何在 Android 中使用 Sound with Toast,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29831737/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com