Android Audio Media Recorder停止失败

时间:2017-12-04 20:22:11

标签: android android-mediaplayer

public class RecordCallService extends Service {

public final static String ACTION_START_RECORDING = "com.ss.ACTION_CLEAN_UP";
public final static String ACTION_STOP_RECORDING = "com.ss.ACTION_STOP_RECORDING";
public final static String EXTRA_PHONE_CALL = "com.ss.EXTRA_PHONE_CALL";

public RecordCallService(){
}

@Override
public IBinder onBind(Intent intent) {
    return null;
}


@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    ContentValues parcelableExtra = intent.getParcelableExtra(EXTRA_PHONE_CALL);
    startRecording(new CallLog(parcelableExtra));
    return START_NOT_STICKY ;
}

@Override
public void onDestroy() {
    stopRecording();
    super.onDestroy();
}

private CallLog phoneCall;

boolean isRecording = false;

private void stopRecording() {

    if (isRecording) {
        try {
            phoneCall.setEndTime(Calendar.getInstance());
            mediaRecorder.stop();
            mediaRecorder.reset();
            mediaRecorder.release();
            mediaRecorder = null;
            isRecording = false;

            phoneCall.save(getBaseContext());
            displayNotification(phoneCall);

            LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(new Intent(LocalBroadcastActions.NEW_RECORDING_BROADCAST));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    phoneCall = null;
}


MediaRecorder mediaRecorder;


private void startRecording(CallLog phoneCall) {
    if (!isRecording) {
        isRecording = true;
        this.phoneCall = phoneCall;
        File file = null;
        try {
            this.phoneCall.setSartTime(Calendar.getInstance());
            File dir = AppPreferences.getInstance(getApplicationContext()).getFilesDirectory();
            mediaRecorder = new MediaRecorder();
            file = File.createTempFile("record", ".3gp", dir);
            this.phoneCall.setPathToRecording(file.getAbsolutePath());
            mediaRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
            mediaRecorder.setAudioSamplingRate(8000);
            mediaRecorder.setAudioEncodingBitRate(12200);
            mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
            mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
            mediaRecorder.setOutputFile(phoneCall.getPathToRecording());
            mediaRecorder.prepare();
            mediaRecorder.start();
        } catch (Exception e) {
            e.printStackTrace();
            isRecording = false;
            if (file != null) file.delete();
            this.phoneCall = null;
            isRecording = false;
        }
    }
}

public void displayNotification(CallLog phoneCall) {
    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
    builder.setSmallIcon(R.drawable.ic_recording_conversation_white_24);
    builder.setContentTitle(getApplicationContext().getString(R.string.notification_title));
    builder.setContentText(getApplicationContext().getString(R.string.notification_text));
    builder.setContentInfo(getApplicationContext().getString(R.string.notification_more_text));
    builder.setAutoCancel(true);

    Intent intent = new Intent(getApplicationContext(), MainActivity.class);
    intent.setAction(Long.toString(System.currentTimeMillis())); // fake action to force PendingIntent.FLAG_UPDATE_CURRENT
    intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

    intent.putExtra("RecordingId", phoneCall.getId());

    builder.setContentIntent(PendingIntent.getActivity(this, 0xFeed, intent, PendingIntent.FLAG_UPDATE_CURRENT));
    notificationManager.notify(0xfeed, builder.build());
}


public static void sartRecording(Context context, CallLog phoneCall) {
    Intent intent = new Intent(context, RecordCallService.class);
    intent.setAction(ACTION_START_RECORDING);
    intent.putExtra(EXTRA_PHONE_CALL, phoneCall.getContent());
    context.startService(intent);
}


public static void stopRecording(Context context) {
    Intent intent = new Intent(context, RecordCallService.class);
    intent.setAction(ACTION_STOP_RECORDING);
    context.stopService(intent);
}


}

这是我的代码

现在是我的Logcat

 12-05 01:44:52.599 6356-6356/com.ss.automaticrecorder I/SSCALLRECORDER: MyCallReceiver.onReceive 
 12-05 01:44:52.683 6356-6356/com.ss.automaticrecorder E/MediaRecorder: stop failed: -1007
 12-05 01:44:52.683 6356-6356/com.ss.automaticrecorder W/System.err: java.lang.RuntimeException: stop failed.
 12-05 01:44:52.687 6356-6356/com.ss.automaticrecorder W/System.err:     at android.media.MediaRecorder.stop(Native Method)
 12-05 01:44:52.688 6356-6356/com.ss.automaticrecorder W/System.err:     at com.ss.automaticrecorder.services.RecordCallService.stopRecording(RecordCallService.java:65)
 12-05 01:44:52.688 6356-6356/com.ss.automaticrecorder W/System.err:     at com.ss.automaticrecorder.services.RecordCallService.onDestroy(RecordCallService.java:52)
 12-05 01:44:52.688 6356-6356/com.ss.automaticrecorder W/System.err:     at android.app.ActivityThread.handleStopService(ActivityThread.java:3058)
 12-05 01:44:52.688 6356-6356/com.ss.automaticrecorder W/System.err:     at android.app.ActivityThread.access$2300(ActivityThread.java:153)
 12-05 01:44:52.688 6356-6356/com.ss.automaticrecorder W/System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1450)
 12-05 01:44:52.688 6356-6356/com.ss.automaticrecorder W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:102)
 12-05 01:44:52.688 6356-6356/com.ss.automaticrecorder W/System.err:     at android.os.Looper.loop(Looper.java:148)
 12-05 01:44:52.688 6356-6356/com.ss.automaticrecorder W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5451)
 12-05 01:44:52.688 6356-6356/com.ss.automaticrecorder W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
 12-05 01:44:52.688 6356-6356/com.ss.automaticrecorder W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
 12-05 01:44:52.688 6356-6356/com.ss.automaticrecorder W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
 12-05 01:44:57.677 6356-6475/com.ss.automaticrecorder V/FA: Inactivity, disconnecting from the service
 12-05 01:45:06.528 6356-6356/com.ss.automaticrecorder I/SSCALLRECORDER: MyCallReceiver.onReceive 

我不知道错误在哪里因为这个代码在三星,联想等其他制造商上完美运行但在asus zenfone系列上遇到了问题 由于logcat显示在开始之前调用了stop,但似乎我做得对 我正在使用媒体记录器获取更多信息

0 个答案:

没有答案