为什么我在 start() 或 stop() 方法上遇到异常?通话录音应用试用

问题描述

嗨,我想制作一个通话录音应用。我没有安卓编程经验。

我构建了一些类,但出现了这个错误

2021-02-10 19:25:03.474 14245-14245/com.codeforlite.myapplication E/MediaRecorder: start Failed: -2147483648
2021-02-10 19:25:03.475 14245-14245/com.codeforlite.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.codeforlite.myapplication,PID: 14245
    java.lang.RuntimeException: start Failed.
        at android.media.MediaRecorder.start(Native Method)
        at com.codeforlite.myapplication.CallReceiver$1.onCallStateChanged(CallReceiver.java:81)
        at android.telephony.PhonestateListener$IPhonestateListenerStub.lambda$onCallStateChanged$10(PhonestateListener.java:1185)
        at android.telephony.-$$Lambda$PhonestateListener$IPhonestateListenerStub$6czWSGzxct0CXPVO54T0aq05qls.run(UnkNown Source:6)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

这是我的课程: 电话呼叫接收器

import java.util.Date;

import android.content.broadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.TelephonyManager;

abstract class PhoneCallReceiver extends broadcastReceiver {

    //The receiver will be recreated whenever android feels like it.  We need a static variable to remember data between instantiations

    private static int lastState = TelephonyManager.CALL_STATE_IDLE;
    private static Date callStartTime;
    private static boolean isIncoming;
    private static String savednumber;  //because the passed incoming is only valid in ringing
    protected Context context;


    @Override
    public void onReceive(Context context,Intent intent) {

        this.context = context;
        //We listen to two intents.  The new outgoing call only tells us of an outgoing call.  We use it to get the number.
        if (intent.getAction().equals("android.intent.action.NEW_OUTGOING_CALL")) {
            savednumber = intent.getExtras().getString("android.intent.extra.PHONE_NUMBER");
        }
        else{
            String stateStr = intent.getExtras().getString(TelephonyManager.EXTRA_STATE);
            String number = intent.getExtras().getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
            int state = 0;
            if(stateStr.equals(TelephonyManager.EXTRA_STATE_IDLE)){
                state = TelephonyManager.CALL_STATE_IDLE;
            }
            else if(stateStr.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)){
                state = TelephonyManager.CALL_STATE_OFFHOOK;
            }
            else if(stateStr.equals(TelephonyManager.EXTRA_STATE_RINGING)){
                state = TelephonyManager.CALL_STATE_RINGING;
            }


            onCallStateChanged(context,state,number);
        }
    }

    //Derived classes should override these to respond to specific events of interest
    protected abstract void onIncomingCallReceived(Context ctx,String number,Date start);
    protected abstract void onIncomingCallAnswered(Context ctx,Date start);
    protected abstract void onIncomingCallEnded(Context ctx,Date start,Date end);

    protected abstract void onOutgoingCallStarted(Context ctx,Date start);
    protected abstract void onOutgoingCallEnded(Context ctx,Date end);

    protected abstract void onMissedCall(Context ctx,Date start);

    //Deals with actual events

    //Incoming call-  goes from IDLE to RINGING when it rings,to OFFHOOK when it's answered,to IDLE when its hung up
    //Outgoing call-  goes from IDLE to OFFHOOK when it dials out,to IDLE when hung up
    public void onCallStateChanged(Context context,int state,String number) {
        if(lastState == state){
            //No change,debounce extras
            return;
        }
        switch (state) {
            case TelephonyManager.CALL_STATE_RINGING:
                isIncoming = true;
                callStartTime = new Date();
                savednumber = number;
                onIncomingCallReceived(context,number,callStartTime);
                break;
            case TelephonyManager.CALL_STATE_OFFHOOK:
                //Transition of ringing->offhook are pickups of incoming calls.  nothing done on them
                if(lastState != TelephonyManager.CALL_STATE_RINGING){
                    isIncoming = false;
                    callStartTime = new Date();
                    onOutgoingCallStarted(context,savednumber,callStartTime);
                }
                else
                {
                    isIncoming = true;
                    callStartTime = new Date();
                    onIncomingCallAnswered(context,callStartTime);
                }

                break;
            case TelephonyManager.CALL_STATE_IDLE:
                //Went to idle-  this is the end of a call.  What type depends on prevIoUs state(s)
                if(lastState == TelephonyManager.CALL_STATE_RINGING){
                    //Ring but no pickup-  a miss
                    onMissedCall(context,callStartTime);
                }
                else if(isIncoming){
                    onIncomingCallEnded(context,callStartTime,new Date());
                }
                else{
                    onOutgoingCallEnded(context,new Date());
                }
                break;
        }
        lastState = state;
    }
}

呼叫接收器类:(在 setAudioSource() 部分中,我尝试了 mic、voice_call、voice_communication。对于 'voice_call',我在 'start()' 方法上出错,其他人在 'stop()' 方法上出错)

>
    import android.Manifest;
    import android.app.Activity;
    import android.content.Context;
    import android.media.MediaRecorder;
    import android.os.Environment;
    import android.telephony.PhonestateListener;
    import android.telephony.TelephonyManager;
    import android.text.format.DateFormat;
    import android.util.Log;
    import android.widget.Toast;
    
    import androidx.core.app.ActivityCompat;
    
    import java.io.File;
    import java.io.IOException;
    import java.util.Date;

import static android.widget.Toast.LENGTH_SHORT;


public class CallReceiver extends PhoneCallReceiver {

    private static final int MY_PERMISSIONS = 800;
    private MediaRecorder recorder;

    public CallReceiver(){

        this.recorder=new MediaRecorder();

    }


    @Override
    protected void onIncomingCallReceived(Context ctx,Date start) {


    }

    @Override
    protected void onIncomingCallAnswered(Context ctx,Date start) {

        TelephonyManager manager=(TelephonyManager)ctx.getSystemService(ctx.TELEPHONY_SERVICE);

        manager.listen(new PhonestateListener(){

            @Override
            public void onCallStateChanged(int state,String phoneNumber) {

             if (TelephonyManager.CALL_STATE_OFFHOOK == state) {

                    File filePath= Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS);
                    CharSequence chsq= DateFormat.format("MM-dd-yy-hh-mm-ss",new Date().getTime());

                    if (recorder==null){recorder=new MediaRecorder();}

                    try {
                        recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_RECOGNITION);
                    } catch (Exception e) {
                        Log.e("Error","SetAudioSource Failed");
                        e.printstacktrace();
                    }


                    recorder.setoutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
                    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
                    recorder.setoutputFile(filePath.getAbsolutePath()+"/"+chsq+"rec.3gp");

                    try {
                        recorder.prepare();
                    } catch (IOException e) {
                        e.printstacktrace();
                    }
                    recorder.start();

                    Toast.makeText(context,"Recording Started",LENGTH_SHORT).show();


                }


            },PhonestateListener.LISTEN_CALL_STATE);


    }

    @Override
    protected void onIncomingCallEnded(Context ctx,Date end) {

        TelephonyManager manager=(TelephonyManager)ctx.getSystemService(ctx.TELEPHONY_SERVICE);
        manager.listen(new PhonestateListener() {

               @Override
               public void onCallStateChanged(int state,String phoneNumber) {


                   if (TelephonyManager.CALL_STATE_IDLE == state) {


                       recorder.stop();
                       recorder.reset();
                       recorder.release();

                   }


               }

        },PhonestateListener.LISTEN_CALL_STATE);



    }

    @Override
    protected void onOutgoingCallStarted(Context ctx,Date start) {

    }

    @Override
    protected void onOutgoingCallEnded(Context ctx,Date end) {

    }

    @Override
    protected void onMissedCall(Context ctx,Date start) {

    }
}

主要活动:

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements PermissionRequestCodes{



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        checkPermissions();
    }
    

    public void checkPermissions(){

        ActivityCompat.requestPermissions(this,new String[]{
                Manifest.permission.READ_PHONE_STATE,Manifest.permission.RECORD_AUdio,Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE},MY_PERMISSIONS);

    }

}

和清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.codeforlite.myapplication">
    <uses-permission android:name="android.permission.RECORD_AUdio" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
    <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>


        <receiver android:name=".CallReceiver" >
            <intent-filter>
                <action android:name="android.intent.action.PHONE_STATE" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.NEW_OUTGOING_CALL" />
            </intent-filter>
        </receiver>

    </application>

</manifest>

如果有人提供帮助,我将不胜感激。如果我的方式完全错误,我如何设法制作通话录音应用程序?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)