play Audio - Android android.media

Android examples for android.media:Audio

Description

play Audio

Demo Code

import android.content.Context;
import android.media.MediaPlayer;

public class Main{

    private static MediaPlayer mediaPlayer;
    public static void playAudio(Context context, int resId) {

        if (context == null)
            throw new NullPointerException("context cannot be null");

        mediaPlayer = MediaPlayer.create(context, resId);
        mediaPlayer.start();/*from   w  w w.  ja  v a 2s .c  o m*/
        mediaPlayer
                .setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                    @Override
                    public void onCompletion(MediaPlayer mp) {
                        mp.release();
                    }
                });
    }

}

Related Tutorials