播放音频文件时平滑增加进度条(Smoothly increase progress bar while playing audio file)

编程入门 行业动态 更新时间:2024-10-24 07:29:05
播放音频文件时平滑增加进度条(Smoothly increase progress bar while playing audio file)

我在我的应用程序中播放不同大小的音频文件,在播放音频时,进度条应该顺利进行,但进度条采用随机大小步骤。

我使用以下代码:

mp = new MediaPlayer(); mp.setDataSource("audio file path"); mp.prepare(); mp.start(); pb.setMax(mp.getDuration()); new AsyncTask<Void, Integer, Void>() { @Override protected Void doInBackground(Void... params) { while(mp.isPlaying()) { publishProgress(mp.getCurrentPosition); } return null; } protected void onProgressUpdate(Integer... progress) { pb.setProgress(progress[0]); } @Override protected void onPostExecute(Void result) { super.onPostExecute(result); mp.stop(); mp.release(); } }.execute();

有没有办法顺利增加进度条?

I am playing audio file of different sizes in my application and while audio is playing the progress bar should progress smoothly but progress bar is taking random size steps.

I am using following code :

mp = new MediaPlayer(); mp.setDataSource("audio file path"); mp.prepare(); mp.start(); pb.setMax(mp.getDuration()); new AsyncTask<Void, Integer, Void>() { @Override protected Void doInBackground(Void... params) { while(mp.isPlaying()) { publishProgress(mp.getCurrentPosition); } return null; } protected void onProgressUpdate(Integer... progress) { pb.setProgress(progress[0]); } @Override protected void onPostExecute(Void result) { super.onPostExecute(result); mp.stop(); mp.release(); } }.execute();

Is there any way to smoothly increase progressbar?

最满意答案

请检查此步骤

第1步:使用MediaPlayer.getDuration()获取音频持续时间

第2步:设置ProgressBar进度最大值。 价值来自第1步

第3步:

在使用Handler播放媒体时,定期从MediaPlayer.getCurrentPosition()更新进度条。

所以代码就是这样的

MediaPlayer mMediaPlayer = new MediaPlayer(); final SeekBar mSeelBar = new SeekBar(this); final int duration = mMediaPlayer.getDuration(); final int amountToUpdate = duration / 100; Timer mTimer = new Timer(); mTimer.schedule(new TimerTask() { @Override public void run() { runOnUiThread(new Runnable() { @Override public void run() { if (!(amountToUpdate * mSeelBar.getProgress() >= duration)) { int p = mSeelBar.getProgress(); p += 1; mSeelBar.setProgress(p); } } }); }; }, amountToUpdate);

我按照此链接获取代码

Please check this steps

Step 1: Get audio duration with MediaPlayer.getDuration()

Step 2: Set ProgressBar progress max. value to value from step 1

Step 3:

Update progress bar periodically from MediaPlayer.getCurrentPosition(), while media playing using Handler.

So the code will be like this

MediaPlayer mMediaPlayer = new MediaPlayer(); final SeekBar mSeelBar = new SeekBar(this); final int duration = mMediaPlayer.getDuration(); final int amountToUpdate = duration / 100; Timer mTimer = new Timer(); mTimer.schedule(new TimerTask() { @Override public void run() { runOnUiThread(new Runnable() { @Override public void run() { if (!(amountToUpdate * mSeelBar.getProgress() >= duration)) { int p = mSeelBar.getProgress(); p += 1; mSeelBar.setProgress(p); } } }); }; }, amountToUpdate);

I followed this link for the code

更多推荐

本文发布于:2023-08-03 01:10:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1382481.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:平滑   音频文件   进度条   Smoothly   increase

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!