无法使用MediaRecorder

时间:2015-10-24 20:39:47

标签: java android android-mediarecorder

我用棒棒糖扎根 Nexus手机。我正在尝试记录收到的&拨打电话。

我的应用程序使用DeviceAdminReceiver

的政策扩展<force-lock />

我可以创建尺寸为&gt;的文件0bytes。但是当我玩它时,它是空白的。

请查看以下代码:

的Manifest.xml

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.STORAGE" />
.....
<service android:name=".RCService"
  android:exported="false" >
</service>
.....
 <receiver
            android:name=".utils.MyDeviceAdminReceiver"
            android:permission="android.permission.BIND_DEVICE_ADMIN" >
            <intent-filter>

                <!-- This action is required -->
                <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" />
            </intent-filter>

            <!-- This is required this receiver to become device admin component. -->
            <meta-data
                android:name="android.app.device_admin"
                android:resource="@xml/device_admin" />
        </receiver>


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

device_admin.xml

<device-admin xmlns:android="http://schemas.android.com/apk/res/android" >
    <uses-policies>
        <force-lock />
    </uses-policies>
</device-admin>

CallRecoderBroadcast.java

/**
 * Lifecycle :
 * Incoming call : RINGING -> OFFHOOK -> IDLE
 * Outgoing Call : OFFHOOK -> IDLE
 */
public class CallRecoderBroadcast extends BroadcastReceiver {

    private static final String TAG = "CallRecoderBroadcast";
    private static final String ACTION_PHONE = "android.intent.action.PHONE_STATE";
    private static final String OUTGOING_CALL = "android.intent.action.NEW_OUTGOING_CALL";

    private static Boolean mCallOnGoing = Boolean.FALSE;
    private MediaRecorder mRecorder = new MediaRecorder();
//    private File mFile = null;
    private String mNumber;

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

        try {

            if (intent.getAction().equals(ACTION_PHONE)) {

                Bundle bundle = intent.getExtras();
                String state = bundle.getString(TelephonyManager.EXTRA_STATE);

                Log.e(TAG, state);

                if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {

                    Log.e(TAG, bundle.getString(TelephonyManager.EXTRA_INCOMING_NUMBER));
                    mNumber = bundle.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);


                } else if (state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {

                    if (!mCallOnGoing) {
                        recordCall(context);
                    }


                } else if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)) {

                    if (mCallOnGoing) {
                        stopRecording();
                    }

                }

            } else if (intent.getAction().equals(OUTGOING_CALL)) {
                Log.e(TAG, "OUTGOING cALL");
                Log.e(TAG, intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER));
                mNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);


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

    }

    private void recordCall(Context context) throws IOException, CustomExcpetion {

        File file = prepareFile(context);

        if (file.exists()) {
            mCallOnGoing = Boolean.TRUE;
            mRecorder.setAudioSource(MediaRecorder.AudioSource.VOICE_COMMUNICATION);
            mRecorder.setOutputFormat(MediaRecorder.OutputFormat.AMR_NB);
            mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
            mRecorder.setOutputFile(file.getAbsolutePath());
            mRecorder.prepare();
            mRecorder.start();
        }

    }

    private void stopRecording() {
        mCallOnGoing = Boolean.FALSE;
//         mRecorder.stop();
        mRecorder.release();
    }

    private File prepareFile(Context context) throws CustomExcpetion {
        File outputFile = null;
        try {
            File dir = new File(context.getFilesDir(), "recorderdFiles");
            if (!dir.exists()) {
                dir.mkdirs();
            }

            Long timeStamp = System.currentTimeMillis();

            outputFile = new File(dir, timeStamp.toString());
            if (!outputFile.exists()) {
                outputFile.createNewFile();
            }
        } catch (IOException exp) {
            throw new CustomExcpetion(context, exp);
        }
        return outputFile;
    }
}

P.S。 - 我也尝试过给出here的例子,但它只是在Mic&amp;不适用于上行链路和同时进行下行链路。

1 个答案:

答案 0 :(得分:0)

不幸的是,该设备不支持语音呼叫记录器, 我认为这将在其他设备上正常工作。