play Audio Via MediaPlayer - Android Media

Android examples for Media:Media Player

Description

play Audio Via MediaPlayer

Demo Code


//package com.java2s;
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();/* w w w.  jav  a2s  .  c  o  m*/
        mediaPlayer
                .setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                    @Override
                    public void onCompletion(MediaPlayer mp) {
                        mp.release();
                    }
                });
    }
}

Related Tutorials