混淆螺纹生活和AudioTrack android中的java [英] Confusion about Thread life and AudioTrack in android java

查看:216
本文介绍了混淆螺纹生活和AudioTrack android中的java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下功能:

void playSound(){
    final AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
            sampleRate, AudioFormat.CHANNEL_OUT_MONO,
            AudioFormat.ENCODING_PCM_16BIT, generatedSnd.length,
            AudioTrack.MODE_STATIC);
    audioTrack.write(generatedSnd, 0, generatedSnd.length);
    audioTrack.play();
}

按钮点击:

public void btnTx_click(View view){
    textView.setText("CLICKED1");

    int rem;

    while(dataToSend != 0){
        rem = dataToSend % 2;
        if(rem==0)
            genTone(0.0);
        else
            genTone(1.0);
        dataToSend /= 2; 
        counter++;
    }

    // Use a new tread as this can take a while
    final Thread thread = new Thread(new Runnable() {
        public void run() {
            // genTone();       // this line is left here for my recor donly
            handler.post(new Runnable() {

                public void run() {
                    playSound();
                }
            });
        }
    });
    thread.start();
}  

btnTx_click()函数prepares的 generatedSnd的,而循环使用数组playSound()功能。当一个按钮pressed,这个程序加载音频采样到 generatedSnd 数组,然后播放它们使用AudioTrack。加载和播放在一个线程中完成的。

The while loop in the btnTx_click() function prepares the generatedSnd array used in the playSound() function. When a button is pressed, this program loads audio samples into the generatedSnd array and then plays them using an AudioTrack. The loading and playing are done in a thread.

我怎么知道这个线头?是否结束AudioTrack的播放结束后,?或者它公共无效的run()退出后立即结束?是否有可能来检查什么时候的线头,如果是,凡在MainActivity.java应该这样的检查是否包括在内?

How do I know when this thread ends? Does it end after the playing of AudioTrack ends? Or does it end immediately after public void run() exits? Is it possible to check exactly when the thread ends, and if yes, where in MainActivity.java should such a check be included?

我想要做的是,一旦AudioTrack完成 generatedSnd 的演奏,我想要加载在 generatedSnd ,并开始播放这个新的基调。实际上,我要玩音1,其次是音2,其次是音3,...等等。

What I want to do is, once the AudioTrack completes playing of generatedSnd, I want to load a new set of values in generatedSnd and start playing this new tone. In effect, I want to play Tone 1, followed by Tone 2, followed by Tone 3,.... and so on.

推荐答案

要audioTrack.play()的调​​用不会阻塞。它会开始播放和线程的run()方法将返回,可能之前播放结束。音频的实际播放在内部创建另一个线程正在发生的事情,不是那个它从调用。

The call to audioTrack.play() does not block. It will begin playback and the run() method of the Thread will return, likely before playback is over. The actual playback of audio is happening in yet another thread internally created, not the one it was called from.

您有 AudioTrack.setPlaybackPositionUpdateListener()可接收回调,当播放到与设置一个点AudioTrack.setNotificationMarkerPosition()这将解决您的问题。

You have AudioTrack.setPlaybackPositionUpdateListener() available to receive a callback when playback reaches a point set with AudioTrack.setNotificationMarkerPosition() which will solve your problem.

这篇关于混淆螺纹生活和AudioTrack android中的java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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