我正在尝试实现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();
}
}
异常(exception)在这里
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, written, 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);
并始终在线程中使用AudioTrack,因为主线程也会处理您的应用程序
关于android - 无法检索write()的AudioTrack指针,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23324943/