我正在使用Service
课程并使用Camera API
开发背景录像机。当我运行应用程序时,一切似乎都很好。但是当我播放输出文件时,我得到"无法播放此视频" 错误。我已经尝试调试应用程序但无法找出问题所在。
这是我的录像机Service
课程:
public class VideoRecordingOldApiService extends Service implements SurfaceHolder.Callback {
private Camera mCamera;
private String outputFile;
private MediaRecorder mMediaRecorder;
private CameraCheck cameraCheck;
private final static String TAG = "VideoRecorderService";
private LinearLayout linearLayout;
private SurfaceView surfaceView;
private SurfaceHolder surfaceHolder;
private WindowManager windowManager;
private Thread thread;
private Handler handler;
public VideoRecordingOldApiService() {
}
@Override
public void onCreate() {
linearLayout = (LinearLayout) LayoutInflater.from(this).inflate(R.layout.surfaceview_layout, null);
linearLayout.setLayoutParams(new LinearLayout.LayoutParams(
getResources().getDimensionPixelSize(R.dimen.textureView_width), getResources().getDimensionPixelSize(R.dimen.textureView_height)));
windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
PixelFormat.OPAQUE
);
layoutParams.gravity = Gravity.TOP | Gravity.LEFT;
windowManager.addView(linearLayout, layoutParams);
surfaceView = (SurfaceView) linearLayout.findViewById(R.id.videoSurfaceView);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
cameraCheck = new CameraCheck();
handler = new Handler();
//Setup Notification
final Intent notificationIntent = new Intent(getApplicationContext(), MainActivity.class);
notificationIntent.addCategory("android.intent.category.LAUNCHER");
final PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
Notification notification;
notification = new Notification.Builder(getApplicationContext())
.setSmallIcon(R.drawable.videorecordicon)
.setOngoing(true)
.setPriority(Notification.PRIORITY_DEFAULT)
.setContentTitle(getResources().getString(R.string.video_recorder_notification))
.setContentText(getResources().getString(R.string.notification_video_text) + "...")
.setContentIntent(pendingIntent).build();
startForeground(1, notification);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (initCamera()) {
thread = new Thread(new Runnable() {
@Override
public void run() {
Looper.prepare();
if (initRecorder()) {
mMediaRecorder.start();
handler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), R.string.video_recorder_started, Toast.LENGTH_LONG).show();
}
});
} else {
handler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), "Couldn't start MediaRecorder", Toast.LENGTH_LONG).show();
}
});
}
}
});
thread.start();
} else {
Toast.makeText(getApplicationContext(), "Camera not found", Toast.LENGTH_SHORT).show();
}
return START_REDELIVER_INTENT;
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
public String getFileName() {
String fileName = null;
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
fileName = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
+ "/" + "video_record" + System.currentTimeMillis() + ".mp4";
Log.i("Camera Recorder", fileName);
} else {
Toast.makeText(getApplicationContext(), "No External Storage Found", Toast.LENGTH_LONG).show();
}
return fileName;
}
private boolean initCamera() {
try {
mCamera = cameraCheck.getDesiredCamera(this, Camera.CameraInfo.CAMERA_FACING_BACK);
Camera.Parameters parameters = mCamera.getParameters();
parameters.setRecordingHint(true);
Camera.Size previewSize = getOptimalPreviewSize(parameters.getSupportedPreviewSizes(),
surfaceView.getWidth(), surfaceView.getHeight());
parameters.setPreviewSize(previewSize.width, previewSize.height);
mCamera.setDisplayOrientation(90);
} catch (Exception e) {
Log.v(TAG, "Could not initialise the camera");
e.printStackTrace();
return false;
}
return true;
}
private boolean initRecorder() {
mMediaRecorder = new MediaRecorder();
outputFile = getFileName();
CamcorderProfile profile = CamcorderProfile.get(Camera.CameraInfo.CAMERA_FACING_BACK, CamcorderProfile.QUALITY_HIGH);
profile.videoFrameWidth = mCamera.getParameters().getSupportedVideoSizes().get(1).width;
profile.videoFrameHeight = mCamera.getParameters().getSupportedVideoSizes().get(1).height;
profile.fileFormat = MediaRecorder.OutputFormat.MPEG_4;
profile.audioCodec = MediaRecorder.AudioEncoder.AAC;
profile.videoCodec = MediaRecorder.VideoEncoder.H264;
try {
mCamera.unlock();
mMediaRecorder.setCamera(mCamera);
mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mMediaRecorder.setProfile(profile);
mMediaRecorder.setOutputFile(outputFile);
//mMediaRecorder.setPreviewDisplay(surfaceHolder.getSurface());
mMediaRecorder.prepare();
Log.v(TAG, "MediaRecorder initialized successfully");
} catch (Exception e) {
Log.v(TAG, "MediaRecorder failed to initialize");
e.printStackTrace();
return false;
}
return true;
}
private void releaseCamera() {
if (mCamera != null) {
mCamera.lock();
mCamera.release();
mCamera = null;
}
}
@Override
public void onDestroy() {
super.onDestroy();
if (!thread.isInterrupted()) {
thread.interrupt();
Toast.makeText(getApplicationContext(), R.string.recording_done, Toast.LENGTH_LONG).show();
}
if (linearLayout != null) {
windowManager.removeView(linearLayout);
linearLayout = null;
}
stopForeground(true);
releaseCamera();
thread = null;
}
private Camera.Size getOptimalPreviewSize(List<Camera.Size> sizes, int w, int h) {
final double ASPECT_TOLERANCE = 0.1;
double targetRatio = (double) h / w;
if (sizes == null)
return null;
Camera.Size optimalSize = null;
double minDiff = Double.MAX_VALUE;
int targetHeight = h;
for (Camera.Size size : sizes) {
double ratio = (double) size.height / size.width;
if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE)
continue;
if (Math.abs(size.height - targetHeight) < minDiff) {
optimalSize = size;
minDiff = Math.abs(size.height - targetHeight);
}
}
if (optimalSize == null) {
minDiff = Double.MAX_VALUE;
for (Camera.Size size : sizes) {
if (Math.abs(size.height - targetHeight) < minDiff) {
optimalSize = size;
minDiff = Math.abs(size.height - targetHeight);
}
}
}
return optimalSize;
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
try {
mCamera.setPreviewDisplay(holder);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
}
编辑:这是我的logcat:
07-09 22:32:26.022 137-137/? W/MPEG4Writer: getEncodeQualiytAdjustParam,sVideoQualityAdjustParamTable no related Quality Param
07-09 22:32:26.022 137-137/? E/MPEG4Writer: configVideoQualityAdjustParams,mVideoBitrateLowPercentage and mVideoDynamicAutoFPSDropRate are invalid, disable quality adjust
07-09 22:32:26.023 137-137/? D/MPEG4Writer: video Track start
07-09 22:32:26.023 137-137/? D/MPEG4Writer: video Track start,mStartTimeRealUs=222832948
07-09 22:32:26.023 137-137/? D/MPEG4Writer: Start Video MediaSource
07-09 22:32:26.024 137-137/? I/OMXCodec: [OMX.MTK.VIDEO.ENCODER.AVC] @@ def.nBufferCountActual = 10,
07-09 22:32:26.057 137-137/? W/CameraSource: Failed to set video buffer count to 10 due to -38
07-09 22:32:26.275 137-3729/? W/CameraSource: Timed out waiting for incoming camera video frames: 0 us
07-09 22:32:26.335 137-137/? D/MPEG4Writer: Start Video MediaSource finish, err=0
07-09 22:32:26.335 137-137/? D/MPEG4Writer: video Track track thread start OK--
07-09 22:32:26.339 137-4835/? D/MPEG4Writer: Video mNumSamples=0, mDone=0
07-09 22:32:26.574 137-3730/? D/MPEG4Writer: left_video_chunk_num=0, mInterleaveDurationUs=1000000, mBitrate=6128000
07-09 22:32:26.732 137-463/? I/CameraSource: the first video frame,mStartTimeUs=10516,RealStartTimeUs=223543464
07-09 22:32:26.802 137-4819/? I/VDO_LOG: [M4U][eVideoMemAlloc] pvMemVa = 0x479cf008, pvAlignMemVa = 0x479cf008, pvMemPa = 0x1b00008, pvAlignMemPa = 0x1b00008, MemSize = 624, ByteAlignment = 1
07-09 22:32:26.823 137-4819/? I/VDO_LOG: [M4U][eVideoMemAlloc] pvMemVa = 0x47d21020, pvAlignMemVa = 0x47d21020, pvMemPa = 0x1b40020, pvAlignMemPa = 0x1b40020, MemSize = 1970560, ByteAlignment = 32
07-09 22:32:26.824 137-4819/? I/VDO_LOG: [M4U][eVideoMemAlloc] pvMemVa = 0x4794cce8, pvAlignMemVa = 0x4794cce8, pvMemPa = 0x1d40ce8, pvAlignMemPa = 0x1d40ce8, MemSize = 624, ByteAlignment = 1
07-09 22:32:26.829 137-4819/? I/VDO_LOG: [M4U][eVideoMemAlloc] pvMemVa = 0x47f03020, pvAlignMemVa = 0x47f03020, pvMemPa = 0x1d80020, pvAlignMemPa = 0x1d80020, MemSize = 1970560, ByteAlignment = 32
07-09 22:32:26.830 137-4819/? I/VDO_LOG: [M4U][eVideoMemAlloc] pvMemVa = 0x438738c0, pvAlignMemVa = 0x438738c0, pvMemPa = 0x1f808c0, pvAlignMemPa = 0x1f808c0, MemSize = 624, ByteAlignment = 1
07-09 22:32:26.841 137-4819/? I/VDO_LOG: [M4U][eVideoMemAlloc] pvMemVa = 0x480e5020, pvAlignMemVa = 0x480e5020, pvMemPa = 0x1fc0020, pvAlignMemPa = 0x1fc0020, MemSize = 1970560, ByteAlignment = 32
07-09 22:32:26.842 137-4819/? I/VDO_LOG: [M4U][eVideoMemAlloc] pvMemVa = 0x45edadc0, pvAlignMemVa = 0x45edadc0, pvMemPa = 0x21c0dc0, pvAlignMemPa = 0x21c0dc0, MemSize = 624, ByteAlignment = 1
07-09 22:32:26.852 137-4819/? I/VDO_LOG: [M4U][eVideoMemAlloc] pvMemVa = 0x482c7020, pvAlignMemVa = 0x482c7020, pvMemPa = 0x2200020, pvAlignMemPa = 0x2200020, MemSize = 1970560, ByteAlignment = 32
07-09 22:32:26.853 137-4819/? I/VDO_LOG: [M4U][eVideoMemAlloc] pvMemVa = 0x46f7ca88, pvAlignMemVa = 0x46f7ca88, pvMemPa = 0x2400a88, pvAlignMemPa = 0x2400a88, MemSize = 624, ByteAlignment = 1
07-09 22:32:26.853 137-4819/? E/VDO_LOG: [ERROR] 1 assertion!! line 221, file(mediatek/platform/mt6572/hardware/vcodec/video_codec/sw_h264_enc/adaptor/src/h264_encoder_component_v2.c)
07-09 22:32:26.853 137-4819/? E/VDO_LOG: [ERROR] 3 assertion error line 221, file(mediatek/platform/mt6572/hardware/vcodec/video_codec/sw_h264_enc/adaptor/src/h264_encoder_component_v2.c)
07-09 22:32:26.854 137-4819/? I/VDO_LOG: [M4U][eVideoMemAlloc] pvMemVa = 0x46f7cd00, pvAlignMemVa = 0x46f7cd00, pvMemPa = 0x2440d00, pvAlignMemPa = 0x2440d00, MemSize = 624, ByteAlignment = 1
07-09 22:32:26.855 137-4819/? E/VDO_LOG: [ERROR] 1 assertion!! line 221, file(mediatek/platform/mt6572/hardware/vcodec/video_codec/sw_h264_enc/adaptor/src/h264_encoder_component_v2.c)
07-09 22:32:26.855 137-4819/? E/VDO_LOG: [ERROR] 3 assertion error line 221, file(mediatek/platform/mt6572/hardware/vcodec/video_codec/sw_h264_enc/adaptor/src/h264_encoder_component_v2.c)
07-09 22:32:26.856 137-4819/? I/VDO_LOG: [M4U][eVideoMemAlloc] pvMemVa = 0x43873010, pvAlignMemVa = 0x43873010, pvMemPa = 0x2480010, pvAlignMemPa = 0x2480010, MemSize = 624, ByteAlignment = 1
07-09 22:32:26.857 137-4819/? E/VDO_LOG: [ERROR] 1 assertion!! line 221, file(mediatek/platform/mt6572/hardware/vcodec/video_codec/sw_h264_enc/adaptor/src/h264_encoder_component_v2.c)
07-09 22:32:26.857 137-4819/? E/VDO_LOG: [ERROR] 3 assertion error line 221, file(mediatek/platform/mt6572/hardware/vcodec/video_codec/sw_h264_enc/adaptor/src/h264_encoder_component_v2.c)
07-09 22:32:26.858 137-4819/? I/VDO_LOG: [M4U][eVideoMemAlloc] pvMemVa = 0x47cffad8, pvAlignMemVa = 0x47cffad8, pvMemPa = 0x24c0ad8, pvAlignMemPa = 0x24c0ad8, MemSize = 624, ByteAlignment = 1
07-09 22:32:26.858 137-4819/? E/VDO_LOG: [ERROR] 1 assertion!! line 221, file(mediatek/platform/mt6572/hardware/vcodec/video_codec/sw_h264_enc/adaptor/src/h264_encoder_component_v2.c)
07-09 22:32:26.858 137-4819/? E/VDO_LOG: [ERROR] 3 assertion error line 221, file(mediatek/platform/mt6572/hardware/vcodec/video_codec/sw_h264_enc/adaptor/src/h264_encoder_component_v2.c)
07-09 22:32:29.278 137-3729/? W/CameraSource: Timed out waiting for incoming camera video frames: 0 us
07-09 22:32:29.599 137-3730/? D/MPEG4Writer: left_video_chunk_num=0, mInterleaveDurationUs=1000000, mBitrate=6128000
07-09 22:32:29.799 137-4836/? D/MPEG4Writer: left_video_chunk_num=0, mInterleaveDurationUs=1000000, mBitrate=6128000
07-09 22:32:30.608 137-3730/? D/MPEG4Writer: left_video_chunk_num=0, mInterleaveDurationUs=1000000, mBitrate=6128000
我一直坚持这个功能很长一段时间。谁能帮我解决这个问题?????