Search code examples
androidandroid-cameraandroid-videoviewandroid-camera-intent

Nexus6 Can't play this video


I am following this [example][1] in order to understand how I can work with Android video camera.

The code for my activity is just a Button and a VideoView. After tap on the button I record a video and then, after stop the recording, the video recorded is visible on the VideoView. The code works perfectly on a Galaxy S2(api16) and on an Huawei L21(api 22) but on a Motorola Nexus 6(api23) I am facing this error

Can not play this video

This is my activity file:

public class MainActivity extends AppCompatActivity {

@Bind(R.id.button)
Button button;

@Bind(R.id.videoView)
VideoView videoView;

private Uri fileUri;
public static final int MEDIA_TYPE_VIDEO = 2;
private static final int CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE = 200;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ButterKnife.bind(this);
}

@OnClick(R.id.button)
protected void startRecording() {
    launchCamera();
}

private void launchCamera() {
    Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
    fileUri = getOutputMediaFileUri(MEDIA_TYPE_VIDEO);  // create a file to save the video
    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);  // set the image file name
    // set the video image quality to high
   startActivityForResult(intent, CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE);
}

private  Uri getOutputMediaFileUri(int type) {
    Uri myUri =  Uri.fromFile(getOutputMediaFile(type));
    Log.d("TAG","uri we have is "+myUri);
    return myUri ;
}

private  File getOutputMediaFile(int type){

    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_MOVIES), "MyCameraApp");

    if (! mediaStorageDir.exists()){
        if (! mediaStorageDir.mkdirs()){
            Log.d("MyCameraApp", "failed to create directory");
            return null;
        }
    }

    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    File mediaFile;

        mediaFile = new File(mediaStorageDir.getPath() + File.separator +
                "VID_"+ timeStamp + ".mp4");

    return mediaFile;
}


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            videoView.setVideoURI(fileUri);
            videoView.start();
}

}

And the Log I am getting with Nexus6

 W/VideoView: Unable to open content: file:///storage/emulated/0/Movies/MyCameraApp/VID_20160503_132541.mp4
                                                                         java.io.IOException: setDataSource failed.
                                                                             at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1096)
                                                                             at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1042)
                                                                             at android.media.MediaPlayer.setDataSource(MediaPlayer.java:991)
                                                                             at android.widget.VideoView.openVideo(VideoView.java:348)
                                                                             at android.widget.VideoView.-wrap0(VideoView.java)
                                                                             at android.widget.VideoView$7.surfaceCreated(VideoView.java:624)
                                                                             at android.view.SurfaceView.updateWindow(SurfaceView.java:595)
                                                                             at android.view.SurfaceView.onWindowVisibilityChanged(SurfaceView.java:243)
                                                                             at android.view.View.dispatchWindowVisibilityChanged(View.java:10214)

  [1]: http://developer.android.com/intl/es/guide/topics/media/camera.html

Any idea why does not work on Nexus 6?


Solution

  • Fixed after update the Camera version app to 3.2.045. It was a issue in the Camera App itself not in the code I posted.