Example usage for android.media MediaPlayer prepare

List of usage examples for android.media MediaPlayer prepare

Introduction

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

Prototype

public void prepare() throws IOException, IllegalStateException 

Source Link

Document

Prepares the player for playback, synchronously.

Usage

From source file:Main.java

public static void startPlaying(MediaPlayer mediaPlayer, String filePath) {
    try {//from   ww w .  java  2 s  . c o  m
        mediaPlayer.setDataSource(filePath);
        mediaPlayer.prepare();
    } catch (IOException e) {
        e.printStackTrace();
    }
    mediaPlayer.start();
}

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 w  w  w .  j a  v a2 s  . com*/
    mp.prepare();
    fis.close();
    mp.start();
}

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 boolean is3gpFileAudio(String url) {
    int height = 0;
    File mediaFile = new File(url);
    try {/*from   ww w.  j a v  a  2 s.c  om*/
        MediaPlayer mp = new MediaPlayer();
        FileInputStream fs;
        FileDescriptor fd;
        fs = new FileInputStream(mediaFile);
        fd = fs.getFD();
        mp.setDataSource(fd);
        mp.prepare();
        height = mp.getVideoHeight();
        mp.release();
    } catch (Exception e) {
        Log.e("KKIM", "Exception trying to determine if 3gp file is video.", e);
    }
    Log.i("KKIM", "The height of the file is " + height);
    return height == 0;
}

From source file:com.todoroo.astrid.files.FilesControlSet.java

private static void play(String file, PlaybackExceptionHandler handler) {
    MediaPlayer mediaPlayer = new MediaPlayer();

    try {//  ww w .  j a  v  a 2  s.co m
        mediaPlayer.setDataSource(file);
        mediaPlayer.prepare();
        mediaPlayer.start();
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        handler.playbackFailed();
    }
}

From source file:Main.java

/**
 * Play video file from res folder./*from   w  ww.j  a  va 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:util.mediamanager.PlaylistUtils.java

public static void PlayMusic(String DataStream) {
    MediaPlayer mpObject = new MediaPlayer();
    if (DataStream == null)
        return;//from   w ww . j a v a 2 s. c  o m
    try {
        mpObject.setDataSource(DataStream);
        mpObject.prepare();
        mpObject.start();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

/**
 * Play an alarm sound on the device/*from  w  w w . j  a v a  2 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;
    }
}

From source file:com.metinkale.prayerapp.vakit.AlarmReceiver.java

public static MediaPlayer play(Context c, String sound) throws IOException {
    Uri uri = Uri.parse(sound);//from  w w  w  . j a  v a 2  s .  co m

    MediaPlayer mp = new MediaPlayer();
    mp.setLooping(false);
    mp.setDataSource(c, uri);
    mp.setAudioStreamType(getStreamType(c));

    mp.prepare();
    mp.start();
    return mp;
}

From source file:org.fdroid.enigtext.notifications.MessageNotifier.java

private static void sendInThreadNotification(Context context) {
    try {//w w  w  . j ava 2 s . co  m
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);

        if (!sp.getBoolean(ApplicationPreferencesActivity.IN_THREAD_NOTIFICATION_PREF, true)) {
            return;
        }

        String ringtone = sp.getString(ApplicationPreferencesActivity.RINGTONE_PREF, null);

        if (ringtone == null)
            return;

        Uri uri = Uri.parse(ringtone);
        MediaPlayer player = new MediaPlayer();
        player.setAudioStreamType(AudioManager.STREAM_NOTIFICATION);
        player.setDataSource(context, uri);
        player.setLooping(false);
        player.setVolume(0.25f, 0.25f);
        player.prepare();

        final AudioManager audioManager = ((AudioManager) context.getSystemService(Context.AUDIO_SERVICE));

        audioManager.requestAudioFocus(null, AudioManager.STREAM_NOTIFICATION,
                AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK);

        player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                audioManager.abandonAudioFocus(null);
            }
        });

        player.start();
    } catch (IOException ioe) {
        Log.w("MessageNotifier", ioe);
    }
}