AudioTrack流模式MODE_STREAMING [英] AudioTrack in streaming mode MODE_STREAMING

查看:146
本文介绍了AudioTrack流模式MODE_STREAMING的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要流在运行时产生的PCM数据。所以我有一个线程一个循环

I need to stream PCM data generated at runtime. So I have a thread with a loop

public void run() {
  while(...) {
    mAudioTrack.write(getPCM(), ...);
  }
}

不幸的是,这并不正常工作。现在看来,这不依赖于AudioTrack缓冲区大小。我想这是非常小的,以模拟类的低延迟性能(150毫秒),使用户可以dinamically改变由getPCM选择了PCM()

Unfortunately this doesn't work. It seems it doesn't depend on AudioTrack buffer size. I want it to be very small to simulate sort of low latency behaviour (150 ms) so the user can dinamically change the PCM picked by getPCM()

int bufferSize = 0.150 * sampleRate * channels * bitsPerSample / 8;

不过,我想增加缓冲区的大小可达10万没有结果

However, I tried to increase the buffer size up to 100k with no result

推荐答案

下面这个简单的例子对我的作品:

Here is short example that works for me:

public class Internal extends Activity
{   
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);              
    }

    public void onPlayClicked(View v)
    {
        start();    
    }

    public void onStopClicked(View v)
    {
        stop();
    }

    boolean m_stop = false;
    AudioTrack m_audioTrack;
    Thread m_noiseThread;

    Runnable m_noiseGenerator = new Runnable()
    {       
        public void run()
        {
            Thread.currentThread().setPriority(Thread.MIN_PRIORITY);

            /* 8000 bytes per second, 1000 bytes = 125 ms */
            byte [] noiseData = new byte[1000];
            Random rnd = new Random();

            while(!m_stop)
            {           
                rnd.nextBytes(noiseData);   
                m_audioTrack.write(noiseData, 0, noiseData.length);                 
            }
        }
    };

    void start()
    {
        m_stop = false;

        /* 8000 bytes per second*/
        m_audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, 8000, AudioFormat.CHANNEL_OUT_MONO,
                                        AudioFormat.ENCODING_PCM_8BIT, 8000 /* 1 second buffer */,
                                        AudioTrack.MODE_STREAM);            

        m_audioTrack.play();        


        m_noiseThread = new Thread(m_noiseGenerator);
        m_noiseThread.start();
    }

    void stop()
    {
        m_stop = true;          
        m_audioTrack.stop();
    }   
}

这篇关于AudioTrack流模式MODE_STREAMING的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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