2011-02-26 100 views
1

我有一个播放/记录样本的音频应用程序。我已经在用户界面上放置了一个进度条,但是当播放该示例时,该栏不会移动或似乎随着示例而增加。我如何让它移动?progressbar not moving

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(); 

回答

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

    } 
+0

@ dave.c嗨,我的Handler是我的UI线程中的一个私有字段,但它仍然无法工作。不应该mhandler.sendMessage和mHandler.post(新的Runnable()做同样的事情,(通过处理程序更新ui线程)?我发誓我已经阅读过,进度条没有任何工作良好的流如audiotrack类我使用pcm文件播放任何想法,谢谢 – turtleboy 2011-02-27 07:39:48

+0

@turtleboy我认为你是对的从快速谷歌似乎'AudioTrack#write()'阻塞,而它的缓冲区中有数据,因此你不会去在写入'write'之后发现任何代码 – 2011-02-27 12:44:23

+0

@ dave.c好的,你有什么建议可以看看吗 – turtleboy 2011-02-27 18:02:12

0

你需要更新通过其他线程在UI线程进度条(这是主要的活动课线程),而不是。你需要在UI线程中创建一个Handler,并通过其他线程向这个Handler发送消息。 Android有一个例子,当他们create a ProgressDialog。请确保展开底部显示“使用第二个线程的示例ProgressDialog”的部分,因为这解释了我在示例中的答案。

1

建议使用AsyncTask与主题和处理程序混杂在一起玩。它包含需要的线程和处理程序方法。 onProgressUpdate是在UI线程中调用的(每调用一次publishProgress之后),以便它可以成功更新您的进度条。