首页 > 解决方案 > 记录来电时出错不起作用?

问题描述

我正在制作通话录音应用程序拨出电话正在录音,但来电没有录音。我也在不同的手机上测试过,但问题是一样的。我测试了不同的方法,如“ MediaRecorder.AudioSource.MIC”、“ MediaRecorder.AudioSource.VOICE_CALL”和“ MediaRecorder.AudioSource.VOICE_COMMUNICATION”。在尝试了不同的方法之后,我尝试了

myRecorder.setAudioSource(MediaRecorder.AudioSource.VOICE_UPLINK+ MediaRecorder.AudioSource.VOICE_DOWNLINK);

主要 MainActivity 代码

public class MainActivity extends AppCompatActivity {

    Button start_rec, stop_rec;
    TextView status;
    MediaRecorder myRecorder;
    SimpleDateFormat dateFormat;
    String currentTimeStamp;
    File outputFile;

    private static final int REQUEST_CODE = 0;
    private DevicePolicyManager mDPM;
    private ComponentName mAdminName;

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

        start_rec = findViewById(R.id.start_rec);
        stop_rec = findViewById(R.id.stop_rec);
        status = findViewById(R.id.status);

        try {
            // Initiate DevicePolicyManager.
            mDPM = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
            mAdminName = new ComponentName(this, DeviceAdminDemo.class);

            if (!mDPM.isAdminActive(mAdminName)) {
                Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
                intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, mAdminName);
                intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "Click on Activate button to secure your application.");
                startActivityForResult(intent, REQUEST_CODE);
            } else {
                // mDPM.lockNow();
                // Intent intent = new Intent(MainActivity.this,
                // TrackDeviceService.class);
                // startService(intent);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        start_rec.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                File sampleDir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/rec2");
                File data = Environment.getDataDirectory();
                boolean success = true;
                if (!sampleDir.exists()) {
                    sampleDir.mkdirs();
                    success = sampleDir.mkdirs();
                }
                if (success) {
                    outputFile = new File(sampleDir, getCurrentTimeStamp() + ".mp3");
                    myRecorder = new MediaRecorder();
                    myRecorder.setAudioSource(MediaRecorder.AudioSource.VOICE_UPLINK + MediaRecorder.AudioSource.VOICE_DOWNLINK);
                    myRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
                    myRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
                    myRecorder.setAudioEncodingBitRate(16);
                    myRecorder.setAudioSamplingRate(44100);
                    myRecorder.setOutputFile(outputFile.getAbsolutePath());

                    try {
                        myRecorder.prepare();
                        myRecorder.start();
                    } catch (IllegalStateException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    status.setText("Recording");
                    status.setTextColor(Color.GREEN);
                    Toast.makeText(getApplicationContext(), "Recording...", Toast.LENGTH_SHORT).show();
                }

            }
        });

        stop_rec.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    myRecorder.stop();
                    myRecorder.reset();
                    myRecorder.release();

                    myRecorder = null;

                } catch (IllegalStateException e) {
                    e.printStackTrace();
                } catch (RuntimeException e) {
                    // no valid audio/video data has been received
                    e.printStackTrace();
                }
                status.setText("Recording Stopped");
                status.setTextColor(Color.RED);

                Toast.makeText(getApplicationContext(), "Recording Stopped", Toast.LENGTH_SHORT).show();
//                refresh();
            }
        });
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (REQUEST_CODE == requestCode) {
//            Intent intent = new Intent(MainActivity.this, TService.class);
//            startService(intent);
        }
    }
    public void refresh() {
        Intent intent = getIntent();
        finish();
        startActivity(intent);
    }

    public String getCurrentTimeStamp() {
        try {
            dateFormat = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
            currentTimeStamp = dateFormat.format(new Date()); // Find todays date
            return currentTimeStamp;

        } catch (Exception e) {
            e.printStackTrace();


        }
        return null;
    }
}

设备管理员代码

public class DeviceAdminDemo extends DeviceAdminReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        super.onReceive(context, intent);
    }

    public void onEnabled(Context context, Intent intent) {
    };

    public void onDisabled(Context context, Intent intent) {
    };
}

清单代码

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="waceem.virk.voicerecording">

    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.CAPTURE_AUDIO_OUTPUT"
        tools:ignore="ProtectedPermissions" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.read_external_storage" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:requestLegacyExternalStorage="true"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.VoiceRecording">
        <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=".DeviceAdminDemo"
            android:permission="android.permission.BIND_DEVICE_ADMIN" >
            <meta-data
                android:name="android.app.device_admin"
                android:resource="@xml/my_admin" />
            <intent-filter>
                <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
                <action android:name="android.app.action.DEVICE_ADMIN_DISABLED" />
                <action android:name="android.app.action.DEVICE_ADMIN_DISABLE_REQUESTED" />
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <category android:name="android.intent.category.HOME" />
            </intent-filter>
        </receiver>

    </application>

</manifest>

标签: javaandroidincoming-callcall-recording

解决方案


要使用它,您需要 Manifest.permission.CAPTURE_AUDIO_OUTPUT 权限,这是一个系统权限。这意味着您需要由 OEM 预安装,或者用户需要 root 他们的设备并知道如何使您的应用程序成为系统应用程序。

基本上Android不允许你这样做。你能做的最多就是让它用普通的麦克风捕捉,并希望呼叫者大声说话,或者用户把它放在免提上。


推荐阅读