Android的AudioTrack单击开始和声音的结束单击、声音、结束、Android

2023-09-07 00:10:33 作者:道貌岸然。

我得到的点击次数在开始和播放声音(从SD卡为WAV)结束。它必须是与轨道缓冲,但我不知道解决的办法。另外,我创造这些新的每次播放声音时,这是正确的,还是有更好的办法?有许多声音玩了个遍。这里是code:

I get clicks at the start and end of playing a sound (a wav from the sdcard). It must be something to do with the track buffering but I dont know the solution. Also, I create a new one of these every time the sound plays, is this ok or is there a better way? There are many sounds playing over and over. Here is the code:

public void PlayAudioTrack(final String filePath, final Float f) throws IOException
    {

    new Thread(new Runnable() { public void run() { 
            //play sound here
        int minSize = AudioTrack.getMinBufferSize( 44100, AudioFormat.CHANNEL_CONFIGURATION_STEREO, AudioFormat.ENCODING_PCM_16BIT );        
           AudioTrack track = new AudioTrack( AudioManager.STREAM_MUSIC, 44100, 
             AudioFormat.CHANNEL_CONFIGURATION_STEREO, AudioFormat.ENCODING_PCM_16BIT, 
             minSize, AudioTrack.MODE_STREAM);

        track.setPlaybackRate((int) (44100*f));

    if (filePath==null)
    return;

    int count = 512 * 1024; 
    //Read the file..
    byte[] byteData = null;
    File file = null;
    file = new File(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();

    while (bytesread < size) { 
    try {
        ret = in.read( byteData,0, count);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } 
    track.play(); 
    if (ret != -1) { 
    // Write the byte array to the track 
    track.write(byteData,0, ret); bytesread += ret; 
    } 
    else break; } 

    try {
        in.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } track.stop(); track.release(); 
    }

        }).start();
        }

非常感谢您的帮助

Many thanks for any help

推荐答案

这不是很可能,你也玩了PCM波形文件头?

Isn't it possible that you play the PCM wave file header too?

每个PCM波形文件都有在文件的开头一个小头,如果你玩,你玩头字节,这可能会导致点击在TE开头。

Each PCM wave file has a small header at the beginning of the file, if you play that, you play the header bytes which could result in a click at te beginning.