Example usage for android.media MediaPlayer MediaPlayer

List of usage examples for android.media MediaPlayer MediaPlayer

Introduction

In this page you can find the example usage for android.media MediaPlayer MediaPlayer.

Prototype

public MediaPlayer() 

Source Link

Document

Default constructor.

Usage

From source file:Main.java

public static MediaPlayer getInstance() {
    if (mPlayer == null) {
        mPlayer = new MediaPlayer();
    }
    return mPlayer;
}

From source file:Main.java

public static void playRecordAction(File audiofile) throws Exception {
    MediaPlayer mp = new MediaPlayer();
    FileInputStream fis = new FileInputStream(audiofile.getAbsolutePath());
    mp.setDataSource(fis.getFD());/*from  ww w  .java 2s  . c  o  m*/
    mp.prepare();
    fis.close();
    mp.start();
}

From source file:Main.java

public static void playSound(Context context, Uri uri) {
    final MediaPlayer player = new MediaPlayer();
    try {//from   ww w .  j  a v a 2 s.c  o  m
        player.setDataSource(context.getApplicationContext(), uri);
        player.setAudioStreamType(AudioManager.STREAM_NOTIFICATION);
        player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                mp.start();
            }
        });
        player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                player.release();
            }
        });
        player.prepareAsync();
    } catch (Exception e) {
    }
}

From source file:Main.java

public static void play(String filePath) {
    if (isPlaying)
        return;/*from w w  w .  ja  v a  2s. c om*/

    if (mediaPlayer == null) {
        mediaPlayer = new MediaPlayer();
    }

    mediaPlayer.reset();
    try {
        mediaPlayer.setDataSource(filePath);
        mediaPlayer.prepare();
        mediaPlayer.start();
        isPlaying = true;
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void playSound(String filePath, MediaPlayer.OnCompletionListener listener) {
    if (sMediaPlayer == null) {
        sMediaPlayer = new MediaPlayer();
        sMediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
            @Override//from  w ww.  j av  a  2 s  .c  om
            public boolean onError(MediaPlayer mediaPlayer, int what, int extra) {
                sMediaPlayer.reset();
                return false;
            }
        });
    } else {
        sMediaPlayer.reset();
    }
    try {
        sMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        if (listener != null) {
            sMediaPlayer.setOnCompletionListener(listener);
        }
        sMediaPlayer.setDataSource(filePath);
        sMediaPlayer.prepare();
        sMediaPlayer.start();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (SecurityException e) {
        e.printStackTrace();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void playSound(String filePath, MediaPlayer.OnCompletionListener onCompletionListener) {
    if (mMediaPlayer == null) {
        mMediaPlayer = new MediaPlayer();
        mMediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
            @Override//from   w  w w .j a  v a  2s . c o  m
            public boolean onError(MediaPlayer mp, int what, int extra) {
                mMediaPlayer.reset();
                return false;
            }
        });
    } else {
        mMediaPlayer.reset();
    }
    try {
        mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        if (onCompletionListener != null) {
            mMediaPlayer.setOnCompletionListener(onCompletionListener);
        }

        mMediaPlayer.setDataSource(filePath);
        mMediaPlayer.prepare();
        mMediaPlayer.start();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (SecurityException e) {
        e.printStackTrace();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

From source file:Main.java

/**
 * Play video file from res folder.//w w w.j a  v  a 2 s. co m
 * Then call mediaPlayer.start();
 * @param fileName
 * @param listener
 * @return
 * @throws Exception
 */
public static MediaPlayer playSound(AssetManager assetManager, String fileName,
        MediaPlayer.OnCompletionListener listener) throws Exception {
    MediaPlayer mediaPlayer = new MediaPlayer();
    if (listener != null) {
        mediaPlayer.setOnCompletionListener(listener);
    }

    AssetFileDescriptor descriptor = assetManager.openFd(fileName);
    mediaPlayer.setDataSource(descriptor.getFileDescriptor(), descriptor.getStartOffset(),
            descriptor.getLength());
    descriptor.close();
    mediaPlayer.prepare();

    return mediaPlayer;
}

From source file:Main.java

/**
 * Get video's duration without {@link ContentProvider}. Because not know
 * {@link Uri} of video.//from   w w  w .j  av a 2s.c  o m
 *
 * @param context
 * @param path    Path of video file.
 * @return Duration of video, in milliseconds. Return 0 if path is null.
 */
public static long getDuration(Context context, String path) {
    MediaPlayer mMediaPlayer = null;
    long duration = 0;
    try {
        mMediaPlayer = new MediaPlayer();
        mMediaPlayer.setDataSource(context, Uri.parse(path));
        mMediaPlayer.prepare();
        duration = mMediaPlayer.getDuration();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (mMediaPlayer != null) {
            mMediaPlayer.reset();
            mMediaPlayer.release();
            mMediaPlayer = null;
        }
    }
    return duration;
}

From source file:Main.java

public static void playNotifycationMusic(Context context, String voicePath) throws IOException {
    // paly music ...
    AssetFileDescriptor fileDescriptor = context.getAssets().openFd(voicePath);
    if (mediaPlayer == null) {
        mediaPlayer = new MediaPlayer();
    }/*  w w w.j a v a  2  s. co  m*/
    if (mediaPlayer.isPlaying()) {
        mediaPlayer.stop();
    }
    mediaPlayer.reset();
    mediaPlayer.setDataSource(fileDescriptor.getFileDescriptor(), fileDescriptor.getStartOffset(),
            fileDescriptor.getLength());
    mediaPlayer.prepare();
    mediaPlayer.setLooping(false);
    mediaPlayer.start();
}

From source file:Main.java

/**
 * Play an alarm sound on the device/*  w  ww .j a v a2 s.c  o  m*/
 *
 * @param context The context
 * @return MediaPlayer
 */
public static MediaPlayer playAlarmSound(Context context) {
    MediaPlayer mediaPlayer = new MediaPlayer();
    try {
        mediaPlayer.setDataSource(context, getAlarmUri());
        final AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
        if (audioManager.getStreamVolume(AudioManager.STREAM_ALARM) != 0) {
            mediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
            mediaPlayer.prepare();
            mediaPlayer.start();
        }

        return mediaPlayer;
    } catch (IOException e) {
        return null;
    }
}