How to use Sound with Toast in Android

自作多情 提交于 2019-12-13 07:51:29

问题


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

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

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.


回答1:


Make a function like this

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

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

EDIT: You seem to want to play several sounds. You can make a simple sound pool on your own

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


来源:https://stackoverflow.com/questions/29831737/how-to-use-sound-with-toast-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!