如何在Android中将声音与Toast结合使用 [英] How to use Sound with Toast in Android

查看:191
本文介绍了如何在Android中将声音与Toast结合使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的android应用中弹出吐司时播放声音.

I want to play a sound when my toast pops up in my android app.

这是我在主班上烤面包的代码:

Here is my code for the toast in my main class:

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 ):

Here is the sound code in my SoundPool class (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方法中调用它,并且我知道我正在使用touchview.我怎样才能使声音仅在烤面包同时出现时才出现.我一直在尝试,但没有设法解决.

I know I am not calling it in my toast method, and I know that I am using a touchview. How can I make it so the sound appears only when the toast appears, at the same time that is. I have been trying, but have not managed.

我不想碰任何东西.只是为了与烤面包一起玩.

I don't want a touch or anything. Just for it to play with the toast simply.

推荐答案

执行类似的功能

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的地方调用它

and call it where you are displaying your Toast like this

playSound(R.raw.sound);

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

You also need to replace the resource so it matches yours

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

像这样制作一个要播放的int数组或资源,并听完MediaPlayer播放完毕后的声音,告诉他播放下一个,直到播放了数组中的最后一个.

Make a int array or resource you want to play like this and listen for when the MediaPlayer is done with playing, tell him to play the next one until it played the last one in the array.

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]);
        }
    }
}

这篇关于如何在Android中将声音与Toast结合使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆