录制音频文件时进度条没有显示任何进度。它适用于 2.3.3 版本和 3.0 版本,当我检查 4.0 版本时它没有显示任何进度。当我单击停止按钮时,它正在关闭应用程序。我的应用程序中的最小目标版本为 10 ,最大目标版本为 16。这是我尝试过的xml代码。
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/background">
<shape>
<corners android:radius="5dip" />
<gradient
android:startColor="#ff9d9e9d"
android:centerColor="#ff5a5d5a"
android:centerY="0.75"
android:endColor="#ff747674"
android:angle="270"
/>
</shape>
</item>
<item android:id="@android:id/secondaryProgress">
<clip>
<shape>
<corners android:radius="5dip" />
<gradient
android:startColor="#80ffd300"
android:centerColor="#80ffb600"
android:centerY="0.75"
android:endColor="#a0ffcb00"
android:angle="270"
/>
</shape>
</clip>
</item>
<item android:id="@android:id/progress">
<clip>
<shape>
<corners android:radius="5dip" />
<gradient
android:startColor="#ffffd300"
android:centerColor="#ffffb600"
android:centerY="0.75"
android:endColor="#ffffcb00"
android:angle="270"
/>
</shape>
</clip>
</item>
</layer-list>
我的录音活动如下
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.recording);
record = (Button) this.findViewById(R.id.record);
time = (Chronometer) this.findViewById(R.id.chronometer1);
OUTPUT_FILE = Environment.getExternalStorageDirectory()+"/recorder.mp3";
recordSeeker = (ProgressBar) this.findViewById(R.id.progressBar1);
record.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(record.getText().equals("Record")){
record.setText("Stop");
try {
start();
}
catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else{
record.setText("Record");
stop();
}
}
private void start() throws IllegalStateException, IOException {
ditchRecorder();
time.setBase(SystemClock.elapsedRealtime());
time.start();
File f = new File(OUTPUT_FILE);
if(f.exists())
f.delete();
recordSeeker.setProgress(0);
recordSeeker.setMax(100);
mediaRecorder = new MediaRecorder();
recorder = new AudioRecorder("/path");
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mediaRecorder.setOutputFile(OUTPUT_FILE);
mediaRecorder.prepare();
mediaRecorder.start();
task = new BackgroundAsyncTask(activity, record, myProgress,
recordSeeker);
task.execute();
}
private void ditchRecorder() {
if(mediaRecorder != null)
mediaRecorder.release();
}
private void stop() {
if(mediaRecorder!= null){
time.stop();
mediaRecorder.stop();
}
}
}
);
}
BackgroundAsync 任务类如下:
public class BackgroundAsyncTask extends AsyncTask<Void, Integer, Void> {
Activity activity;
Button playing;
int myProgress;
ProgressBar progressBar;
String reviewdetails;
BackgroundAsyncTask(Activity act, Button playing, int myProgress,
ProgressBar progressBar) {
activity = act;
this.playing = playing;
this.myProgress = myProgress;
this.progressBar = progressBar;
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
Toast.makeText(activity, "onPostExecute", Toast.LENGTH_LONG).show();
playing.setText("Stop");
Toast.makeText(activity, "Completed asynk", Toast.LENGTH_SHORT).show();
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
// Toast.makeText(activity, "onPreExecute", Toast.LENGTH_LONG).show();
myProgress = 0;
}
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
while (myProgress < 1800) {
myProgress++;
publishProgress(myProgress);
SystemClock.sleep(1000);
if (isCancelled())
break;
}
return null;
}
@Override
protected void onProgressUpdate(Integer... values) {
// TODO Auto-generated method stub
progressBar.setProgress(values[0]);
}