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

如何解决为什么我在 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 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-