当前位置:网站首页 > 更多 > 编程开发 > 正文

[安卓] Android VideoView播放视频

作者:CC下载站 日期:2020-10-15 00:00:00 浏览:51 分类:编程开发

今天的任务是

所以先来记录一下VideoView

经过一番操作之后,视频总算是放出来了

而且这个只是本地的视频,我播放网络的视频总是出现

然后报错

2020-10-15 10:07:12.914 11984-11984/cn.lanol.studykongjian V/MediaHTTPService: MediaHTTPService([email protected]): Cookies: null

2020-10-15 10:07:12.916 11984-11984/cn.lanol.studykongjian E/MediaPlayerNative: Unable to create media player

2020-10-15 10:07:12.918 11984-11984/cn.lanol.studykongjian W/VideoView: Unable to open content: https://pan.lanol.cn/视频地址.mp4

java.io.IOException: setDataSource failed.: status=0x80000000

at android.media.MediaPlayer.nativeSetDataSource(Native Method)

at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1175)

at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1162)

at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1079)

at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1100)

at android.widget.VideoView.openVideo(VideoView.java:412)

at android.widget.VideoView.access$2200(VideoView.java:83)

at android.widget.VideoView$7.surfaceCreated(VideoView.java:694)

at android.view.SurfaceView.updateSurface(SurfaceView.java:1153)

at android.view.SurfaceView.lambda$new$0$SurfaceView(SurfaceView.java:173)

at android.view.-$$Lambda$SurfaceView$w68OV7dB_zKVNsA-r0IrAUtyWas.onPreDraw(Unknown Source:2)

at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:1093)

at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:3089)

at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1952)

at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:8171)

at android.view.Choreographer$CallbackRecord.run(Choreographer.java:972)

at android.view.Choreographer.doCallbacks(Choreographer.java:796)

at android.view.Choreographer.doFrame(Choreographer.java:731)

at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:957)

at android.os.Handler.handleCallback(Handler.java:938)

at android.os.Handler.dispatchMessage(Handler.java:99)

at android.os.Looper.loop(Looper.java:223)

at android.app.ActivityThread.main(ActivityThread.java:7656)

at java.lang.reflect.Method.invoke(Native Method)

at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)

at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

2020-10-15 10:07:12.918 11984-11984/cn.lanol.studykongjian D/VideoView: Error: 1,0

于是暂时先放弃了,反正比赛也是无网环境。

起初播放本地视频也是如此,但后来发现是因为权限的原因,所以需要在AndroidManifest.xml中加入

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

然后还需要在Activity中动态申请权限

intpermission=ActivityCompat.checkSelfPermission(videoVIew.this,
Manifest.permission.WRITE_EXTERNAL_STORAGE);
if(permission!=PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(videoVIew.this,PERMISSIONS_STORAGE,
REQUEST_EXTERNAL_STORAGE);
video.start();
}

然后在提示授权的时候点击允许

然后就没什么好说的了,这是Activity全部代码

packagecn.lanol.studykongjian;

importandroid.Manifest;
importandroid.content.pm.PackageManager;
importandroid.os.Bundle;
importandroid.os.Environment;
importandroid.widget.MediaController;
importandroid.widget.VideoView;

importandroidx.appcompat.app.AppCompatActivity;
importandroidx.core.app.ActivityCompat;

publicclassvideoVIewextendsAppCompatActivity{
privatefinalintREQUEST_EXTERNAL_STORAGE=1;
privateString[]PERMISSIONS_STORAGE={
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE};
@Override
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video_view);
//设置视频地址
StringvideoUrl1=Environment.getExternalStorageDirectory().getPath()+"/onceok.mp4";
//找到视频控件进行一系列设置
VideoViewvideo=findViewById(R.id.video);
video.setMediaController(newMediaController(this));
video.setVideoPath(videoUrl1);
intpermission=ActivityCompat.checkSelfPermission(videoVIew.this,
Manifest.permission.WRITE_EXTERNAL_STORAGE);
if(permission!=PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(videoVIew.this,PERMISSIONS_STORAGE,
REQUEST_EXTERNAL_STORAGE);
video.start();
}
}
}

-------------------------------------------------------------------

播放URL视频提示Can't play this video的问题解决了,因为没有加网络权限,所以需要在AndroidManifest.xml中加入

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

然后就是设置播放URL

Uriuri=Uri.parse("https://pan.lanol.cn/播放地址.mp4")
video.setVideoURI(uri);

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯