play Audio - Android Media

Android examples for Media:Audio

Description

play Audio

Demo Code


//package com.java2s;
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;

public class Main {

    public static void playAudio(Context context, Uri uri) {
        AudioManager audioManager = (AudioManager) context
                .getSystemService(Context.AUDIO_SERVICE);
        MediaPlayer mediaPlayer;// w w  w  .j  ava  2  s . com

        if (audioManager.getStreamVolume(AudioManager.STREAM_NOTIFICATION) > 0) {
            mediaPlayer = MediaPlayer.create(context, uri);
            mediaPlayer.start();
        }
    }
}

Related Tutorials