我使用以下代码播放录制的音频文件,效果很好。我有一个显示游戏进度的搜索栏。我有两个名为 start 和 end 的静态文本。我需要在开始文本中显示播放的持续时间(00.00 到最大持续时间),并将时间从最大持续时间减少到 00.00。我需要显示进度时间,剩余时间......任何建议..
私人无效playAudio(){
// TODO 自动生成的方法存根
start.setVisibility(View.VISIBLE);
end.setVisibility(View.VISIBLE);
上下文上下文 = getApplicationContext();
CharSequence text1 = "您的音频将播放";
int 持续时间 = 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);
}