Android VideoView播放视频

Lan
Lan
2020-10-15 / 0 评论 / 1,136 阅读 / 正在检测是否收录...
温馨提示:
本文最后更新于2020年10月15日,已超过1533天没有更新,若内容或图片失效,请留言反馈。

今天的任务是

image.png

所以先来记录一下VideoView

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

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

然后报错

2020-10-15 10:07:12.914 11984-11984/cn.lanol.studykongjian V/MediaHTTPService: MediaHTTPService(android.media.MediaHTTPService@37be89c): 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://gitlab.com/Vastsa/lanpicbed/-/raw/master/视频地址.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-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

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

int permission = 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();
}

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

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

package cn.lanol.studykongjian;

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

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

public class videoVIew extends AppCompatActivity {
    private  final int REQUEST_EXTERNAL_STORAGE = 1;
    private  String[] PERMISSIONS_STORAGE = {
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_view);
        //设置视频地址
        String videoUrl1 = Environment.getExternalStorageDirectory().getPath()+"/onceok.mp4" ;
        //找到视频控件进行一系列设置
        VideoView video = findViewById(R.id.video);
        video.setMediaController(new MediaController(this));
        video.setVideoPath(videoUrl1);
        int permission = 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-permission android:name="android.permission.INTERNET" />

然后就是设置播放URL

Uri uri = Uri.parse("https://gitlab.com/Vastsa/lanpicbed/-/raw/master/播放地址.mp4")
video.setVideoURI(uri);

image.png

0

评论 (0)

取消