其他分享
首页 > 其他分享> > android – 无法检索用于write()的AudioTrack指针

android – 无法检索用于write()的AudioTrack指针

作者:互联网

我正在尝试实现AudioTrack来检索我的Android设备中的音频来接收来自IAX的来电,但是在一段时间后面临异常.

private void writeBuff(short[] buf) {

    try {

        if (this.track == null) {
            Log.w("IAX2Audio", "write() without an AudioTrack");
            return;
        }

        int written = 0;
        while (written < buf.length) {


            if (this.track != null) {
                int res;

                res = this.track.write(buf, written, buf.length - written);      
                switch (res) {
                case AudioTrack.ERROR_INVALID_OPERATION:
                    Log.e("IAX2Audio", "Invalid write()");
                    return;
                case AudioTrack.ERROR_BAD_VALUE:
                    Log.e("IAX2Audio", "Bad arguments to write()");
                    return;
                }

                written += res;
            }

        }

    } catch (Exception e) {
        e.printStackTrace();
    }

}

例外就在这里

 04-27 18:26:15.865: W/System.err(12681): java.lang.IllegalStateException: Unable to retrieve AudioTrack pointer for write()
04-27 18:26:15.890: W/System.err(12681):    at android.media.AudioTrack.native_write_short(Native Method)
04-27 18:26:15.895: W/System.err(12681):    at android.media.AudioTrack.write(AudioTrack.java:1023)
04-27 18:26:15.900: W/System.err(12681):    at org.androvoip.iax2.AndroidAudioInterface.writeBuff(AndroidAudioInterface.java:322)
04-27 18:26:15.900: W/System.err(12681):    at org.androvoip.iax2.AndroidAudioInterface.playbackTime(AndroidAudioInterface.java:350)
04-27 18:26:15.905: W/System.err(12681):    at org.androvoip.iax2.AndroidAudioInterface.playTick(AndroidAudioInterface.java:413)
04-27 18:26:15.905: W/System.err(12681):    at org.androvoip.iax2.AndroidAudioInterface.access$2(AndroidAudioInterface.java:406)
04-27 18:26:15.910: W/System.err(12681):    at org.androvoip.iax2.AndroidAudioInterface$3.run(AndroidAudioInterface.java:560)
04-27 18:26:15.910: W/System.err(12681):    at java.lang.Thread.run(Thread.java:856)

这是接收正确音频一段时间后的行创建异常,就像我试图再次启动它所以有异常res = this.track.write(buf,write,buf.length – written);

这就是我如何停止并释放我的音轨

public void stopPlay() {
    Log.d("IAX2Audio", "stopPlay()");

    if (this.track == null)
        return;

    if (this.track != null) {
        //this.track.stop();
        //this.track.release();

        if (this.track != null
                && this.track.getState() != AudioTrack.STATE_UNINITIALIZED) {
            if (track.getPlayState() != AudioTrack.PLAYSTATE_STOPPED) {
                track.stop();
                track.release();
            }
        }
    }

    try {
        if (this.playThread != null) {
            final Thread t = this.playThread;
            /* Setting this to null is the signal to the thread to exit. */
            this.playThread = null;
            t.join();
        }
    } catch (final InterruptedException e) {
        e.printStackTrace();
    }

}

请找到AndroidAudioInterface.java的更新固定解决方案. AndroidAudioInterface.java

解决方法:

这是一个简单的修复,但可能是Android问题,请参阅下面的修复程序

if (track != null && track.getState() != AudioTrack.STATE_UNINITIALIZED) {
    if (track.getPlayState() != AudioTrack.PLAYSTATE_STOPPED) {

        try{
            track.stop();
        }catch (IllegalStateException e)
        {
            e.printStackTrace();
        }

    }

    track.release();     
    am.setMode(AudioManager.MODE_NORMAL); 

    //track.release();
    //track = null;
}

和OnCreate

private AudioManager am;

am = (AudioManager)this.context.getSystemService(Context.AUDIO_SERVICE); 
am.setMode(AudioManager.MODE_NORMAL); 
setVolumeControlStream(AudioManager.STREAM_VOICE_CALL);

并且始终在Thread中使用AudioTrack,因为主线程也将处理您的应用程序

标签:android,audio-recording,android-audiomanager,audiotrack,android-audiorecord
来源: https://codeday.me/bug/20190624/1279028.html