安卓VideoView播放视频(raw下)

版权声明:转载请联系博主、标明出处 https://blog.csdn.net/jyfbug/article/details/89024991

继音频之后,这次又用到视频,那就写一个视频的教程吧。
1、首先写一个layout。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <VideoView
        android:id="@+id/video_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>


2、定义videoView和设置路径

private VideoView videoView;
videoView = (VideoView)findViewById(R.id.video_view);
videoView.setVideoURI(Uri.parse("android.resource://com.example.jy.myapplication/"+R.raw.xx));//路径


这里我是把文件打包在raw下面的,也可以替换成网络上的路径。

3、设置播放和停止。

videoView.start();//播放
videoView.pause();//暂停
videoView.resume();//从头播放
videoView.suspend();//暂停


PS:遇到问题代码:MediaPlayer: Error (1,-2147483648)。原因是视频格式不对,换了一个视频就解决了。

猜你喜欢

转载自blog.csdn.net/jyfbug/article/details/89024991