进度条不动

时间:2011-02-26 13:36:25

标签: android progress-bar

我有一个播放/记录样本的音频应用。我在UI上放了一个进度条,但是当播放样本时,条形图不会移动或者似乎随样本增加。我怎么让它移动?

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() {
                                 mProgress.setProgress(mProgressStatus);

                             }
                         });
                     }
                 }
             }).start();

3 个答案:

答案 0 :(得分:1)

建议使用AsyncTask与线程和处理程序混合使用。它包含所需的线程和处理程序方法。 onProgressUpdate是在UI线程中调用的(在每次publishProgress调用之后),因此它可以成功更新您的进度条。

答案 1 :(得分:0)

您需要更新UI线程(主要的活动类线程)中的进度条,而不是通过其他线程。您需要在UI线程中创建一个Handler,并由其他线程向此Handler发送消息。 Android有create a ProgressDialog的例子。请务必展开底部的部分,其中显示“带有第二个线程的示例ProgressDialog”,因为这解释了我对该示例的回答。

答案 2 :(得分:0)

正如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");
        }

    }