E / AudioRecord:AudioFlinger无法创建记录轨道,状态:-1

时间:2018-02-05 14:00:48

标签: java android sip voip

我的VoiSip应用程序出现此错误

E/AudioRecord: AudioFlinger could not create record track, status: -1
E/AudioGroup: cannot initialize audio device

我尝试拨打另一个SIP地址

后发生此错误

这里有2个java类(WalkieTalkieActivity.java& IncomingCallReceiver.java)

WalkieTalkieActivity.java

public class WalkieTalkieActivity extends Activity implements View.OnTouchListener {

    public IncomingCallReceiver callReceiver;

    public SipManager mSipManager = null;

    public SipProfile mSipProfile = null;

    public SipAudioCall call = null;

    TextView tv;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_walkie_talkie);



        ToggleButton pushToTalkButton = (ToggleButton) findViewById(R.id.pushToTalk);
        pushToTalkButton.setOnTouchListener(this);

        IntentFilter filter = new IntentFilter();
        filter.addAction("android.SipDemo.INCOMING_CALL");
        callReceiver = new IncomingCallReceiver();
        this.registerReceiver(callReceiver, filter);

        if (mSipManager == null) {
            mSipManager = SipManager.newInstance(this);
        }

        tv = (TextView)findViewById(R.id.textView);
    }

    @Override
    public void onStart() {
        super.onStart();
        // When we get back from the preference setting Activity, assume
        // settings have changed, and re-login with new auth info.
        initializeManager();
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        if (call != null) {
            call.close();
        }
        closeLocalProfile();
        if (callReceiver != null) {
            this.unregisterReceiver(callReceiver);
        }
    }

    public void closeLocalProfile() {
        if (mSipManager == null) {
            return;
        }
        try {
            if (mSipProfile != null) {
                mSipManager.close(mSipProfile.getUriString());
            }
        } catch (Exception ee) {
            Log.d("onDestroy", "Failed to close local profile.", ee);
        }
    }

    public void initializeManager() {
        if(mSipManager == null) {
            mSipManager = SipManager.newInstance(this);
        }
        initializeLocalProfile();
    }

    private void initializeLocalProfile() {
        String domain = "mydomain";
        String username = "myusername";
        String password = "mypassword";

        try {
            SipProfile.Builder builder = new SipProfile.Builder(username, domain);
            builder.setPassword(password);
            mSipProfile = builder.build();

            if (mSipProfile == null){
                Log.e("error cukimai", "null");
            }else{
                Log.e("error cukimai", "not null");
            }

            Intent i = new Intent();
            i.setAction("android.SipDemo.INCOMING_CALL");
            PendingIntent pi = PendingIntent.getBroadcast(this, 0, i, Intent.FILL_IN_DATA);
            mSipManager.open(mSipProfile, pi, null);

            mSipManager.setRegistrationListener(mSipProfile.getUriString(), new SipRegistrationListener() {
                public void onRegistering(String localProfileUri) {
                    updateStatus("Registering with SIP Server...");
                    Log.e("process","Registering with SIP Server...");
                }

                public void onRegistrationDone(String localProfileUri, long expiryTime) {
                    updateStatus("Ready");
                    Log.e("process","ready");
                }

                public void onRegistrationFailed(String localProfileUri, int errorCode,
                                                 String errorMessage) {
                    updateStatus("Registration failed.  Please check settings.");
                    Log.e("process","Registration failed.  Please check settings.");
                }

            });

            Log.e("process","stop");
        } catch (SipException e) {
            e.printStackTrace();
            Log.e("error cukimai", "cuk cuk");
        } catch (ParseException e) {
            e.printStackTrace();
            Log.e("error cukimai", "cuk");
        }
    }

    public void updateStatus(final String st){
        this.runOnUiThread(new Runnable() {
            public void run() {
                tv.setText(st);
            }
        });
    }

    public void updateStatus(SipAudioCall call) {
        String useName = call.getPeerProfile().getDisplayName();
        if(useName == null) {
            useName = call.getPeerProfile().getUserName();
        }
        updateStatus(useName + "@" + call.getPeerProfile().getSipDomain());
    }


    public void callAct(View view) {
        Toast.makeText(this, "about to make call", Toast.LENGTH_LONG).show();

        makeCall();
    }


    public void makeCall(){

        try {
            SipAudioCall.Listener listener = new SipAudioCall.Listener() {
                // Much of the client's interaction with the SIP Stack will
                // happen via listeners.  Even making an outgoing call, don't
                // forget to set up a listener to set things up once the call is established.
                @Override
                public void onCallEstablished(SipAudioCall call) {
                    call.startAudio();
                    call.setSpeakerMode(true);
                    call.toggleMute();
                    updateStatus(call);
                }

                @Override
                public void onCallEnded(SipAudioCall call) {
                    updateStatus("Ready.");
                }
            };

            call = mSipManager.makeAudioCall(mSipProfile.getUriString(), "sip:destination@domain", listener, 30);

            Log.e("make call", "true");
            start();

        }catch (Exception e){
            Log.i("error", "Error when trying to close manager.", e);
            if (mSipProfile != null) {
                try {
                    mSipManager.close(mSipProfile.getUriString());
                } catch (Exception ee) {
                    Log.i("error", "Error when trying to close manager.", ee);
                    ee.printStackTrace();
                }
            }
            if (call != null) {
                call.close();
            }
        }
    }


    @Override
    public boolean onTouch(View view, MotionEvent event) {
        if (call == null) {
            return false;
        } else if (event.getAction() == MotionEvent.ACTION_DOWN && call != null && call.isMuted()) {
            call.toggleMute();
        } else if (event.getAction() == MotionEvent.ACTION_UP && !call.isMuted()) {
            call.toggleMute();
        }
        return false;
    }


    final MediaRecorder recorder = new MediaRecorder();
    final String path;

    /**
     * Creates a new audio recording at the given path (relative to root of SD card).
     */
    public WalkieTalkieActivity(String path) {
        this.path = sanitizePath(path);
    }

    private String sanitizePath(String path) {
        if (!path.startsWith("/")) {
            path = "/" + path;
        }
        if (!path.contains(".")) {
            path += ".3gp";
        }
        return Environment.getExternalStorageDirectory().getAbsolutePath() + path;
    }

    /**
     * Starts a new recording.
     */
    public void start() throws IOException {
        String state = android.os.Environment.getExternalStorageState();
        if(!state.equals(android.os.Environment.MEDIA_MOUNTED))  {
            throw new IOException("SD Card is not mounted.  It is " + state + ".");
        }

        // make sure the directory we plan to store the recording in exists
        File directory = new File(path).getParentFile();
        if (!directory.exists() && !directory.mkdirs()) {
            throw new IOException("Path to file could not be created.");
        }

        recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        recorder.setOutputFile(path);
        recorder.prepare();
        recorder.start();
    }

    /**
     * Stops a recording that has been previously started.
     */
    public void stop() throws IOException {
        recorder.stop();
        recorder.release();
    }

}

IncomingCallReceiver.java

public class IncomingCallReceiver extends BroadcastReceiver {
    /**
     * Processes the incoming call, answers it, and hands it over to the
     * WalkieTalkieActivity.
     * @param context The context under which the receiver is running.
     * @param intent The intent being received.
     */
    @Override
    public void onReceive(Context context, Intent intent) {
        SipAudioCall incomingCall = null;
        try {
            SipAudioCall.Listener listener = new SipAudioCall.Listener() {
                @Override
                public void onRinging(SipAudioCall call, SipProfile caller) {
                    try {
                        call.answerCall(30);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            };
            WalkieTalkieActivity wtActivity = (WalkieTalkieActivity) context;
            incomingCall = wtActivity.mSipManager.takeAudioCall(intent, listener);
            incomingCall.answerCall(30);
            incomingCall.startAudio();
            incomingCall.setSpeakerMode(true);
            if(incomingCall.isMuted()) {
                incomingCall.toggleMute();
            }
            wtActivity.call = incomingCall;
            wtActivity.updateStatus(incomingCall);
        } catch (Exception e) {
            if (incomingCall != null) {
                incomingCall.close();
            }
        }
    }
}

我在Android Studio中的Sip和Voip实现方面真的很新。我从谷歌源代码获得此代码。

我认为由于使用硬件(音频)而发生此错误。但是我一直在google上搜索了将近一周而没有给出结果。有人能帮助我吗?

2 个答案:

答案 0 :(得分:0)

我遇到了同样的问题,但是当我在build.gradel中将targetSdkVersion更改为12时就修复了。

this的答案帮助我解决了问题。

答案 1 :(得分:0)

要在应用程序中记录或调用时,必须选择BufferSize。例如:

int bufferSize = AudioRecord.getMinBufferSize(rate, channelConfig, audioFormat);
                if (bufferSize > 0 && bufferSize <= 256){
                    bufferSize = 256;
                }else if (bufferSize > 256 && bufferSize <= 512){
                    bufferSize = 512;
                }else if (bufferSize > 512 && bufferSize <= 1024){
                    bufferSize = 1024;
                }else if (bufferSize > 1024 && bufferSize <= 2048){
                    bufferSize = 2048;
                }else if (bufferSize > 2048 && bufferSize <= 4096){
                    bufferSize = 4096;
                }else if (bufferSize > 4096 && bufferSize <= 8192){
                    bufferSize = 8192;
                }else if (bufferSize > 8192 && bufferSize <= 16384){
                    bufferSize = 16384;
                }else{
                    bufferSize = AudioRecord.getMinBufferSize(rate, channelConfig, audioFormat);
                }