本文整理汇总了Java中android.media.MediaPlayer.getDuration方法的典型用法代码示例。如果您正苦于以下问题:Java MediaPlayer.getDuration方法的具体用法?Java MediaPlayer.getDuration怎么用?Java MediaPlayer.getDuration使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类android.media.MediaPlayer
的用法示例。
在下文中一共展示了MediaPlayer.getDuration方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getLength
import android.media.MediaPlayer; //导入方法依赖的package包/类
public int getLength(String songid)
{
MediaPlayer mp = new MediaPlayer();
Uri musicURI = Uri.withAppendedPath(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, songid);
try
{
mp.setDataSource(context, musicURI);
mp.prepare();
}
catch (Exception e)
{
}
return mp.getDuration();
}
示例2: getDuration
import android.media.MediaPlayer; //导入方法依赖的package包/类
public static int getDuration(MediaPlayer mediaPlayer, AssetFileDescriptor afd) {
int duration = 500;
try {
mediaPlayer.reset();
mediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
afd.close();
mediaPlayer.prepare();
duration = mediaPlayer.getDuration();
} catch(IOException e) {
Log.e(TAG, e.toString());
}
mediaPlayer.reset();
return duration;
}
示例3: setMediaPlayer
import android.media.MediaPlayer; //导入方法依赖的package包/类
public void setMediaPlayer(MediaPlayer player) {
rippleVisualizerView.setMediaPlayer(player);
int durationMilliseconds = player.getDuration();
if (durationMilliseconds == -1) {
durationMilliseconds = 0;
}
setSecondToFirstDecimalPoint(durationMilliseconds);
rippleVisualizerView.setOnMediaPlayFinishCallbackk(new OnMediaPlayFinishCallback() {
@Override
public void finished() {
currentMediaState = State.PAUSED;
updateControlButton();
}
});
}
示例4: onPrepared
import android.media.MediaPlayer; //导入方法依赖的package包/类
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
isPrepared = true;
videoDuration = mediaPlayer.getDuration();
seekBarDuration.setProgress(0);
seekBarDuration.setMax(videoDuration);
tvPosition.setText(VidstaUtil.getTimeString(0, false));
tvDuration.setText(VidstaUtil.getTimeString(videoDuration, true));
proViewVideoLoading.stop();
proViewVideoLoading.setVisibility(INVISIBLE);
removeView(videoLoadingView);
videoPlayer.setOnVideoSizeChangedListener(this);
if (initialVideoWidth == null && initialVideoHeight == null) {
initialVideoWidth = videoPlayer.getVideoWidth();
initialVideoHeight = videoPlayer.getVideoHeight();
}
if (autoPlay) {
start();
} else {
controlPlayPause.setVisibility(VISIBLE);
controlSeekBar.setVisibility(VISIBLE);
start();
pause();
}
}
示例5: mediaDuration
import android.media.MediaPlayer; //导入方法依赖的package包/类
public static int mediaDuration(Context context,String path){
MediaPlayer mp = MediaPlayer.create(context, Uri.parse(path));
if(mp==null){
return 0;
}
int duration = mp.getDuration();
mp.release();
return duration;
}
示例6: initMediaPlayer
import android.media.MediaPlayer; //导入方法依赖的package包/类
private void initMediaPlayer() {
mPlayer = new MediaPlayer();
try {
String audioFilePath = new File(audioAttachmentDir, "/" + mCurrent.getAudioFilename()).getAbsolutePath();
mPlayer.setDataSource(audioFilePath);
mPlayer.prepare();
mPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
mPlayPause.setImageResource(R.drawable.icon_play);
mSeekBar.setProgress(0);
mElapsed.setText("00:00");
mRemaining.setText(mRemainingTimeStr);
}
});
//Get audio duration
mDurationTime = mPlayer.getDuration();
//Set remaining time string
int remainingSecs = (int) (mDurationTime / 1000);
int remainingMins = remainingSecs / 60;
remainingSecs = remainingSecs % 60;
mRemainingTimeStr = String.format(Locale.getDefault(), "%02d", remainingMins) + ":" + String.format(Locale.getDefault(), "%02d", remainingSecs);
} catch (IOException e) {
Log.e(TAG, "prepare() failed");
}
}
示例7: onPrepared
import android.media.MediaPlayer; //导入方法依赖的package包/类
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
mediaPlayer.start();
isPlaying = true;
this.duration = mediaPlayer.getDuration();
this.currentTime = mediaPlayer.getCurrentPosition();
updateTimeAudio();
if (jcPlayerServiceListeners != null) {
for (JcPlayerView.JcPlayerViewServiceListener jcPlayerServiceListener : jcPlayerServiceListeners) {
jcPlayerServiceListener.updateTitle(currentJcAudio.getTitle());
jcPlayerServiceListener.onPreparedAudio(currentJcAudio.getTitle(), mediaPlayer.getDuration());
}
}
if (notificationListener != null) {
notificationListener.updateTitle(currentJcAudio.getTitle());
notificationListener.onPreparedAudio(currentJcAudio.getTitle(), mediaPlayer.getDuration());
}
if (jcPlayerStatusListeners != null) {
for (JcPlayerView.JcPlayerViewStatusListener jcPlayerViewStatusListener : jcPlayerStatusListeners) {
jcStatus.setJcAudio(currentJcAudio);
jcStatus.setPlayState(JcStatus.PlayState.PLAY);
jcStatus.setDuration(duration);
jcStatus.setCurrentPosition(currentTime);
jcPlayerViewStatusListener.onPreparedAudioStatus(jcStatus);
}
}
}
示例8: startPlay
import android.media.MediaPlayer; //导入方法依赖的package包/类
public void startPlay(String audioPath, TextView timeView) {
if (!mIsPlaying) {
if (!StringUtils.isEmpty(audioPath)) {
mPlayer = new MediaPlayer();
try {
mPlayer.setDataSource(audioPath);
mPlayer.prepare();
if (timeView != null) {
int len = (mPlayer.getDuration() + 500) / 1000;
timeView.setText(len + "s");
}
mPlayer.start();
if (listener != null) {
listener.starPlay();
}
mIsPlaying = true;
mPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
stopPlay();
}
});
} catch (Exception e) {
e.printStackTrace();
}
} else {
AppContext.showToastShort(R.string.record_sound_not_found);
}
} else {
stopPlay();
} // end playing
}
示例9: onPrepared
import android.media.MediaPlayer; //导入方法依赖的package包/类
private void onPrepared(MediaPlayer mp, int position) {
if(position > 0 && position < mp.getDuration()) {
mp.seekTo(position);
}
setStateInternal(STATE_PLAYING);
mp.start();
}
示例10: onPrepared
import android.media.MediaPlayer; //导入方法依赖的package包/类
@Override
public void onPrepared(MediaPlayer mp) {
mMediaPlayerValid = true;
mVideoDuration = mp.getDuration();
WritableMap naturalSize = Arguments.createMap();
naturalSize.putInt(EVENT_PROP_WIDTH, mp.getVideoWidth()*2);
naturalSize.putInt(EVENT_PROP_HEIGHT, mp.getVideoHeight()*2);
// if (mp.getVideoWidth() > mp.getVideoHeight())
// naturalSize.putString(EVENT_PROP_ORIENTATION, "landscape");
// else
// naturalSize.putString(EVENT_PROP_ORIENTATION, "portrait");
naturalSize.putString(EVENT_PROP_ORIENTATION, "landscape");
WritableMap event = Arguments.createMap();
event.putDouble(EVENT_PROP_DURATION, mVideoDuration / 1000.0);
event.putDouble(EVENT_PROP_CURRENT_TIME, mp.getCurrentPosition() / 1000.0);
event.putMap(EVENT_PROP_NATURALSIZE, naturalSize);
// TODO: Actually check if you can.
event.putBoolean(EVENT_PROP_FAST_FORWARD, true);
event.putBoolean(EVENT_PROP_SLOW_FORWARD, true);
event.putBoolean(EVENT_PROP_SLOW_REVERSE, true);
event.putBoolean(EVENT_PROP_REVERSE, true);
event.putBoolean(EVENT_PROP_FAST_FORWARD, true);
event.putBoolean(EVENT_PROP_STEP_BACKWARD, true);
event.putBoolean(EVENT_PROP_STEP_FORWARD, true);
mEventEmitter.receiveEvent(getId(), Events.EVENT_LOAD.toString(), event);
applyModifiers();
if (mUseNativeControls) {
initializeMediaControllerIfNeeded();
mediaController.setMediaPlayer(this);
mediaController.setAnchorView(this);
videoControlHandler.post(new Runnable() {
@Override
public void run() {
mediaController.setEnabled(true);
mediaController.show();
}
});
}
}
示例11: voicePlay
import android.media.MediaPlayer; //导入方法依赖的package包/类
public static void voicePlay(final MediaPlayer player, String path, final SeekBar seekBar, final Chronometer chronometer) {
if (player != null) {
setChronometer(chronometer, Constant.CHRONOMETER_START);
if (!isPlaying) {
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
player.setDataSource(path);
player.prepare();
} catch (IOException e) {
e.printStackTrace();
}
isPlaying = true;
}
player.start();
//设置Bar的最大值
int max = player.getDuration();
seekBar.setMax(max);
//定时器更新进度条
final Timer timer = new Timer();
TimerTask timeTask = new TimerTask() {
@Override
public void run() {
// TODO Auto-generated method stub
int a = player.getCurrentPosition();
seekBar.setProgress(player.getCurrentPosition());
}
};
timer.schedule(timeTask, 0, 500);
/*player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
// TODO Auto-generated method stub
player.start();
seekBar.setMax(player.getDuration());
seekBar.setEnabled(true);
}
});*/
//音频播放完之后重新设置显示
player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
seekBar.setProgress(0);
setChronometer(chronometer, Constant.CHRONOMETER_RESET);
timer.cancel();
}
});
}
}