我在我的应用程序中播放不同大小的音频文件,在播放音频时,进度条应该顺利进行,但进度条采用随机大小步骤。
我使用以下代码:
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
更多推荐
发布评论