听是我的片段代码。我正在使用媒体播放器进行录制 当我仅使用像ForntCamera和BackCamera这样的单个摄像头时,此代码有效,但是当我使用camaraId切换摄像头时,媒体播放器会给出“无效的预览图面”异常。我哪里错了? 请帮我。谢谢,谢谢
import android.hardware.Camera;
import android.media.CamcorderProfile;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextClock;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import static android.provider.MediaStore.Files.FileColumns.MEDIA_TYPE_IMAGE;
import static android.provider.MediaStore.Files.FileColumns.MEDIA_TYPE_VIDEO;
import static mms.dweb.buzzcutz.ApplicationContext.TAG;
public class VideoRecordFragmentB extends BaseFragment {
@BindView(R.id.record_btn_frb)
ImageView record_btn;
@BindView(R.id.cameraSwitch)
ImageView cameraSwitch;
@BindView(R.id.mTextField)
TextClock mTextField;
int camId = 0;
@BindView(R.id.camera_preview)
FrameLayout preview;
MediaPlayer mediaPlayer;
String catId;
Boolean back = true;
private View fragmentView;
private MainActivity mainActivity;
private Camera mCamera;
private CameraPreview mPreview;
private MediaRecorder mMediaRecorder;
private boolean isRecording = false;
private CategoryResponce.Responsedata categoryData;
private static Uri getOutputMediaFileUri(int type) {
return Uri.fromFile(getOutputMediaFile(type));
}
/**
* Create a File for saving an image or video
*/
private static File getOutputMediaFile(int type) {
// To be safe, you should check that the SDCard is mounted
// using Environment.getExternalStorageState() before doing this.
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), "Buzzcut");
// This location works best if you want the created images to be shared
// between applications and persist after your app has been uninstalled.
// Create the storage directory if it does not exist
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
Log.d("MyCameraApp", "failed to create directory");
return null;
}
}
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File mediaFile;
if (type == MEDIA_TYPE_IMAGE) {
mediaFile = new File(mediaStorageDir.getPath() + File.separator +
"IMG_" + timeStamp + ".jpg");
} else if (type == MEDIA_TYPE_VIDEO) {
mediaFile = new File(mediaStorageDir.getPath() + File.separator +
"VID_" + timeStamp + ".mp4");
} else {
return null;
}
return mediaFile;
}
@OnClick(R.id.cameraSwitch)
void changeCam() {
if (camId == 0) {
camId = 1;
} else {
camId = 0;
}
managView();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
fragmentView =
inflater.inflate(R.layout.fragment_video_record_fragment_b, container, false);
ButterKnife.bind(this, fragmentView);
initializeView();
return fragmentView;
}
@Override
public void initializeView() {
super.initializeView();
if (getActivity() instanceof MainActivity) {
mainActivity = (MainActivity) getActivity();
}
managView();
}
private void managView() {
mCamera = getCameraInstance(camId);
mPreview = new CameraPreview(getContext(), mCamera);
preview.addView(mPreview);
Bundle bundle = getArguments();
categoryData = (CategoryResponce.Responsedata)
bundle.getSerializable("CategoryResponceData");
catId = categoryData.getId();
Bundle bundle1 = new Bundle();
bundle1.putSerializable("CategoryResponceData", categoryData);
record_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (isRecording) {
// stop recording and release camera
mMediaRecorder.stop(); // stop the recording
releaseMediaRecorder(); // release the MediaRecorder object
mCamera.lock();// take camera access back from MediaRecorder
record_btn.setImageResource(R.drawable.record_btn);
// inform the user that recording has stopped
isRecording = false;
Bundle bundle = new Bundle();
bundle.putString("fileUri",
getOutputMediaFileUri(MEDIA_TYPE_VIDEO).toString());
bundle.putSerializable("CategoryResponceData", categoryData);
mainActivity.replaceFragment(FragmentNames.VideoUploadFragment, bundle, false, false);
} else {
// initialize video camera
if (prepareVideoRecorder(camId)) {
// Camera is available and unlocked, MediaRecorder is prepared,
// now you can start recording
record_btn.setImageResource(R.drawable.recoder);
mMediaRecorder.start();
// inform the user that recording has started
isRecording = true;
} else {
// prepare didn't work, release the camera
releaseMediaRecorder();
// inform user
}
}
}
});
}
@Override
public void onStop() {
super.onStop();
}
public Camera getCameraInstance(int camid) {
releaseCameraAndPreview();
Camera c = null;
try {
c = Camera.open(camid); // attempt to get a Camera instance
} catch (Exception e) {
// Camera is not available (in use or does not exist)
}
return c; // returns null if camera is unavailable
}
private void releaseCameraAndPreview() {
preview.removeAllViews();
if (mCamera != null) {
mCamera.release();
mCamera = null;
}
}
private boolean prepareVideoRecorder(int camid) {
mCamera = getCameraInstance(camid);
mMediaRecorder = new MediaRecorder();
mCamera.unlock();
mMediaRecorder.setCamera(mCamera);
mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mMediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));
mMediaRecorder.setOutputFile(getOutputMediaFile(MEDIA_TYPE_VIDEO).toString());
mMediaRecorder.setPreviewDisplay(mPreview.getHolder().getSurface());
try {
mMediaRecorder.prepare();
} catch (IllegalStateException e) {
Log.d(TAG, "IllegalStateException preparing MediaRecorder: " + e.getMessage());
releaseMediaRecorder();
return false;
} catch (IOException e) {
Log.d(TAG, "IOException preparing MediaRecorder: " + e.getMessage());
releaseMediaRecorder();
return false;
}
return true;
}
@Override
public void onPause() {
super.onPause();
releaseMediaRecorder(); // if you are using MediaRecorder, release it first
releaseCamera(); // release the camera immediately on pause event
}
private void releaseMediaRecorder() {
if (mMediaRecorder != null) {
mMediaRecorder.reset(); // clear recorder configuration
mMediaRecorder.release(); // release the recorder object
mMediaRecorder = null;
mCamera.lock(); // lock camera for later use
}
}
private void releaseCamera() {
if (mCamera != null) {
mCamera.release(); // release the camera for other applications
mCamera = null;
}
}
}
听力是“相机预览类”。我正在使用此类来获取相机
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder mHolder;
private Camera mCamera;
private CamcorderProfile camcorderProfile;
public CameraPreview(Context context, Camera camera) {
super(context);
mCamera = camera;
mHolder = getHolder();
mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
public void surfaceCreated(SurfaceHolder holder) {
preview.
try {
mCamera.setPreviewDisplay(holder);
mCamera.setDisplayOrientation(90);
mCamera.startPreview();
} catch (IOException e) {
Log.d(TAG, "Error setting camera preview: " + e.getMessage());
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
}
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
if (mHolder.getSurface() == null){
return;
}
try {
mCamera.stopPreview();
} catch (Exception e){
}
try {
mCamera.setPreviewDisplay(mHolder);
mCamera.setDisplayOrientation(90);
mCamera.startPreview();
} catch (Exception e){
Log.d(TAG, "Error starting camera preview: " + e.getMessage());
}
}
}
答案 0 :(得分:0)
无论如何,请在onResume中初始化视图
@Override
public void onResume() {
super.onResume();
initializeView();
}
您是否已在清单中声明了许可?
<uses-permission android:name="android.permission.RECORD_AUDIO" />