2012-01-05 825 views
1

我需要創建一個類來處理音頻AudioTrack。AudioTrack類與入隊,播放,停止,暫停,恢復

在這個類中必須有方法使排斥音符排列並同步播放它們。

任何人都可以幫助我嗎?

我不知道同步出隊並播放音頻的每個部分。

我需要使用它,因爲數據傳遞給字節[]中的audiotrack不在文件中。所以我不能使用MediaPlayer。

回答

1

您可以使用下面的代碼...

bufferSize = AudioTrack.getMinBufferSize(sampleRateInHz, 
       AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT); 

     if (bufferSize != AudioTrack.ERROR_BAD_VALUE && bufferSize != AudioTrack.ERROR) { 
      audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, 
        this.sampleRateInHz, AudioFormat.CHANNEL_OUT_MONO, 
        AudioFormat.ENCODING_PCM_16BIT, this.bufferSize, 
        AudioTrack.MODE_STREAM); 
      if(audioTrack!=null && audioTrack.getState() == AudioTrack.STATE_INITIALIZED){ 
       Log.i(LOG_TAG,"Audio Track instance created buffer Size : "+this.bufferSize); 

       audioTrack.setPositionNotificationPeriod(320); 
       audioTrack.setPlaybackPositionUpdateListener(this); 
       audioTrack.play(); 
       short[] tempBuf =new short[bufferSize/2]; 

          // now write the code here to fill the tempBuf by reading from the file in shorts or bytes 
       audioTrack.write(tempBuf,0, tempBuf.length); 



      }else{ 
       Log.e(LOG_TAG,"Unble to create AudioTrack instance"); 
      } 
     } else { 
      Log.e(LOG_TAG, "Unable to get the minimum buffer size"); 
     }