2017-11-13 5 views
0

ほとんどのバージョンでは正常に動作しますが、Api 24-25の場合は、録画が開始されて録画を完了するとすぐにMEDIA_ERROR_SERVER_ADエラーが発生します。 具体的な理由がある場合は、録音を中断しないように処理できますか?MediaRecordで書き込もうとしたときにMEDIA_ERROR_SERVER_DIEDエラーが発生する

これが役立つ場合は、コードを添付します。タクス!

import android.content.Context; 
import android.content.Intent; 
import android.content.IntentFilter; 
import android.hardware.Camera; 
import android.media.CamcorderProfile; 
import android.media.MediaRecorder; 
import android.os.BatteryManager; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.OrientationEventListener; 
import android.view.Surface; 
import android.view.SurfaceHolder; 
import android.view.SurfaceView; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.FrameLayout; 

import java.io.File; 
import java.io.IOException; 
import java.text.SimpleDateFormat; 
import java.util.Date; 
import java.util.List; 
import java.util.Locale; 

import javax.inject.Inject; 

public class MainCameraFragment extends BaseFragment implements SurfaceHolder.Callback, 
    MediaRecorder.OnErrorListener, MediaRecorder.OnInfoListener { 

    private static final String TAG = "MainCameraFragment"; 

    private static final float MIN_BATTERY_CHARGE = 0.1f; 

    private Camera camera = null; 
    private int cameraId = -1; 
    private MediaRecorder recorder = null; 
    private boolean inPreview = false; 
    private Action<RecordingError> recordingErrorAction = null; 

    /** Surface view container. */ 
    private FrameLayout container; 
    /** Surface view instance. */ 
    private SurfaceView surfaceView; 
    /** Orientation listener. */ 
    private OrientationEventListener orientationEventListener; 
    /** Start recording time. */ 
    private long startRecordingTime = 0; 
    /** Last video path. */ 
    private File lastVideoPath; 

    /** Recording manager instance. */ 
    @Inject 
    RecordingManager recordingManager; 
    /** Settings manager instance. */ 
    @Inject 
    SettingsManager settingsManager; 

    /** Do on resume. */ 
    private Runnable doOnResume; 

    public enum RecordingError { 
    ILLEGAL_MEDIA_RECORDER_STATE, 
    NOT_ENOUGH_MEMORY, 
    NOT_ENOUGH_BATTERY, 
    } 

    /** Start preview from camera. */ 
    private void startPreview() { 
    if (camera != null && !inPreview) { 
     try { 

     camera.setPreviewDisplay(surfaceView.getHolder()); 

     Camera.Parameters parameters = camera.getParameters(); 
     final List<Camera.Size> supportedSizes = parameters.getSupportedPreviewSizes(); 

     Camera.Size bestFitSize = camera.new Size(0, 0); 

     for(Camera.Size size : supportedSizes) { 
      if(size.width <= surfaceView.getWidth() && size.height <= surfaceView.getHeight()) { 
      bestFitSize = size; 
      break; 
      } 
     } 

     if(bestFitSize.width == 0 && bestFitSize.height == 0) { 
      bestFitSize = supportedSizes.get(0); 
     } 

     parameters.setPreviewSize(bestFitSize.width, bestFitSize.height); 
     camera.setParameters(parameters); 
     camera.startPreview(); 

     inPreview = true; 
     } catch (IOException e) { 
     Log.e(TAG, "Error setting camera preview: " + e.getMessage()); 
     } 
    } 
    } 

    /** Stop camera preview. */ 
    private void stopPreview() { 
    if (camera != null && inPreview) { 
     try { 
     camera.stopPreview(); 
     } catch (Exception e) { 
     Log.i(TAG, "Error setting camera preview: " + e.getMessage()); 
     } 

     inPreview = false; 
    } 

    } 

    /** Init camera. */ 
    private void initCamera() { 
    if (camera == null) { 
     cameraId = recordingManager.getCameraId(); 

     if (cameraId >= 0) { 
     try { 
      camera = Camera.open(cameraId); 

      setupCameraDisplayOrientation(); 

      lockAutoFocus(camera, settingsManager.isLockAutoFocus()); 
      lockAutoExposure(camera, settingsManager.isLockAutoExposure()); 
     } 
     catch (Exception e) { 
      Log.e(TAG, e.getMessage(), e); 
     } 
     } else { 
     Log.e(TAG, "Can't find camera id"); 
     } 
    } 
    } 

    /** Release camera. */ 
    private void releaseCamera() { 
    if (camera != null) { 
     camera.release(); 
     camera = null; 
     cameraId = -1; 
    } 
    } 

    /** @return recording state. */ 
    public boolean isRecording() { 
    return recorder != null; 
    } 

    /** Start recording. */ 
    public void startRecording(final Action<RecordingError> recordingErrorAction, boolean micUsage) { 
    if (camera != null && !isRecording()) { 
     camera.stopPreview(); 
     final RecordingError recordingError = checkEnoughs(); 
     if (recordingError != null) { 
     if (recordingErrorAction != null) { 
      recordingErrorAction.act(recordingError); 
     } 
     return; 
     } 

     startRecordingTime = System.currentTimeMillis(); 

     final CamcorderProfile profile = recordingManager.getCamcorderProfile(cameraId, camera.getParameters()); 

     stopPreview(); 
     camera.unlock(); 

     MediaRecorder recorder = null; 
     try { 
     recorder = new MediaRecorder(); 
     recorder.setCamera(camera); 

     recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_COMMUNICATION); 

     recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); 

     recorder.setProfile(profile); 

     final String ts = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US).format(new Date()); 
     lastVideoPath = new File(recordingManager.getRecordsDirPath(), "Video_" + ts + ".mp4"); 
     recorder.setOutputFile(lastVideoPath.getAbsolutePath()); 
     final int orientationHint = getRecorderOrientationHint(); 
     if (orientationHint != -1) { 
      recorder.setOrientationHint(orientationHint); 
     } 
     recorder.setMaxFileSize(Long.MAX_VALUE); 
     recorder.setMaxDuration(Integer.MAX_VALUE); 

     stopPreview(); 
     recorder.setPreviewDisplay(surfaceView.getHolder().getSurface()); 
     recorder.setOnErrorListener(this); 
     recorder.setOnInfoListener(this); 
     recorder.prepare(); 
     recorder.start(); 

     this.recordingErrorAction = recordingErrorAction; 
     } catch (IOException e) { 
     Log.d(TAG, "Error " + e.getMessage(), e); 
     if (recorder != null) { 
      recorder.release(); 
      recorder = null; 
     } 
     } finally { 
     this.recorder = recorder; 
     } 
    } 
    } 

    /** Stop recording. */ 
    public void stopRecording() { 
    if (isRecording()) { 
     final MediaRecorder recorder = this.recorder; 
     this.recorder = null; 

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

     this.recordingErrorAction = null; 
     try { 
     camera.reconnect(); 
     startPreview(); 
     } catch (IOException e) { 
     Log.w(TAG, e.getMessage(), e); 
     } 
    } 
    } 

    @Override 
    public void onError(final MediaRecorder mr, final int what, final int extra) { 
    Log.e(TAG, "Media recorder error: " + what + " extra: " + extra); 

    if (recordingErrorAction != null) { 
     recordingErrorAction.act(RecordingError.ILLEGAL_MEDIA_RECORDER_STATE); 
    } 
    } 
} 
+0

これらの行のためにエラーが発生しました。 > recorder.setMaxFileSize(Long.MAX_VALUE); > recorder.setMaxDuration(Integer.MAX_VALUE); 削除された後、すべて正常に動作しています。 –

答えて

0

あなたはonErrorメソッド内MEDIA_ERROR_SERVER_DIEDメッセージをキャッチし、再度ごrecorderをインスタンス化しようとすることができます。 I上記の私のサンプルコードで

public void onError(final MediaRecorder mr, final int what, final int extra) { 
    Log.e(TAG, "Media recorder error: " + what + " extra: " + extra); 

    if (what == MediaPlayer.MEDIA_ERROR_SERVER_DIED) { 
     Log.i(LOGTAG, "MediaPlayer died, restarting"); 
     recorder.release(); 

     recorder = new MediaRecorder(); 
    } 

    if (recordingErrorAction != null) { 
     recordingErrorAction.act(RecordingError.ILLEGAL_MEDIA_RECORDER_STATE); 
    } 
} 

ちょうどラインを介してrecorderのインスタンスを再:recorder = new MediaRecorder();。おそらく、それ以降に設定を適用する必要があるかもしれません。

関連する問題