正如Hakan所说,您需要在UI线程中执行所有UI更新。既然你已经有了Handler
,你只需要重写handleMessage()
方法并在那里执行你的UI更新。你的代码会变成类似于:
private static final int PROGRESS_UPDATE = 0;
...
mProgress = (ProgressBar) findViewById(R.id.progressBar1);
// final long trackDuration = (musicLength/44100)*1000; // track duration in millisec
// Start lengthy operation in a background thread
new Thread(new Runnable() {
public void run() {
//Log.i("music length = ", trackDuration+"");
while (mProgressStatus < musicLength) {
mProgressStatus = audioTrack.write(music, 0, musicLength);
// Update the progress bar
Message msg = Message.obtain();
msg.what = PROGRESS_UPDATE;
msg.arg1 = mProgressStatus;
mHandler.sendMessage(msg);
}
}
}).start();
...
Handler mHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
switch(msg.what){
case PROGRESS_UPDATE:
mProgress.setProgress(msg.arg1);
break;
default:
Log.e("MyTag","Unsupported message type");
break;
}
}
}
好的,谢谢你的dave.c.我已经从蒸汽模式转换为静态模式。在跟踪赛道上的赛道之前,我还将数据写入赛道。我在可处理进度条更新的runnable中调用play。应用程序将播放音频样本,但进度条仍不会移动。我将发布下面显示修正的代码。为什么进度条没有移动的任何想法?
// Get the length of the audio stored in the file (16 bit so 2 bytes per short)
// and create a short array to store the recorded audio.
musicLength = (int)(file.length()/2);
final short[] music = new short[musicLength];
Log.i("filename length", file.length()+""+file.getName());
try {
// Create a DataInputStream to read the audio data back from the saved file.
InputStream is = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(is);
DataInputStream dis = new DataInputStream(bis);
// Read the file into the music array.
int i = 0;
while (dis.available() > 0) {
//music[musicLength-1-i] = dis.readShort();
music[i] = dis.readShort();
i++;
}
Log.i("buffer with "+ file.getName(), music.length+" passed in from array");
// Close the input streams.
dis.close();
// Create a new AudioTrack object using the same parameters as the AudioRecord
// object used to create the file.
audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
44100,
AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT,
musicLength,
AudioTrack.MODE_STATIC);
Log.i("audio instance = ", ""+audioTrack.getState());
// Start playback
//audioTrack.play();
mProgress = (ProgressBar) findViewById(R.id.progressBar1);
// final long trackDuration = (musicLength/44100)*1000; // track duration in millisec
// Start lengthy operation in a background thread
new Thread(new Runnable() {
public void run() {
//Log.i("music length = ", trackDuration+"");
while (mProgressStatus < musicLength) {
mProgressStatus = audioTrack.write(music, 0, musicLength);
// Update the progress bar
mHandler.post(new Runnable() {
public void run() {
audioTrack.play();
mProgress.setProgress(mProgressStatus);
}
});
Message msg = Message.obtain();
msg.what = PROGRESS;
msg.arg1 = mProgressStatus;
mHandler.sendMessage(msg);
}
}
}).start();
mHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
switch(msg.what){
case PROGRESS:
mProgress.setProgress(msg.arg1);
break;
default:
Log.e("MyTag","Unsupported message type");
break;
}
}
};
} catch (Exception e) {
e.printStackTrace();
Log.e("AudioTrack","Playback Failed");
}
}
@ dave.c嗨,我的Handler是我的UI线程中的一个私有字段,但它仍然无法工作。不应该mhandler.sendMessage和mHandler.post(新的Runnable()做同样的事情,(通过处理程序更新ui线程)?我发誓我已经阅读过,进度条没有任何工作良好的流如audiotrack类我使用pcm文件播放任何想法,谢谢 – turtleboy 2011-02-27 07:39:48
@turtleboy我认为你是对的从快速谷歌似乎'AudioTrack#write()'阻塞,而它的缓冲区中有数据,因此你不会去在写入'write'之后发现任何代码 – 2011-02-27 12:44:23
@ dave.c好的,你有什么建议可以看看吗 – turtleboy 2011-02-27 18:02:12