【问题标题】:Android AudioTrack clicks at start and end of soundAndroid AudioTrack 在声音开始和结束时点击
【发布时间】:2011-11-20 18:29:19
【问题描述】:

我在播放声音(来自 SD 卡的 wav)的开始和结束时都会收到点击。它必须与轨道缓冲有关,但我不知道解决方案。另外,每次播放声音时我都会创建一个新的,这样可以吗还是有更好的方法?有许多声音一遍又一遍地播放。代码如下:

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

非常感谢您的帮助

【问题讨论】:

  • 我的音频体验不在 Android 上,但您在向其写入任何字节之前调用 track.play() 似乎很奇怪。你不应该先写字节吗?
  • 不,您需要使用 play() 打开音轨,然后写入它,因为它的 mode_streaming。我认为它可能没有正确读取 wav 标题或其他内容。
  • wtf 你认为你在做 ChrisWue 吗?编辑随机帖子以获得徽章对任何人都没有帮助,是吗?您至少可以尝试回答...

标签: android audio wav


【解决方案1】:

难道你也可以播放 PCM 波形文件头吗?

每个 PCM 波形文件在文件的开头都有一个小标题,如果你播放它,你会播放标题字节,这可能会导致在开始时点击。

【讨论】:

  • 确实,这 44 个字节的 WAVE-header 在播放时听起来像是点击。解决方法是在 AT 开始播放此类文件时跳过 44 个字节。
【解决方案2】:

我使用 AudioTrack 在每首曲目的开头都有这些相同的点击。我通过关闭轨道音量,等待半秒钟,然后恢复正常音量来解决它。我不再有任何点击。这是代码的相关部分。

    at.play();
    at.setStereoVolume (0.0f, 0.0f);

    new Thread (new Runnable(){
        public void run() {
            try{
                Thread.sleep(500);
            } catch (InterruptedException ie) { ; }
            at.setStereoVolume (1.0f, 1.0f);
        }
    }).start();

    new Thread (new Runnable(){
        public void run() {
            int i = 0;
            try{
                buffer = new byte[512];
                while(((i = is.read(buffer)) != -1) && !paused){
                    at.write(buffer, 0, i);
                    position += i;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (!paused){
                parent.trackEnded();
            }
        }
    }).start();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-24
    • 1970-01-01
    • 2012-08-29
    • 1970-01-01
    • 2021-08-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多