Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/181.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java E/AudioRecord:AudioFlinger无法创建录制曲目,状态:-1_Java_Android_Sip_Voip - Fatal编程技术网

Java E/AudioRecord:AudioFlinger无法创建录制曲目,状态:-1

Java E/AudioRecord:AudioFlinger无法创建录制曲目,状态:-1,java,android,sip,voip,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 pu

我的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非常陌生。我从谷歌的源代码中得到了这个代码


我相信这个错误是因为使用了硬件(音频)。然而,我已经在谷歌上搜索了将近一周,没有给出结果。有人能帮我吗?

我也有同样的问题,但当我在build.gradel中将targetSdkVersion更改为12时,问题就解决了


和回答帮助我解决了这个问题。

当您想要录制或调用应用程序时,必须选择缓冲区大小。例如:

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);
                }
int bufferSize=AudioRecord.getMinBufferSize(速率、信道配置、音频格式);
如果(bufferSize>0&&bufferSize 256&&bufferSize 512&&bufferSize 1024&&bufferSize 2048&&bufferSize 4096&&bufferSize 8192&&bufferSize