如何在下一个活动中传输摄像机录制的视频并播放下一个活动的视频点播视频?
使用Media Recorder执行相机功能。
当尝试在空对象引用(根据日志)上调用虚拟方法'android.net.Uri android.content.Intent.getData()'时,当前有NullPointerException。
如何解决此问题。 这是我的代码:
第一项活动
public class AndroidVideoCaptureExample extends Activity {
private Camera mCamera;
private CameraPreview mPreview;
private MediaRecorder mediaRecorder;
private Button capture, switchCamera;
private Context myContext;
private LinearLayout cameraPreview;
private boolean cameraFront = false;
String mFileName;
Intent mediaIntent;
boolean isPlaying = false;
Uri url;
long maxduration = 10000;
SurfaceHolder holder;
Uri videoUri;
private static final int CAMERA_CAPTURE_VIDEO_REQUEST_CODE = 200;
// for timer
private TextView timerValue;
private long startTime = 0L;
private Handler customHandler = new Handler();
long timeInMilliseconds = 0L;
long timeSwapBuff = 0L;
long updatedTime = 0L;
MediaPlayer mediaPlayer;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
myContext = this;
initialize();
}
private int findFrontFacingCamera() {
int cameraId = -1;
// Search for the front facing camera
int numberOfCameras = Camera.getNumberOfCameras();
for (int i = 0; i < numberOfCameras; i++) {
CameraInfo info = new CameraInfo();
Camera.getCameraInfo(i, info);
if (info.facing == CameraInfo.CAMERA_FACING_FRONT) {
cameraId = i;
cameraFront = true;
break;
}
}
return cameraId;
}
private int findBackFacingCamera() {
int cameraId = -1;
// Search for the back facing camera
// get the number of cameras
int numberOfCameras = Camera.getNumberOfCameras();
// for every camera check
for (int i = 0; i < numberOfCameras; i++) {
CameraInfo info = new CameraInfo();
Camera.getCameraInfo(i, info);
if (info.facing == CameraInfo.CAMERA_FACING_BACK) {
cameraId = i;
cameraFront = false;
break;
}
}
return cameraId;
}
public void onResume() {
super.onResume();
if (!hasCamera(myContext)) {
Toast toast = Toast.makeText(myContext, "Sorry, your phone does not have a camera!", Toast.LENGTH_LONG);
toast.show();
finish();
}
if (mCamera == null) {
// if the front facing camera does not exist
if (findFrontFacingCamera() < 0) {
Toast.makeText(this, "No front facing camera found.", Toast.LENGTH_LONG).show();
switchCamera.setVisibility(View.GONE);
}
mCamera = Camera.open(findBackFacingCamera());
mPreview.refreshCamera(mCamera);
}
}
public void initialize() {
cameraPreview = (LinearLayout) findViewById(R.id.camera_preview);
mPreview = new CameraPreview(myContext, mCamera);
cameraPreview.addView(mPreview);
capture = (Button) findViewById(R.id.button_capture);
capture.setOnClickListener(captrureListener);
switchCamera = (Button) findViewById(R.id.button_ChangeCamera);
switchCamera.setOnClickListener(switchCameraListener);
timerValue = (TextView) findViewById(R.id.timer);
}
OnClickListener switchCameraListener = new OnClickListener() {
@Override
public void onClick(View v) {
// get the number of cameras
if (!recording) {
int camerasNumber = Camera.getNumberOfCameras();
if (camerasNumber > 1) {
// release the old camera instance
// switch camera, from the front and the back and vice versa
releaseCamera();
chooseCamera();
} else {
Toast toast = Toast.makeText(myContext, "Sorry, your phone has only one camera!", Toast.LENGTH_LONG);
toast.show();
}
}
}
};
public void chooseCamera() {
// if the camera preview is the front
if (cameraFront) {
int cameraId = findBackFacingCamera();
if (cameraId >= 0) {
// open the backFacingCamera
// set a picture callback
// refresh the preview
mCamera = Camera.open(cameraId);
// mPicture = getPictureCallback();
mPreview.refreshCamera(mCamera);
}
} else {
int cameraId = findFrontFacingCamera();
if (cameraId >= 0) {
// open the backFacingCamera
// set a picture callback
// refresh the preview
mCamera = Camera.open(cameraId);
// mPicture = getPictureCallback();
mPreview.refreshCamera(mCamera);
}
}
}
@Override
protected void onPause() {
super.onPause();
// when on Pause, release camera in order to be used from other
// applications
releaseCamera();
}
private boolean hasCamera(Context context) {
// check if the device has camera
if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
return true;
} else {
return false;
}
}
boolean recording = false;
OnClickListener captrureListener = new OnClickListener() {
@Override
public void onClick(View v) {
if (recording) {
String extension = MimeTypeMap.getFileExtensionFromUrl(mFileName);
String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
Intent mediaIntent = new Intent(Intent.ACTION_VIEW);
mediaIntent.setDataAndType(Uri.parse(mFileName), mimeType);
startActivityForResult(mediaIntent,CAMERA_CAPTURE_VIDEO_REQUEST_CODE);
// stop recording and release camera
mediaRecorder.stop(); // stop the recording
releaseMediaRecorder(); // release the MediaRecorder object
Toast.makeText(AndroidVideoCaptureExample.this, "Video captured!", Toast.LENGTH_LONG).show();
recording = false;
} else {
if (!prepareMediaRecorder()) {
Toast.makeText(AndroidVideoCaptureExample.this, "Fail in prepareMediaRecorder()!\n - Ended -", Toast.LENGTH_LONG).show();
finish();
}
// work on UiThread for better performance
runOnUiThread(new Runnable() {
public void run() {
// If there are stories, add them to the table
try {
mediaRecorder.start();
} catch (final Exception ex) {
// Log.i("---","Exception in thread");
}
}
});
recording = true;
}
startTime = SystemClock.uptimeMillis();
customHandler.postDelayed(updateTimerThread, 0);
}
};
private Runnable updateTimerThread = new Runnable() {
public void run() {
timeInMilliseconds = SystemClock.uptimeMillis() - startTime;
updatedTime = timeSwapBuff + timeInMilliseconds;
int secs = (int) (updatedTime / 1000);
int mins = secs / 60;
secs = secs % 60;
int milliseconds = (int) (updatedTime % 1000);
timerValue.setText("" + mins + ":"
+ String.format("%02d", secs));
customHandler.postDelayed(this, 0);
}
};
private void releaseCamera() {
// stop and release camera
if (mCamera != null) {
mCamera.release();
mCamera = null;
}
}
private void releaseMediaRecorder() {
if (mediaRecorder != null) {
mediaRecorder.reset(); // clear recorder configuration
mediaRecorder.release(); // release the recorder object
mediaRecorder = null;
mCamera.lock(); // lock camera for later use
}
}
private boolean prepareMediaRecorder() {
mediaRecorder = new MediaRecorder();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
String i = sdf.format(new Date());
mFileName = Environment.getExternalStorageDirectory().getAbsolutePath();
mFileName += "/audiorecordtest" + i + ".mp4";
mCamera.unlock();
mediaRecorder.setCamera(mCamera);
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mediaRecorder.setMaxDuration((int) maxduration);
mediaRecorder.setVideoSize(640, 480);
mediaRecorder.setVideoFrameRate(90);
mediaRecorder.setVideoEncodingBitRate(3000000);
// mediaRecorder.MEDIA_RECORDER_INFO_MAX_DURATION_REACHED()
mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
mediaRecorder.setOutputFile(mFileName);
try {
mediaRecorder.prepare();
} catch (IllegalStateException e) {
releaseMediaRecorder();
return false;
} catch (IOException e) {
releaseMediaRecorder();
return false;
}
mediaRecorder.setOnInfoListener(new MediaRecorder.OnInfoListener() {
@Override
public void onInfo(MediaRecorder mr, int what, int extra) {
if (what == MediaRecorder.MEDIA_RECORDER_INFO_MAX_DURATION_REACHED) {
String extension = MimeTypeMap.getFileExtensionFromUrl(mFileName);
String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
Intent mediaIntent = new Intent(Intent.ACTION_VIEW);
mediaIntent.setDataAndType(Uri.parse(mFileName), mimeType);
startActivityForResult(mediaIntent,CAMERA_CAPTURE_VIDEO_REQUEST_CODE);
mediaRecorder.stop();
if (mediaPlayer.isPlaying()) {
mediaPlayer.reset();
}
mediaPlayer.setDisplay((SurfaceHolder) mPreview);
try {
mediaPlayer.setDataSource(mFileName);
mediaPlayer.prepare();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mediaPlayer.start();
}
}
});
return true;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode==CAMERA_CAPTURE_VIDEO_REQUEST_CODE) {
videoUri = data.getData(); // get the uri of video from intent
launchUploadActivity1(true,videoUri);
//
// }
}
}
private void launchUploadActivity1(boolean isPlaying, Uri uri) {
Intent i = new Intent(AndroidVideoCaptureExample.this, SecondLast.class);
// i.setData(uri); // set the uri to intent
i.putExtra("play",videoUri);
startActivity(i);
}
}
第二项活动
public class SecondActivity extends Activity {
MediaPlayer mediaPlayer;
SurfaceView surfaceView;
SurfaceHolder surfaceHolder;
boolean pausing = false;;
Uri file;
VideoView video;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final Uri videoUri = getIntent().getData(); // do anything from the Uri
setContentView(R.layout.activity_second_last);
ImageView buttonPlayVideo = (ImageView) findViewById(R.id.play_button);
video = (VideoView) findViewById(R.id.video_view);
ImageView image = (ImageView) findViewById(R.id.image_view);
Intent i = getIntent();
file = Uri.parse(i.getExtras().get("play").toString());
video.setVideoURI(file);
video.start();
buttonPlayVideo.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
Intent i = getIntent();
file = Uri.parse(i.getExtras().get("play").toString());
video.setVideoURI(file);
video.start();
}});
}
}
//预览课程
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder mHolder;
private Camera mCamera;
MediaPlayer mediaPlayer;
public CameraPreview(Context context, Camera camera) {
super(context);
mCamera = camera;
mHolder = getHolder();
mHolder.addCallback(this);
// mHolder.setFixedSize(176, 144);
mediaPlayer = new MediaPlayer();
// deprecated setting, but required on Android versions prior to 3.0
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
public void surfaceCreated(SurfaceHolder holder) {
try {
// create the surface and start camera preview
if (mCamera == null) {
assert mCamera != null;
mCamera.setPreviewDisplay(holder);
mCamera.setDisplayOrientation(90);
mCamera.startPreview();
}
} catch (IOException e) {
Log.d(VIEW_LOG_TAG, "Error setting camera preview: " + e.getMessage());
}
}
public void refreshCamera(Camera camera) {
if (mHolder.getSurface() == null) {
// preview surface does not exist
return;
}
// stop preview before making changes
try {
mCamera.stopPreview();
} catch (Exception e) {
// ignore: tried to stop a non-existent preview
}
// set preview size and make any resize, rotate or
// reformatting changes here
// start preview with new settings
setCamera(camera);
try {
mCamera.setPreviewDisplay(mHolder);
mCamera.setDisplayOrientation(90);
mCamera.startPreview();
} catch (Exception e) {
Log.d(VIEW_LOG_TAG, "Error starting camera preview: " + e.getMessage());
}
}
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
// If your preview can change or rotate, take care of those events here.
// Make sure to stop the preview before resizing or reformatting it.
refreshCamera(mCamera);
}
public void setCamera(Camera camera) {
//method to set a camera instance
mCamera = camera;
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
mCamera.release();
mHolder.getSurface();
}
}
我目前的代码根据要求重新编码视频,并在录制后播放。但在第二项活动中,它什么也没做。
答案 0 :(得分:2)
在您的活动的onActivityResult方法中获取uri并将其发送到上传活动:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode==CAMERA_CAPTURE_VIDEO_REQUEST_CODE) {
launchUploadActivity1(true);
if (resultCode == RESULT_OK) {
// video successfully recorded
// launching upload activity
Uri videoUri = data.getData(); // get the uri of video from intent
launchUploadActivity1(true,videoUri);
}
}
}
private void launchUploadActivity1(boolean isPlaying, Uri uri) {
Intent i = new Intent(AndroidVideoCaptureExample.this, SecondLast.class);
i.setData(uri); // set the uri to intent
startActivity(i);
}
在您的SecondLast活动中
protected void onCreate(Bundle b){
super.onCreate(b);
Uri videoUri = getIntent().getData(); // do anything from the Uri
}