当前位置: 首页>>代码示例>>Java>>正文


Java MediaPlayer.getDuration方法代码示例

本文整理汇总了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();
}
 
开发者ID:icaynia,项目名称:pracler,代码行数:17,代码来源:MusicFileManager.java

示例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;
}
 
开发者ID:Samsung,项目名称:microbit,代码行数:18,代码来源:AlertPlugin.java

示例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();
    }
  });
}
 
开发者ID:wotomas,项目名称:droid-vizu,代码行数:17,代码来源:SoundPlayerView.java

示例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();
    }
}
 
开发者ID:JakeSteam,项目名称:Vidsta,代码行数:28,代码来源:VidstaPlayer.java

示例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;
}
 
开发者ID:NewCasino,项目名称:browser,代码行数:10,代码来源:HelpUtils.java

示例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");
    }
}
 
开发者ID:abicelis,项目名称:Remindy,代码行数:31,代码来源:AudioAttachmentViewHolder.java

示例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);
        }
    }
}
 
开发者ID:yusufkocak1,项目名称:TMP-Music_player,代码行数:31,代码来源:JcPlayerService.java

示例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
}
 
开发者ID:hsj-xiaokang,项目名称:OSchina_resources_android,代码行数:33,代码来源:RecordButtonUtil.java

示例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();
}
 
开发者ID:LightSun,项目名称:android-util2,代码行数:8,代码来源:MediaHelper.java

示例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();
                }
            });
        }
    }
 
开发者ID:12d,项目名称:react-native-videoplayer,代码行数:46,代码来源:ReactVideoView.java

示例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();
            }
        });
    }
}
 
开发者ID:lpy19930103,项目名称:MinimalismJotter,代码行数:55,代码来源:RecoderManager.java


注:本文中的android.media.MediaPlayer.getDuration方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。