gpt4 book ai didi

android - 如何在录制时获取视频时长-Android

转载 作者:行者123 更新时间:2023-11-30 02:51:39 27 4
gpt4 key购买 nike

我遇到了一个问题。我想在录制视频时获取视频的持续时间。当它通过代码完成时,我可以得到视频的持续时间

MediaPlayer mp = MediaPlayer.create(mContext, Uri.parse(video));
if(mp == null)
return -1;
int duration = mp.getDuration();
mp.release();

但我想每次录制视频时都获取持续时间以更新进度条。希望得到您的回复!

最佳答案

   private class PlaybackObserver extends Thread {

public void run() {
currentPosition = 0;
try {
while (!killObserverThread) {
Thread.sleep(1000);

currentPosition = (int) mediaPlayer.getCurrentPosition();

runOnUiThread(new Runnable() {
public void run() {

yourProgressBar.setProgress(currentPosition);


}
});
}
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

@Override
protected void onDestroy() {

super.onDestroy();

killObserverThread = false;
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();



if (task == null || !task.isAlive()) {
task = new PlaybackObserver();
task.start();
}

startPlaying();

}

为您的 UiThread 添加一个私有(private)类来更新您的搜索栏/进度条。然后从onResume()开始

关于android - 如何在录制时获取视频时长-Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24055117/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com