Android:AudioTrack在开始时会发出咔嗒声 [英] Android: AudioTrack makes a click sound at beginning

查看:95
本文介绍了Android:AudioTrack在开始时会发出咔嗒声的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Android应用,我想播放一些短声音(〜2s).我尝试了Soundpool,但是它真的不适合我,因为它无法检查声音是否已经在播放.因此,我决定使用AudioTrack.

I'm working on an Android app and I would like to play some short sounds(~ 2s). I tried Soundpool but it doesn't really suit for me since it can't check if a sounds is already playing. So I decided to use AudioTrack.

大多数情况下,它运行良好,但是当它开始播放声音时会发出喀哒"声.我检查了我的音频文件,发现它们很干净.

It works quite good BUT most of the time, when it begins to play a sound there is a "click" sound. I checked my audiofiles and they are clean.

我在流媒体模式下使用音轨.我看到静态模式更适合于短声音,但是经过多次搜索后,我仍然不知道如何使其工作.我还读到咔嗒声可能是由wav文件的标题引起的,因此,如果我使用 setPlaybackHeadPosition(int positionInFrames)函数(应该只起作用)跳过此标题,则声音可能会消失处于静态模式)

I use audiotrack on stream mode. I saw that static mode is better for short sounds but after many searchs I still don't understand how to make it work. I also read that the clicking noise can be caused by the header of the wav file, so maybe the sound would disappear if I skip this header with setPlaybackHeadPosition(int positionInFrames) function (that is supposed to work only in static mode)

这是我的代码(所以问题是开头的滴答声)

Here is my code (so the problem is the ticking noise at the beginning)

    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); 
audioTrack.play();
            int i = 0;
            int bufferSize = 2048; //don't really know which value to put
            audioTrack.setPlaybackRate(88200);

            byte [] buffer = new byte[bufferSize];
//there we open the wav file >
            InputStream inputStream = getResources().openRawResource(R.raw.abordage);
            try {
                while((i = inputStream.read(buffer)) != -1)
                    audioTrack.write(buffer, 0, i);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            try {
                inputStream.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

有人能避免这种噪音吗?我尝试了,该方法有时有效,但并非每次都有效.有人可以告诉我如何在 MODE_STATIC 中实现音轨吗?谢谢

Does anyone has a solution to avoid that noise? I tried this, that works sometimes but not everytime. Could someone show me how to implement audiotrack in MODE_STATIC ? Thank you

推荐答案

最后,经过大量实验,我使它在没有咔嗒声的情况下也可以正常工作.这是我的代码(遗憾的是,由于 getChannel().size()方法仅适用于FileInputStream类型,因此我无法读取inputStream的大小)

Finally, after a lot of experimentation, I made it work without the click noise. Here is my code (unfortunaly, I can't read the size of the inputStream since the getChannel().size() method only works with FileInputStream type)

try{
    long totalAudioLen = 0;  
    InputStream inputStream = getResources().openRawResource(R.raw.abordage); // open the file
    totalAudioLen = inputStream.available();
    byte[] rawBytes = new byte[(int)totalAudioLen];
    AudioTrack track = new AudioTrack(AudioManager.STREAM_MUSIC,
                                      44100,
                                      AudioFormat.CHANNEL_CONFIGURATION_MONO,
                                      AudioFormat.ENCODING_PCM_16BIT,
                                     (int)totalAudioLen,
                                     AudioTrack.MODE_STATIC);
    int offset = 0;
    int numRead = 0;

    track.setPlaybackHeadPosition(100); // IMPORTANT to skip the click
    while (offset < rawBytes.length
               && (numRead=inputStream.read(rawBytes, offset, rawBytes.length-offset)) >= 0) {
            offset += numRead;
    } //don't really know why it works, it reads the file
    track.write(rawBytes, 0, (int)totalAudioLen); //write it in the buffer?
    track.play();  // launch the play
    track.setPlaybackRate(88200);
    inputStream.close();
    }
    catch (FileNotFoundException e) {

        Log.e(TAG, "Error loading audio to bytes", e);
    } catch (IOException e) {
        Log.e(TAG, "Error loading audio to bytes", e);
    } catch (IllegalArgumentException e) {
        Log.e(TAG, "Error loading audio to bytes", e);
    }

因此,跳过咔嗒声的解决方案是使用 MODE_STATIC setPlaybackHeadPosition 函数跳过音频文件的开头(可能是标题,或者我不知道)不知道是什么).我希望这部分代码可以对某人有所帮助,我花了太多时间试图找到静态模式代码示例,而没有找到加载原始资源的方法.

So the solution to skip the clicking noise is to use MODE_STATIC and setPlaybackHeadPosition function to skip the beginning of the audio file (that is probably the header or I don't know what). I hope that this part of code will help someone, I spent too many time trying to find a static mode code sample without finding a way to load a raw ressource.

在各种设备上测试了该解决方案之后,它们似乎始终具有喀嗒声.

After testing this solution on various devices, it appears that they have the clicking noise anyway.

这篇关于Android:AudioTrack在开始时会发出咔嗒声的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆