List of usage examples for android.media MediaPlayer MediaPlayer
public MediaPlayer()
From source file:org.botlibre.sdk.activity.ChatActivity.java
public MediaPlayer playAudio(String audio, boolean loop, boolean cache, boolean start) { try {//from ww w . j a v a2 s . c o m Uri audioUri = null; if (cache) { audioUri = HttpGetVideoAction.fetchVideo(this, audio); } if (audioUri == null) { audioUri = Uri.parse(MainActivity.connection.fetchImage(audio).toURI().toString()); } final MediaPlayer audioPlayer = new MediaPlayer(); audioPlayer.setDataSource(getApplicationContext(), audioUri); audioPlayer.setOnErrorListener(new OnErrorListener() { @Override public boolean onError(MediaPlayer mp, int what, int extra) { Log.wtf("Audio error", "what:" + what + " extra:" + extra); audioPlayer.stop(); audioPlayer.release(); return true; } }); audioPlayer.setOnCompletionListener(new OnCompletionListener() { @Override public void onCompletion(MediaPlayer mp) { audioPlayer.release(); runOnUiThread(new Runnable() { public void run() { try { beginListening(); } catch (Exception e) { Log.e("ChatActivity", "MediaPlayer: " + e.getMessage()); } } }); } }); audioPlayer.prepare(); audioPlayer.setLooping(loop); if (start) { audioPlayer.start(); } return audioPlayer; } catch (Exception exception) { Log.wtf(exception.toString(), exception); return null; } }
From source file:org.deviceconnect.android.deviceplugin.host.HostDeviceService.java
/** * 3GP??Video?Audio??.//from ww w . j a v a 2 s . c om * * @param mFile ???URI * @return Video?true, audio?false */ public static boolean checkVideo(final File mFile) { int height = 0; try { mMediaPlayer = new MediaPlayer(); FileInputStream fis = null; FileDescriptor mFd = null; fis = new FileInputStream(mFile); mFd = fis.getFD(); mMediaPlayer.setDataSource(mFd); mMediaPlayer.prepare(); height = mMediaPlayer.getVideoHeight(); mMediaPlayer.release(); fis.close(); } catch (Exception e) { if (BuildConfig.DEBUG) { e.printStackTrace(); } } return height > 0; }
From source file:com.halseyburgund.rwframework.core.RWService.java
/** * Creates a media player for sound playback, with initial volume of 0. *//*from w ww . j a v a 2 s. c om*/ private void createPlayer() { if (mPlayer == null) { mPlayer = new MediaPlayer(); mPlayer.setWakeMode(this, PowerManager.PARTIAL_WAKE_LOCK); float volume = (float) 0.0; mPlayer.setVolume(volume, volume); mVolumeLevel = 0; mPlayer.pause(); mPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { @Override public void onPrepared(MediaPlayer mp) { if (D) { Log.d(TAG, "MediaPlayer prepared event"); } broadcast(RW.READY_TO_PLAY); if (mStartPlayingWhenReady) { playbackFadeIn(mVolumeLevel); } mAssetTracker.start(); } }); mPlayer.setOnInfoListener(new MediaPlayer.OnInfoListener() { @Override public boolean onInfo(MediaPlayer mp, int what, int extra) { if (D) { Log.i(TAG, "MediaPlayer info event"); if (MediaPlayer.MEDIA_INFO_METADATA_UPDATE == what) { Log.i(TAG, "MediaPlayer metadata updated, extra = " + extra); } } return true; } }); mPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() { @Override public boolean onError(MediaPlayer mp, int what, int extra) { if (D) { Log.d(TAG, "MediaPlayer error event"); } mAssetTracker.reset(); return true; } }); mPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { @Override public void onCompletion(MediaPlayer mp) { if (D) { Log.d(TAG, "MediaPlayer completion event"); } mAssetTracker.stop(); mp.stop(); broadcast(RW.PLAYBACK_FINISHED); } }); mPlayer.setOnBufferingUpdateListener(new MediaPlayer.OnBufferingUpdateListener() { @Override public void onBufferingUpdate(MediaPlayer mp, int percent) { if (D) { Log.d(TAG, "MediaPlayer buffering event, %=" + percent); } } }); } }
From source file:github.daneren2005.dsub.service.DownloadService.java
@TargetApi(Build.VERSION_CODES.JELLY_BEAN) private synchronized void setupNext(final DownloadFile downloadFile) { try {/*w ww .j a v a 2 s. c o m*/ final File file = downloadFile.isCompleteFileAvailable() ? downloadFile.getCompleteFile() : downloadFile.getPartialFile(); resetNext(); // Exit when using remote controllers if (remoteState != LOCAL) { return; } nextMediaPlayer = new MediaPlayer(); nextMediaPlayer.setWakeMode(DownloadService.this, PowerManager.PARTIAL_WAKE_LOCK); try { nextMediaPlayer.setAudioSessionId(audioSessionId); } catch (Throwable e) { nextMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); } nextMediaPlayer.setDataSource(file.getPath()); setNextPlayerState(PREPARING); nextMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { public void onPrepared(MediaPlayer mp) { // Changed to different while preparing so ignore if (nextMediaPlayer != mp) { return; } try { setNextPlayerState(PREPARED); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN && (playerState == PlayerState.STARTED || playerState == PlayerState.PAUSED)) { mediaPlayer.setNextMediaPlayer(nextMediaPlayer); nextSetup = true; } applyReplayGain(nextMediaPlayer, downloadFile); } catch (Exception x) { handleErrorNext(x); } } }); nextMediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() { public boolean onError(MediaPlayer mediaPlayer, int what, int extra) { Log.w(TAG, "Error on playing next " + "(" + what + ", " + extra + "): " + downloadFile); return true; } }); nextMediaPlayer.prepareAsync(); } catch (Exception x) { handleErrorNext(x); } }
From source file:com.haomee.chat.activity.ChatActivity.java
private void playBackgroundMusic(int resId) { try {/* w w w.j ava 2s . c om*/ if (mediaPlayer_background == null) { mediaPlayer_background = new MediaPlayer(); } mediaPlayer_background.reset(); AssetFileDescriptor afd = this.getResources().openRawResourceFd(resId); if (afd != null) { mediaPlayer_background.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength()); afd.close(); mediaPlayer_background.setLooping(false); mediaPlayer_background.prepare(); mediaPlayer_background.start(); } } catch (IOException e) { e.printStackTrace(); } }
From source file:com.shinymayhem.radiopresets.ServiceRadioPlayer.java
public static boolean validateUrl(String url) { if (!URLUtil.isHttpUrl(url) && !URLUtil.isHttpsUrl(url)) { //if (LOCAL_LOGV) log("not a valid http or https url", "v"); return false; }/* w w w . j a v a 2 s. c om*/ //check for empty after prefix if (url.equals("http://") || url.equals("https://")) { return false; } MediaPlayer mediaPlayer = new MediaPlayer(); mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); try { mediaPlayer.setDataSource(url); mediaPlayer.release(); } catch (IOException e) { return false; } return true; }
From source file:me.spadival.podmode.PodModeService.java
/** * Makes sure the media player exists and has been reset. This will create * the media player if needed, or reset the existing media player if one * already exists.//from w w w .j a va 2s .co m */ void createMediaPlayerIfNeeded() { if (mPlayer == null) { mPlayer = new MediaPlayer(); // Make sure the media player will acquire a wake-lock while // playing. If we don't do // that, the CPU might go to sleep while the song is playing, // causing playback to stop. // // Remember that to use this, we have to declare the // android.permission.WAKE_LOCK // permission in AndroidManifest.xml. mPlayer.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK); // we want the media player to notify us when it's ready preparing, // and when it's done // playing: mPlayer.setOnPreparedListener(this); mPlayer.setOnCompletionListener(this); mPlayer.setOnErrorListener(this); } else mPlayer.reset(); }
From source file:it.feio.android.omninotes.DetailFragment.java
private void startPlaying(Uri uri) { if (mPlayer == null) { mPlayer = new MediaPlayer(); }/*ww w. ja v a 2 s.com*/ try { mPlayer.setDataSource(mainActivity, uri); mPlayer.prepare(); mPlayer.start(); mPlayer.setOnCompletionListener(mp -> { mPlayer = null; ((ImageView) isPlayingView.findViewById(R.id.gridview_item_picture)) .setImageBitmap(recordingBitmap); recordingBitmap = null; isPlayingView = null; }); } catch (IOException e) { Log.e(Constants.TAG, "prepare() failed", e); mainActivity.showMessage(R.string.error, ONStyle.ALERT); } }
From source file:com.dycody.android.idealnote.DetailFragment.java
private void startPlaying(Uri uri) { if (mPlayer == null) { mPlayer = new MediaPlayer(); }/*w w w. j a v a2s . c o m*/ try { mPlayer.setDataSource(mainActivity, uri); mPlayer.prepare(); mPlayer.start(); mPlayer.setOnCompletionListener(mp -> { mPlayer = null; if (isPlayingView != null) { ((ImageView) isPlayingView.findViewById(R.id.gridview_item_picture)) .setImageBitmap(recordingBitmap); recordingBitmap = null; isPlayingView = null; } }); } catch (IOException e) { Log.e(Constants.TAG, "prepare() failed", e); Toast.makeText(getActivity(), R.string.error, Toast.LENGTH_SHORT).show(); //mainActivity.showMessage(R.string.error, ONStyle.ALERT); } }