我使用以下代码播放录制的音频文件,它工作正常。我有一个搜索栏,显示了该剧的进展。我有两个名为start和end的静态文本。我需要在开始文本中显示播放的持续时间(00.00到最大持续时间),并将时间从最大持续时间减少到00.00。我需要显示进度时间,剩余时间......任何建议......
private void playAudio() {
// TODO Auto-generated method stub
start.setVisibility(View.VISIBLE);
end.setVisibility(View.VISIBLE);
Context context = getApplicationContext();
CharSequence text1 = "Your audio will play";
int duration = Toast.LENGTH_SHORT;
Toast toast1 = Toast.makeText(context, text1, duration);
toast1.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast1.show();
String newFolderName = "/ComAudioRecorder";
String extstoredir = Environment.getExternalStorageDirectory()
.toString();
String filename = "/combinedrecaudio.wav";
String path1 = extstoredir + newFolderName + filename;
Log.d("path1", path1);
/*
* String path=path2; Log.d("path",path);
*/
mPlayer = new MediaPlayer();
try {
mPlayer.setDataSource(path1);
mPlayer.prepare();
mPlayer.start();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mSeekBar.setMax(mPlayer.getDuration());
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
while (mPlayer != null
&& mPlayer.getCurrentPosition() < mPlayer.getDuration()) {
Log.d("Indide Run Method",Integer.toString(mPlayer.getCurrentPosition()));
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mSeekBar.setProgress(mPlayer.getCurrentPosition());
start.setText(Long.toString(startTime));
//end.setText(Double.toString());
}
}
}).start();
pos=mPlayer.getCurrentPosition();
mSeekBar.setProgress(mPlayer.getCurrentPosition());
// mPlay.setVisibility(View.INVISIBLE);
pauseRecord.setVisibility(View.INVISIBLE);
resumeRec.setVisibility(View.VISIBLE);
/*start.setText(Double.toString(starttext));
starttext=starttext+00.01;*/
// start.setText((int) (mPlayer.getCurrentPosition()/100000.0));
// end.setText(currentTime);
// start.setText();
// mDelete.setVisibility(View.INVISIBLE);
}
答案 0 :(得分:4)
你无法更新线程中的UI。你可以将它们发布到Runnable。
在run方法()中输入以下代码。
mHandler.post(updateUI);
然后使用handler更新ui
private final Runnable updateUI= new Runnable()
{
public void run()
{
try
{
//update ur ui here
start.setText((mPlayer.getCurrentPosition()/mPlayer.getDuration())*100); end.setText(mPlayer.getDuration()-mPlayer.getCurrentPosition())
}
catch (Exception e)
{
e.printStackTrace();
}
}
};
private final Handler mHandler = new Handler();
答案 1 :(得分:3)
每1秒您可以使用公式(mPlayer.getCurrentPosition()/mPlayer.getDuration)*100;
同样,您可以使用 mPlayer.getDuration-mPlayer.getCurrentPosition()
;
你可以在onPreparedListener上获得音频文件的getDuration()......
获得成功让我知道。