List of usage examples for android.media MediaPlayer getDuration
public native int getDuration();
From source file:com.sentaroh.android.TaskAutomation.TaskExecutor.java
final static public void playBackMusic(TaskManagerParms taskMgrParms, EnvironmentParms envParms, CommonUtilities util, TaskResponse taskResponse, ActionResponse ar, String action, String dlg_id, String fp, String vol_left, String vol_right) { ar.action_resp = ActionResponse.ACTION_SUCCESS; File lf = new File(fp); if (!lf.exists()) { ar.action_resp = ActionResponse.ACTION_ERROR; ar.resp_msg_text = String.format(taskMgrParms.teMsgs.msgs_thread_task_play_sound_notfound, fp); return;// w w w.ja v a 2s . c o m } if (!isRingerModeNormal(envParms)) { ar.action_resp = ActionResponse.ACTION_WARNING; ar.resp_msg_text = taskMgrParms.teMsgs.msgs_thread_task_exec_ignore_sound_ringer_not_normal; return; } MediaPlayer player = MediaPlayer.create(taskMgrParms.context, Uri.parse(fp)); taskResponse.active_action_name = action; if (player != null) { int duration = player.getDuration(); TaskManager.showMessageDialog(taskMgrParms, envParms, util, taskResponse.active_group_name, taskResponse.active_task_name, taskResponse.active_action_name, taskResponse.active_dialog_id, MESSAGE_DIALOG_MESSAGE_TYPE_SOUND, fp); if (!vol_left.equals("-1") && !vol_left.equals("")) player.setVolume(Float.valueOf(vol_left) / 100, Float.valueOf(vol_right) / 100); player.start(); waitTimeTc(taskResponse, duration + 10); if (!taskResponse.active_thread_ctrl.isEnable()) { ar.action_resp = ActionResponse.ACTION_CANCELLED; ar.resp_msg_text = "Action was cancelled"; } player.stop(); player.release(); TaskManager.closeMessageDialog(taskMgrParms, envParms, util, taskResponse); } else { ar.action_resp = ActionResponse.ACTION_ERROR; ar.resp_msg_text = String.format(taskMgrParms.teMsgs.msgs_thread_task_play_sound_error, fp); } }
From source file:com.geryon.ocraa.MusicService.java
/** Called when media player is done preparing. */ public void onPrepared(MediaPlayer player) { // The media player is done preparing. That means we can start playing! updateProgressBar();//w w w . j ava 2 s .c o m mState = State.Playing; updateNotification(mSongTitle + " (playing)"); Intent in = new Intent(); in.setAction("SETDUR"); in.putExtra("duration", player.getDuration()); in.putExtra("name", mSongTitle); this.sendBroadcast(in); configAndStartMediaPlayer(); }
From source file:org.dharmaseed.android.PlayTalkActivity.java
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_play_talk); // Turn on action bar up/home button ActionBar actionBar = getSupportActionBar(); if (actionBar != null) { actionBar.setDisplayHomeAsUpEnabled(true); }/*from w w w.j av a 2s . c o m*/ // Get the ID of the talk to display Intent i = getIntent(); talkID = (int) i.getLongExtra(NavigationActivity.TALK_DETAIL_EXTRA, 0); dbManager = DBManager.getInstance(this); // only hit the DB again if we know the talk is different than the one // we have saved. // for example, if the user selects a talk, exits, and re-opens it, no need // to hit the DB again, since we already have that talk saved if (talk == null || talk.getId() != talkID) { Cursor cursor = getCursor(); if (cursor.moveToFirst()) { // convert DB result to an object talk = new Talk(cursor); talk.setId(talkID); } else { Log.e(LOG_TAG, "Could not look up talk, id=" + talkID); cursor.close(); return; } cursor.close(); } // else we already have the talk, just re-draw the page // Set the talk title TextView titleView = (TextView) findViewById(R.id.play_talk_talk_title); titleView.setText(talk.getTitle()); // Set the teacher name TextView teacherView = (TextView) findViewById(R.id.play_talk_teacher); teacherView.setText(talk.getTeacherName()); // Set the center name TextView centerView = (TextView) findViewById(R.id.play_talk_center); centerView.setText(talk.getCenterName()); // Set the talk description TextView descriptionView = (TextView) findViewById(R.id.play_talk_talk_description); descriptionView.setText(talk.getDescription()); // Set teacher photo String photoFilename = talk.getPhotoFileName(); ImageView photoView = (ImageView) findViewById(R.id.play_talk_teacher_photo); Log.i(LOG_TAG, "photoFilename: " + photoFilename); try { FileInputStream photo = openFileInput(photoFilename); photoView.setImageBitmap(BitmapFactory.decodeStream(photo)); } catch (FileNotFoundException e) { Drawable icon = ContextCompat.getDrawable(this, R.drawable.dharmaseed_icon); photoView.setImageDrawable(icon); } // set the image of the download button based on whether the talk is // downloaded or not toggleDownloadImage(); // Set date TextView dateView = (TextView) findViewById(R.id.play_talk_date); String recDate = talk.getDate(); SimpleDateFormat parser = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try { dateView.setText(DateFormat.getDateInstance().format(parser.parse(recDate))); } catch (ParseException e) { dateView.setText(""); Log.w(LOG_TAG, "Could not parse talk date for talk ID " + talkID); } // Get/create a persistent fragment to manage the MediaPlayer instance FragmentManager fm = getSupportFragmentManager(); talkPlayerFragment = (TalkPlayerFragment) fm.findFragmentByTag("talkPlayerFragment"); if (talkPlayerFragment == null) { // add the fragment talkPlayerFragment = new TalkPlayerFragment(); fm.beginTransaction().add(talkPlayerFragment, "talkPlayerFragment").commit(); } else if (talkPlayerFragment.getMediaPlayer().isPlaying()) { setPPButton("ic_media_pause"); } // Set the talk duration final TextView durationView = (TextView) findViewById(R.id.play_talk_talk_duration); double duration = talk.getDurationInMinutes(); String durationStr = DateUtils.formatElapsedTime((long) (duration * 60)); durationView.setText(durationStr); // Start a handler to periodically update the seek bar and talk time final SeekBar seekBar = (SeekBar) findViewById(R.id.play_talk_seek_bar); seekBar.setMax((int) (duration * 60 * 1000)); userDraggingSeekBar = false; userSeekBarPosition = 0; seekBar.setOnSeekBarChangeListener(this); final Handler handler = new Handler(); final MediaPlayer mediaPlayer = talkPlayerFragment.getMediaPlayer(); handler.post(new Runnable() { @Override public void run() { handler.postDelayed(this, 1000); if (talkPlayerFragment.getMediaPrepared() && !userDraggingSeekBar) { try { int pos = mediaPlayer.getCurrentPosition(); int mpDuration = mediaPlayer.getDuration(); seekBar.setMax(mpDuration); seekBar.setProgress(pos); String posStr = DateUtils.formatElapsedTime(pos / 1000); String mpDurStr = DateUtils.formatElapsedTime(mpDuration / 1000); durationView.setText(posStr + "/" + mpDurStr); } catch (IllegalStateException e) { } } } }); }
From source file:com.sentaroh.android.TaskAutomation.TaskExecutor.java
final static public void playBackRingtone(TaskManagerParms taskMgrParms, EnvironmentParms envParms, CommonUtilities util, TaskResponse taskResponse, ActionResponse ar, String dlg_id, String rt, String rn, String rp, String vol_left, String vol_right) { ar.action_resp = ActionResponse.ACTION_SUCCESS; if (!isRingerModeNormal(envParms)) { ar.action_resp = ActionResponse.ACTION_WARNING; ar.resp_msg_text = taskMgrParms.teMsgs.msgs_thread_task_exec_ignore_sound_ringer_not_normal; return;/*from w w w . java 2s .c o m*/ } MediaPlayer player = MediaPlayer.create(taskMgrParms.context, Uri.parse("content://media" + rp)); // taskResponse.active_action_name=action; if (player != null) { int duration = 0; if (!rt.equals(PROFILE_ACTION_RINGTONE_TYPE_NOTIFICATION)) duration = RINGTONE_PLAYBACK_TIME; else duration = player.getDuration(); if (duration >= 5000) { TaskManager.showMessageDialog(taskMgrParms, envParms, util, taskResponse.active_group_name, taskResponse.active_task_name, taskResponse.active_action_name, taskResponse.active_dialog_id, MESSAGE_DIALOG_MESSAGE_TYPE_SOUND, rt + " " + rn); } if (!vol_left.equals("-1") && !vol_left.equals("")) player.setVolume(Float.valueOf(vol_left) / 100, Float.valueOf(vol_right) / 100); player.start(); waitTimeTc(taskResponse, duration + 10); if (!taskResponse.active_thread_ctrl.isEnable()) { ar.action_resp = ActionResponse.ACTION_CANCELLED; ar.resp_msg_text = "Action was cancelled"; } player.stop(); player.release(); if (duration >= 5000) { TaskManager.closeMessageDialog(taskMgrParms, envParms, util, taskResponse); } } else { ar.action_resp = ActionResponse.ACTION_ERROR; ar.resp_msg_text = String.format(taskMgrParms.teMsgs.msgs_thread_task_play_sound_error, rp); } }
From source file:com.nd.android.u.square.service.MusicPlaybackService.java
/** * Called when media player is done preparing. *//*from ww w .j av a 2 s . c o m*/ public void onPrepared(MediaPlayer player) { Log.d(TAG, "prepared: " + mSongTitle); // The media player is done preparing. That means we can start playing! mState = State.Playing; updateNotification(mSongTitle + ""); configAndStartMediaPlayer(); if (mPlayingItem.lastPosition > 0 && mPlayingItem.lastPosition <= player.getDuration()) player.seekTo(mPlayingItem.lastPosition); }
From source file:com.nttec.everychan.ui.gallery.GalleryActivity.java
private void setVideo(final GalleryItemViewTag tag, final File file) { runOnUiThread(new Runnable() { @Override/*from w ww .j a v a 2s .c o m*/ public void run() { setOnClickView(tag, getString(R.string.gallery_tap_to_play), new View.OnClickListener() { @Override public void onClick(View v) { if (!settings.useInternalVideoPlayer()) { openExternal(); } else { recycleTag(tag, false); tag.thumbnailView.setVisibility(View.GONE); View videoContainer = inflater.inflate(R.layout.gallery_videoplayer, tag.layout); final VideoView videoView = (VideoView) videoContainer .findViewById(R.id.gallery_video_view); final TextView durationView = (TextView) videoContainer .findViewById(R.id.gallery_video_duration); videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { @Override public void onPrepared(final MediaPlayer mp) { mp.setLooping(true); durationView.setText("00:00 / " + formatMediaPlayerTime(mp.getDuration())); tag.timer = new Timer(); tag.timer.schedule(new TimerTask() { @Override public void run() { runOnUiThread(new Runnable() { @Override public void run() { try { durationView.setText( formatMediaPlayerTime(mp.getCurrentPosition()) + " / " + formatMediaPlayerTime(mp.getDuration())); } catch (Exception e) { Logger.e(TAG, e); tag.timer.cancel(); } } }); } }, 1000, 1000); videoView.start(); } }); videoView.setOnErrorListener(new MediaPlayer.OnErrorListener() { @Override public boolean onError(MediaPlayer mp, int what, int extra) { Logger.e(TAG, "(Video) Error code: " + what); if (tag.timer != null) tag.timer.cancel(); showError(tag, getString(R.string.gallery_error_play)); return true; } }); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ECLAIR) { CompatibilityImpl.setVideoViewZOrderOnTop(videoView); } videoView.setVideoPath(file.getAbsolutePath()); } } }); } }); }
From source file:com.nttec.everychan.ui.gallery.GalleryActivity.java
private void setAudio(final GalleryItemViewTag tag, final File file) { runOnUiThread(new Runnable() { @Override//from ww w.j a va2 s.c om public void run() { setOnClickView(tag, getString(R.string.gallery_tap_to_play), new View.OnClickListener() { @Override public void onClick(View v) { if (!settings.useInternalAudioPlayer()) { openExternal(); } else { recycleTag(tag, false); final TextView durationView = new TextView(GalleryActivity.this); durationView.setGravity(Gravity.CENTER); tag.layout.setVisibility(View.VISIBLE); tag.layout.addView(durationView); tag.audioPlayer = new MediaPlayer(); tag.audioPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { @Override public void onPrepared(final MediaPlayer mp) { mp.setLooping(true); durationView.setText( getSpannedText("00:00 / " + formatMediaPlayerTime(mp.getDuration()))); tag.timer = new Timer(); tag.timer.schedule(new TimerTask() { @Override public void run() { runOnUiThread(new Runnable() { @Override public void run() { try { durationView.setText(getSpannedText( formatMediaPlayerTime(mp.getCurrentPosition()) + " / " + formatMediaPlayerTime(mp.getDuration()))); } catch (Exception e) { Logger.e(TAG, e); tag.timer.cancel(); } } }); } }, 1000, 1000); mp.start(); } }); tag.audioPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() { @Override public boolean onError(MediaPlayer mp, int what, int extra) { Logger.e(TAG, "(Audio) Error code: " + what); if (tag.timer != null) tag.timer.cancel(); showError(tag, getString(R.string.gallery_error_play)); return true; } }); try { tag.audioPlayer.setDataSource(file.getAbsolutePath()); tag.audioPlayer.prepareAsync(); } catch (Exception e) { Logger.e(TAG, "audio player error", e); if (tag.timer != null) tag.timer.cancel(); showError(tag, getString(R.string.gallery_error_play)); } } } }); } }); }
From source file:com.wwtv.player.mediaplayer.LocalPlayerActivity.java
private void setupControlsCallbacks() { mVideoView.setOnErrorListener(new OnErrorListener() { @Override/*from w w w . j av a2 s . co m*/ public boolean onError(MediaPlayer mp, int what, int extra) { Log.e(TAG, "OnErrorListener.onError(): VideoView encountered an " + "error, what: " + what + ", extra: " + extra); String msg; if (extra == MediaPlayer.MEDIA_ERROR_TIMED_OUT) { msg = getString(R.string.video_error_media_load_timeout); } else if (what == MediaPlayer.MEDIA_ERROR_SERVER_DIED) { msg = getString(R.string.video_error_server_unaccessible); } else { msg = getString(R.string.video_error_unknown_error); } Utils.showErrorDialog(LocalPlayerActivity.this, msg); mVideoView.stopPlayback(); mPlaybackState = PlaybackState.IDLE; updatePlayButton(mPlaybackState); return true; } }); mVideoView.setOnPreparedListener(new OnPreparedListener() { @Override public void onPrepared(MediaPlayer mp) { Log.d(TAG, "onPrepared is reached"); mDuration = mp.getDuration(); mEndText.setText( com.google.android.libraries.cast.companionlibrary.utils.Utils.formatMillis(mDuration)); mSeekbar.setMax(mDuration); restartTrickplayTimer(); } }); mVideoView.setOnCompletionListener(new OnCompletionListener() { @Override public void onCompletion(MediaPlayer mp) { stopTrickplayTimer(); Log.d(TAG, "setOnCompletionListener()"); mPlaybackState = PlaybackState.IDLE; updatePlayButton(mPlaybackState); } }); mVideoView.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { if (!mControllersVisible) { updateControllersVisibility(true); } startControllersTimer(); return false; } }); mSeekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() { @Override public void onStopTrackingTouch(SeekBar seekBar) { if (mPlaybackState == PlaybackState.PLAYING) { play(seekBar.getProgress()); } else if (mPlaybackState != PlaybackState.IDLE) { mVideoView.seekTo(seekBar.getProgress()); } startControllersTimer(); } @Override public void onStartTrackingTouch(SeekBar seekBar) { stopTrickplayTimer(); mVideoView.pause(); stopControllersTimer(); } @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { mStartText.setText( com.google.android.libraries.cast.companionlibrary.utils.Utils.formatMillis(progress)); } }); mPlayPause.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (mLocation == PlaybackLocation.LOCAL) { togglePlayback(); } } }); }
From source file:com.google.sample.cast.refplayer.mediaplayer.LocalPlayerActivity.java
private void setupControlsCallbacks() { mVideoView.setOnErrorListener(new OnErrorListener() { @Override/* w w w . j ava2 s .c o m*/ public boolean onError(MediaPlayer mp, int what, int extra) { Log.e(TAG, "OnErrorListener.onError(): VideoView encountered an " + "error, what: " + what + ", extra: " + extra); String msg; if (extra == MediaPlayer.MEDIA_ERROR_TIMED_OUT) { msg = getString(R.string.video_error_media_load_timeout); } else if (what == MediaPlayer.MEDIA_ERROR_SERVER_DIED) { msg = getString(R.string.video_error_server_unaccessible); } else { msg = getString(R.string.video_error_unknown_error); } Utils.showErrorDialog(LocalPlayerActivity.this, msg); mVideoView.stopPlayback(); mPlaybackState = PlaybackState.IDLE; updatePlayButton(mPlaybackState); return true; } }); mVideoView.setOnPreparedListener(new OnPreparedListener() { @Override public void onPrepared(MediaPlayer mp) { Log.d(TAG, "onPrepared is reached"); mDuration = mp.getDuration(); mEndText.setText(Utils.formatMillis(mDuration)); mSeekbar.setMax(mDuration); restartTrickplayTimer(); } }); mVideoView.setOnCompletionListener(new OnCompletionListener() { @Override public void onCompletion(MediaPlayer mp) { stopTrickplayTimer(); Log.d(TAG, "setOnCompletionListener()"); mPlaybackState = PlaybackState.IDLE; updatePlayButton(mPlaybackState); } }); mVideoView.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { if (!mControllersVisible) { updateControllersVisibility(true); } startControllersTimer(); return false; } }); mSeekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() { @Override public void onStopTrackingTouch(SeekBar seekBar) { if (mPlaybackState == PlaybackState.PLAYING) { play(seekBar.getProgress()); } else if (mPlaybackState != PlaybackState.IDLE) { mVideoView.seekTo(seekBar.getProgress()); } startControllersTimer(); } @Override public void onStartTrackingTouch(SeekBar seekBar) { stopTrickplayTimer(); mVideoView.pause(); stopControllersTimer(); } @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { mStartText.setText(Utils.formatMillis(progress)); } }); mPlayPause.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (mLocation == PlaybackLocation.LOCAL) { togglePlayback(); } } }); }
From source file:com.hectorosorio.hosocast.mediaplayer.LocalPlayerActivity.java
private void setupControlsCallbacks() { mVideoView.setOnErrorListener(new MediaPlayer.OnErrorListener() { @Override//from w ww .ja va2s . c o m public boolean onError(MediaPlayer mp, int what, int extra) { Log.e(TAG, "OnErrorListener.onError(): VideoView encountered an " + "error, what: " + what + ", extra: " + extra); String msg; if (extra == MediaPlayer.MEDIA_ERROR_TIMED_OUT) { msg = getString(R.string.video_error_media_load_timeout); } else if (what == MediaPlayer.MEDIA_ERROR_SERVER_DIED) { msg = getString(R.string.video_error_server_unaccessible); } else { msg = getString(R.string.video_error_unknown_error); } Utils.showErrorDialog(LocalPlayerActivity.this, msg); mVideoView.stopPlayback(); mPlaybackState = PlaybackState.IDLE; updatePlayButton(mPlaybackState); return true; } }); mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { @Override public void onPrepared(MediaPlayer mp) { Log.d(TAG, "onPrepared is reached"); mDuration = mp.getDuration(); mEndText.setText( com.google.android.libraries.cast.companionlibrary.utils.Utils.formatMillis(mDuration)); mSeekbar.setMax(mDuration); restartTrickplayTimer(); } }); mVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { @Override public void onCompletion(MediaPlayer mp) { stopTrickplayTimer(); Log.d(TAG, "setOnCompletionListener()"); mPlaybackState = PlaybackState.IDLE; updatePlayButton(mPlaybackState); } }); mVideoView.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { if (!mControllersVisible) { updateControllersVisibility(true); } startControllersTimer(); return false; } }); mSeekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onStopTrackingTouch(SeekBar seekBar) { if (mPlaybackState == PlaybackState.PLAYING) { play(seekBar.getProgress()); } else if (mPlaybackState != PlaybackState.IDLE) { mVideoView.seekTo(seekBar.getProgress()); } startControllersTimer(); } @Override public void onStartTrackingTouch(SeekBar seekBar) { stopTrickplayTimer(); mVideoView.pause(); stopControllersTimer(); } @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { mStartText.setText( com.google.android.libraries.cast.companionlibrary.utils.Utils.formatMillis(progress)); } }); mPlayPause.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (mLocation == PlaybackLocation.LOCAL) { togglePlayback(); } } }); }