Example usage for android.media MediaPlayer setDataSource

List of usage examples for android.media MediaPlayer setDataSource

Introduction

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

Prototype

@UnsupportedAppUsage
public void setDataSource(String path, Map<String, String> headers)
        throws IOException, IllegalArgumentException, SecurityException, IllegalStateException 

Source Link

Document

Sets the data source (file-path or http/rtsp URL) to use.

Usage

From source file:Main.java

public static void playSound(Context context, Uri uri) {
    final MediaPlayer player = new MediaPlayer();
    try {/*from   ww w . j a  va  2s .c  om*/
        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

/**
 * Play an alarm sound on the device/*from w  w w. ja  v a  2s  .  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:Main.java

/**
 * Get video's duration without {@link ContentProvider}. Because not know
 * {@link Uri} of video.//w  ww.j  ava2 s. 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:com.metinkale.prayerapp.vakit.AlarmReceiver.java

public static MediaPlayer play(Context c, String sound) throws IOException {
    Uri uri = Uri.parse(sound);/*from   www . j  av a  2s.  c o 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 {//from   w  w w.j  a  va2 s  .c o 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);
    }
}

From source file:com.securecomcode.text.notifications.MessageNotifier.java

private static void sendInThreadNotification(Context context) {
    try {//w  w  w  . j  av  a2 s  .  co  m
        if (!TextSecurePreferences.isInThreadNotifications(context)) {
            return;
        }

        String ringtone = TextSecurePreferences.getNotificationRingtone(context);

        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);
    }
}

From source file:org.thoughtcrime.SMP.notifications.MessageNotifier.java

private static void sendInThreadNotification(Context context, Recipients recipients) {
    try {/*from ww  w . j ava  2  s .  c om*/
        if (!TextSecurePreferences.isInThreadNotifications(context)) {
            return;
        }

        Uri uri = recipients.getRingtone();

        if (uri == null) {
            String ringtone = TextSecurePreferences.getNotificationRingtone(context);

            if (ringtone == null) {
                Log.w(TAG, "ringtone preference was null.");
                return;
            } else {
                uri = Uri.parse(ringtone);
            }
        }

        if (uri == null) {
            Log.w(TAG, "couldn't parse ringtone uri " + TextSecurePreferences.getNotificationRingtone(context));
            return;
        }

        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);
    }
}

From source file:org.schabi.terminightor.NightKillerService.java

private MediaPlayer setupNewMediaPlayer(Alarm alarm) {
    MediaPlayer mediaPlayer = null;
    try {//from ww  w.  jav  a2  s. c o m
        mediaPlayer = new MediaPlayer();
        mediaPlayer.setDataSource(this, Uri.parse(alarm.getAlarmTone()));
        boolean overrideVolume = PreferenceManager.getDefaultSharedPreferences(this)
                .getBoolean(getString(R.string.overrideAlarmVolume), false);
        if (overrideVolume) {
            mediaPlayer.setVolume(1.0f, 1.0f);
        }
        mediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
        mediaPlayer.setLooping(true);
        mediaPlayer.prepare();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return mediaPlayer;
}

From source file:com.google.android.apps.forscience.whistlepunk.metadata.TriggerHelper.java

public void doAudioAlert(Context context) {
    // Use a throttler to keep this from interrupting itself too much.
    if (System.currentTimeMillis() - mLastAudioMs < THROTTLE_LIMIT_MS) {
        return;//from   w w w .j a v  a  2  s.  com
    }
    mLastAudioMs = System.currentTimeMillis();
    final MediaPlayer mediaPlayer = new MediaPlayer();
    mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    try {
        mediaPlayer.setDataSource(context, mNotification);
        mediaPlayer.setOnPreparedListener(MEDIA_PLAYER_ON_PREPARED_LISTENER);
        mediaPlayer.setOnCompletionListener(MEDIA_PLAYER_COMPLETION_LISTENER);
        // Don't prepare the mediaplayer on the UI thread! That's asking for trouble.
        mediaPlayer.prepareAsync();
    } catch (IOException e) {
        if (Log.isLoggable(TAG, Log.DEBUG)) {
            Log.d(TAG, "error getting notification sound");
        }
    }
}

From source file:edu.northwestern.cbits.activitydetector.app.MainActivity.java

private void UpdateGUI() {
    i++;//from w  ww  .j  a v a 2 s.co  m

    final MainActivity me = this;

    // Runs once a second
    mHandler.post(new Runnable() {
        public void run() {
            Log.d(getString(R.string.app_name), ActivityRecognitionIntentService.ACTIVITY_NAME);
            nameView.setText(
                    ActivityRecognitionIntentService.ACTIVITY_NAME + " " + System.currentTimeMillis() / 1000);

            //setting up tone for on_foot
            Uri defaultRingtoneUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            MediaPlayer mediaPlayer = new MediaPlayer();

            try {
                mediaPlayer.setDataSource(me, defaultRingtoneUri);
                mediaPlayer.setAudioStreamType(AudioManager.STREAM_NOTIFICATION);
                mediaPlayer.prepare();
                mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {

                    @Override
                    public void onCompletion(MediaPlayer mp) {
                        mp.release();
                    }
                });

                if (ActivityRecognitionIntentService.ACTIVITY_NAME == "on_foot") {
                    mediaPlayer.start();
                } else {
                    mediaPlayer.release();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            ;
        }
    });
}