List of usage examples for android.media MediaPlayer prepare
public void prepare() throws IOException, IllegalStateException
From source file:com.securecomcode.text.notifications.MessageNotifier.java
private static void sendInThreadNotification(Context context) { try {/*from ww w.j a v a 2s . com*/ 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 {/* w ww.ja v a 2 s . c o m*/ 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:com.visva.voicerecorder.utils.Utils.java
public static long getDurationTimeFromFile(String filePath) { if (StringUtility.isEmpty(filePath)) { AIOLog.e(MyCallRecorderConstant.TAG, "filePath is null"); return 0; }/*from ww w. j av a2 s. co m*/ File file = new File(filePath); if (file == null || !file.exists()) { Log.d("KieuThang", "file it not exitsted!"); } MediaPlayer mediaPlayer = new MediaPlayer(); long duration = 0L; try { mediaPlayer.setDataSource(filePath); mediaPlayer.prepare(); duration = mediaPlayer.getDuration(); mediaPlayer.reset(); mediaPlayer.release(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (SecurityException e) { e.printStackTrace(); } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } //the valid time we offer to save at least more than 1s if (duration > 1000) { AIOLog.d(MyCallRecorderConstant.TAG, "InValid time:" + duration); return duration; } AIOLog.d(MyCallRecorderConstant.TAG, "Valid time:" + duration); return 0; }
From source file:com.xlythe.engine.theme.Theme.java
public static long getDurationOfSound(Context context, Theme.Res res) { int millis = 0; MediaPlayer mp = new MediaPlayer(); try {//from www . java2s .co m AssetFileDescriptor afd; int id = getId(context, res.getType(), res.getName()); if (id == 0) { id = context.getResources().getIdentifier(res.getName(), res.getType(), context.getPackageName()); afd = context.getResources().openRawResourceFd(id); } afd = getThemeContext(context).getResources().openRawResourceFd(id); mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength()); afd.close(); mp.prepare(); millis = mp.getDuration(); } catch (Exception e) { e.printStackTrace(); } finally { mp.release(); mp = null; } return millis; }
From source file:de.azapps.mirakel.helper.TaskDialogHelpers.java
public static void playbackFile(final Activity context, final FileMirakel file, final boolean loud) { final MediaPlayer mPlayer = new MediaPlayer(); final AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); if (!loud) {/*from w w w .j a v a 2 s . c om*/ am.setSpeakerphoneOn(false); am.setMode(AudioManager.MODE_IN_CALL); context.setVolumeControlStream(AudioManager.STREAM_VOICE_CALL); } try { mPlayer.reset(); if (!loud) { mPlayer.setAudioStreamType(AudioManager.STREAM_VOICE_CALL); } mPlayer.setDataSource(file.getFileStream(context).getFD()); mPlayer.prepare(); mPlayer.start(); mPlayer.setOnCompletionListener(new OnCompletionListener() { @Override public void onCompletion(final MediaPlayer mp) { audio_playback_dialog.dismiss(); } }); am.setMode(AudioManager.MODE_NORMAL); audio_playback_playing = true; } catch (final IOException e) { Log.e(TAG, "prepare() failed"); } audio_playback_dialog = new AlertDialog.Builder(context).setTitle(R.string.audio_playback_title) .setPositiveButton(R.string.audio_playback_pause, null) .setNegativeButton(R.string.audio_playback_stop, new DialogInterface.OnClickListener() { @Override public void onClick(final DialogInterface dialog, final int which) { mPlayer.release(); } }).setOnCancelListener(new OnCancelListener() { @Override public void onCancel(final DialogInterface dialog) { mPlayer.release(); dialog.cancel(); } }).create(); audio_playback_dialog.setOnShowListener(new OnShowListener() { @Override public void onShow(final DialogInterface dialog) { final Button button = ((AlertDialog) dialog).getButton(DialogInterface.BUTTON_POSITIVE); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(final View v) { if (audio_playback_playing) { button.setText(R.string.audio_playback_play); mPlayer.pause(); audio_playback_playing = false; } else { button.setText(R.string.audio_playback_pause); mPlayer.start(); audio_playback_playing = true; } } }); } }); audio_playback_dialog.show(); }
From source file:edu.stanford.mobisocial.dungbeetle.feed.objects.VoiceObj.java
@Override public void activate(final Context context, final SignedObj obj) { Runnable r = new Runnable() { @Override//from w ww. ja v a 2 s. co m public void run() { byte[] bytes = obj.getRaw(); if (bytes == null) { Pair<JSONObject, byte[]> p = splitRaw(obj.getJson()); // content = p.first; bytes = p.second; } /*AudioTrack track = new AudioTrack(AudioManager.STREAM_MUSIC, RECORDER_SAMPLERATE, RECORDER_CHANNELS, RECORDER_AUDIO_ENCODING, bytes.length, AudioTrack.MODE_STATIC); track.write(bytes, 0, bytes.length); try { // TODO: hack. Thread.sleep(500); } catch (InterruptedException e) { } track.play(); */ /****/ File file = new File(getTempFilename()); try { OutputStream os = new FileOutputStream(file); BufferedOutputStream bos = new BufferedOutputStream(os); bos.write(bytes, 0, bytes.length); bos.flush(); bos.close(); copyWaveFile(getTempFilename(), getFilename()); deleteTempFile(); MediaPlayer mp = new MediaPlayer(); mp.setDataSource(getFilename()); mp.prepare(); mp.start(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }; if (context instanceof Activity) { ((Activity) context).runOnUiThread(r); } else { r.run(); } }
From source file:org.cyanogenmod.theme.chooser.AudiblePreviewFragment.java
private MediaPlayer initAudibleMediaPlayer(String audiblePath, int type) throws IOException { MediaPlayer mp = mMediaPlayers.get(type); if (mp == null) { mp = new MediaPlayer(); mMediaPlayers.put(type, mp);/*w ww.ja v a 2 s. c o m*/ } else { mp.reset(); } mp.setDataSource(audiblePath); mp.prepare(); mp.setOnCompletionListener(mPlayCompletionListener); return mp; }
From source file:com.phonegap.Capture.java
/** * Get the Image specific attributes/* w ww.j a va 2 s . c om*/ * * @param filePath path to the file * @param obj represents the Media File Data * @param video if true get video attributes as well * @return a JSONObject that represents the Media File Data * @throws JSONException */ private JSONObject getAudioVideoData(String filePath, JSONObject obj, boolean video) throws JSONException { MediaPlayer player = new MediaPlayer(); try { player.setDataSource(filePath); player.prepare(); obj.put("duration", player.getDuration()); if (video) { obj.put("height", player.getVideoHeight()); obj.put("width", player.getVideoWidth()); } } catch (IOException e) { Log.d(LOG_TAG, "Error: loading video file"); } return obj; }
From source file:edu.northwestern.cbits.activitydetector.app.MainActivity.java
private void UpdateGUI() { i++;// w w w . j a v a2s.c o 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(); } ; } }); }
From source file:org.apache.cordova.Capture.java
/** * Get the Image specific attributes/*from www . j a v a 2 s .c o m*/ * * @param filePath path to the file * @param obj represents the Media File Data * @param video if true get video attributes as well * @return a JSONObject that represents the Media File Data * @throws JSONException */ private JSONObject getAudioVideoData(String filePath, JSONObject obj, boolean video) throws JSONException { MediaPlayer player = new MediaPlayer(); try { player.setDataSource(filePath); player.prepare(); obj.put("duration", player.getDuration() / 1000); if (video) { obj.put("height", player.getVideoHeight()); obj.put("width", player.getVideoWidth()); } } catch (IOException e) { Log.d(LOG_TAG, "Error: loading video file"); } return obj; }