List of usage examples for android.media MediaPlayer setOnCompletionListener
public void setOnCompletionListener(OnCompletionListener listener)
From source file:Main.java
public static void playSoundfile(Context context, int R_raw_fileid) { MediaPlayer mp = MediaPlayer.create(context, R_raw_fileid); mp.setOnCompletionListener(new OnCompletionListener() { @Override// w w w . j a va 2s . c o m public void onCompletion(MediaPlayer mp) { mp.release(); } }); mp.start(); }
From source file:Main.java
/** * Play video file from res folder./*from ww w . j a v a2 s . c o 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
public static void playSound(Context context, Uri uri) { final MediaPlayer player = new MediaPlayer(); try {//from w ww . j av a 2 s . com 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:org.fdroid.enigtext.notifications.MessageNotifier.java
private static void sendInThreadNotification(Context context) { try {/*from www . ja va 2 s. c om*/ 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 {/*www. jav 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 {/*from w w w.j a v a2 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.dudka.rich.streamingmusicplayer.ServiceMusicPlayer.java
@Override public void onPrepared(MediaPlayer mp) { mp.start();/*from w w w. ja v a2 s . co m*/ mp.setOnCompletionListener(this); sendLocalBroadcast(MainActivity.PLAYER_STARTED); }
From source file:mozilla.voicejam.singwithme.CameraActivity.java
void play() { Uri uri = Uri.parse(filePath);// www .j a v a2 s . c o m MediaPlayer mediaPlayer = MediaPlayer.create(this, uri); mediaPlayer.start(); mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { @Override public void onCompletion(MediaPlayer MP) { MP.release(); } }); }
From source file:com.jungle.base.utils.MiscUtils.java
public static void playSound(Context context, int soundResId) { final MediaPlayer player = MediaPlayer.create(context, soundResId); if (player == null) { return;//w w w . j av a 2 s. c om } AudioManager manager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); int maxVolume = manager.getStreamMaxVolume(AudioManager.STREAM_MUSIC); int currVolume = manager.getStreamVolume(AudioManager.STREAM_MUSIC); float volume = 1.0f; if (maxVolume > 0) { volume = (float) currVolume / (float) maxVolume; } player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { @Override public void onCompletion(MediaPlayer mp) { player.release(); } }); player.setVolume(volume, volume); player.start(); }
From source file:net.potterpcs.recipebook.TimerFragment.java
@Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); startButton = (Button) getActivity().findViewById(R.id.timerstartbutton); stopButton = (Button) getActivity().findViewById(R.id.timerstopbutton); display = (TextView) getActivity().findViewById(R.id.timerdisplay); minutePicker = (NumberPicker) getActivity().findViewById(R.id.minutepicker); secondPicker = (NumberPicker) getActivity().findViewById(R.id.secondpicker); timermin = (EditText) getActivity().findViewById(R.id.timerdisplayminutes); timersec = (EditText) getActivity().findViewById(R.id.timerdisplayseconds); if (minutePicker != null) { minutePicker.setMinValue(0);//from w w w .j a v a 2 s . co m minutePicker.setMaxValue(99); secondPicker.setMinValue(0); secondPicker.setMaxValue(59); secondPicker.setFormatter(new Formatter() { @Override public String format(int value) { return String.format("%02d", value); } }); getActivity().findViewById(R.id.pickerlayout).setVisibility(View.VISIBLE); display.setVisibility(View.GONE); } startButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { int seconds; if (minutePicker != null) { seconds = minutePicker.getValue() * 60 + secondPicker.getValue(); } else { int m = Integer.parseInt(timermin.getEditableText().toString()); int s = Integer.parseInt(timersec.getEditableText().toString()); seconds = m * 60 + s; timermin.setText(timermin.getText(), BufferType.NORMAL); timersec.setText(timersec.getText(), BufferType.NORMAL); } startButton.setEnabled(false); stopButton.setEnabled(true); if (minutePicker == null) { timermin.setEnabled(false); timersec.setEnabled(false); } else { minutePicker.setEnabled(false); secondPicker.setEnabled(false); getActivity().findViewById(R.id.pickerlayout).setVisibility(View.GONE); display.setVisibility(View.VISIBLE); } if (display != null) { display.setText(DateUtils.formatElapsedTime(seconds)); } timer = new CountDownTimer(seconds * 1000, 1000) { @Override public void onTick(long millisUntilFinished) { // Log.i(TAG, "tick " + millisUntilFinished); if (display != null) { display.setText(DateUtils.formatElapsedTime(millisUntilFinished / 1000)); } else { int totalsec = (int) (millisUntilFinished / 1000); int s = totalsec % 60; int m = totalsec / 60; timermin.setText(String.format("%02d", m)); timersec.setText(String.format("%02d", s)); } } @Override public void onFinish() { // Play a sound and show a message when the timer ends MediaPlayer mp = MediaPlayer.create(getActivity(), R.raw.ding); mp.setOnCompletionListener(new OnCompletionListener() { @Override public void onCompletion(MediaPlayer mp) { mp.release(); } }); mp.setVolume(1.0f, 1.0f); Toast.makeText(getActivity(), "Done!", Toast.LENGTH_LONG).show(); mp.start(); clearToZero(); } }; timer.start(); } }); stopButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { timer.cancel(); clearToZero(); } }); }