0
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    int minBufferSize = AudioTrack.getMinBufferSize(44100, AudioFormat.CHANNEL_CONFIGURATION_MONO, 
            AudioFormat.ENCODING_PCM_16BIT);

  audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, 44100, AudioFormat.CHANNEL_CONFIGURATION_MONO, 
        AudioFormat.ENCODING_PCM_16BIT, minBufferSize, AudioTrack.MODE_STREAM); 


    playfilesound();
}


private void playfilesound() throws IOException
{




    int count = 512 * 1024; // 512 kb
    //Reading the file..
    byte[] byteData = null; 
    File file = null; 
    file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/"+"recordsound");    //filePath


    byteData = new byte[(int)count];
    FileInputStream in = null;
    try {
    in = new FileInputStream( file );

    } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }


    int bytesread = 0, ret = 0;
    int size = (int) file.length();
    audioTrack.play();



    while (bytesread < size) {    // Write the byte array to the track 
        ret = in.read( byteData,0, count);   //ret =size in bytes

        if (ret != -1) {
            audioTrack.write(byteData,0, ret);
            bytesread += ret; }  //ret
        else break; 

    }   //while



    in.close();
   audioTrack.stop(); audioTrack.release();
    }  

デバッガーを使用してコードをステップ実行し、audioTrackの上にカーソルを合わせると、割り当てられて初期化されます。ファイルも存在します。

ただし、audioTrack.play()にヒットすると、不正な状態の例外である、初期化されていないAudioTrackというエラーがスローされます。

録音ファイル部分を含むプロジェクトを同封しました。 http://www.mediafire.com/?6i2r3whg7e7rs79

4

3 に答える 3

2

あなたが使用するチャンネルの構成は廃止され、代わりに録音と再生にAudioFormat.CHANNEL_CONFIGURATION_MONO使用されます...AudioFormat.CHANNEL_IN_MONOAudioFormat.CHANNEL_OUT_MONO

于 2017-02-02T00:24:17.520 に答える
0

書く前に遊びを呼んだようです!これを試して ...

int bytesread = 0, ret = 0;
int size = (int) file.length();
//audioTrack.play();  <---- play called prematurely



while (bytesread < size) {    // Write the byte array to the track 
    ret = in.read( byteData,0, count);   //ret =size in bytes

    if (ret != -1) {
        audioTrack.write(byteData,0, ret);
        bytesread += ret; 
        audioTrack.play(); //<--- try calling it here!
    }  //ret
    else break; 

}   //while
于 2013-08-19T15:21:07.243 に答える