简体   繁体   English

Android Studio-视频应用程序崩溃-Videoview和Mediaplayer-无法播放录像

[英]Android Studio - Video application crashes - Videoview & Mediaplayer - Can not play recording

I have an application where I use a button to start the camera through intent to record video. 我有一个应用程序,在该应用程序中,我使用按钮来通过意图录制视频来启动相机。 This work fine and the application returns to mainactivity. 这项工作正常,应用程序返回到mainactivity。 When returned to mainactivity I would like last recorded video to be played in the VideoView. 返回mainactivity后,我希望最后一个录制的视频在VideoView中播放。 At this point my problem occur. 此时,我的问题出现了。

Im trying to follow the instructions on the link provided below, but something is obviously not correct. 我试图按照下面提供的链接上的说明进行操作,但显然不正确。

https://developer.android.com/training/camera/videobasics.html https://developer.android.com/training/camera/videobasics.html

Logcat says I need to use Mediaplayer. Logcat说我需要使用Mediaplayer。 Could someone help me out with some code? 有人可以帮我一些代码吗?

Thanks in advance! 提前致谢!

package com.example.t.videorecorder;

import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.VideoView;

public class MainActivity extends AppCompatActivity {

static final int REQUEST_VIDEO_CAPTURE=1;



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

public void startCamera(View view){
    Intent intent= new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
    if (intent.resolveActivity(getPackageManager())!=null){
        startActivityForResult(intent, REQUEST_VIDEO_CAPTURE);
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intentintent) {

    VideoView vW=(VideoView)findViewById(R.id.videoView);
    if (requestCode==REQUEST_VIDEO_CAPTURE && resultCode==RESULT_OK){
        Uri video= intent.getData();
        vW.setVideoURI(video);
    }
}
}

LogCat: logcat的:

11-09 12:43:12.894 27047-27047/com.example.t.videorecorder E/MediaPlayer[Native]: Unable to create media player
11-09 12:43:12.896 27047-27047/com.example.t.videorecorder W/VideoView: Unable to open content: content://media/external/video/media/33789
java.io.IOException: setDataSource failed.: status=0x80000000
at android.media.MediaPlayer.nativeSetDataSource(Native Method)
at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1061)
at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1050)
at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1004)
at android.widget.VideoView.openVideo(VideoView.java:353)
at android.widget.VideoView.access$2200(VideoView.java:72)
at android.widget.VideoView$7.surfaceCreated(VideoView.java:664)
at android.view.SurfaceView.updateWindow(SurfaceView.java:579)
at android.view.SurfaceView.onWindowVisibilityChanged(SurfaceView.java:238)
at android.view.View.dispatchWindowVisibilityChanged(View.java:8704)
at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1269)
at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1269)
at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1269)
at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1269)
at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1269)
at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1269)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1338)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1077)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5845)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
at android.view.Choreographer.doCallbacks(Choreographer.java:580)
at android.view.Choreographer.doFrame(Choreographer.java:550)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5272)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:909)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:704)

Here is the code to include the media Player: 以下是包含媒体播放器的代码:

vW.setMediaController(new MediaController(this));
vW.requestFocus();
vW.start();

And add the READ_EXTERNAL_STORAGE permission in the manifest 并在清单中添加READ_EXTERNAL_STORAGE权限

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

In android marshmallow go to settings then app then go to the app and in the permission allow the Storage Access permission. 在android棉花糖中,依次转到设置和应用,然后转到该应用,并在权限中授予存储访问权限。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM