本文介绍了无法检索write()的AudioTrack指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现AudioTrack,以在我的android设备中检索来自IAX的来电的音频,但过一会儿就会遇到异常。

I am trying to implement AudioTrack to retrieve audio in my android device for incoming call from IAX but facing exception after some while .

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,写入,buf.length-写入) ;

This is line create exception after somewhile of receiving proper audio, as when I am trying to initiate it again so having exception res = this.track.write(buf, written, buf.length - written);

这就是我停止和发布曲目的方式

And this is how I am stoping and releasing my track

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。

Please find Updated Fixed solution for AndroidAudioInterface.java . AndroidAudioInterface.java

推荐答案

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

It is a simple fix but might be an Android issue, please refer to my fix below

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

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

And always use AudioTrack in Thread as main thread will also hand your application

这篇关于无法检索write()的AudioTrack指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-18 23:29