软件编程
位置:首页>> 软件编程>> Android编程>> 5步学会使用VideoView播放视频

5步学会使用VideoView播放视频

作者:TaooLee  发布时间:2023-09-12 05:51:07 

标签:VideoView,播放视频

我们可以试想ImageView能显示图片,而VideoView就是用来显示视频的。

使用VideoView播放视频的步骤如下

【1】在界面布局中定义VideoView


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical"
 tools:context=".MainActivity">

<VideoView
   android:id="@+id/videoview"
   android:layout_width="match_parent"
   android:layout_height="0dp"
   android:layout_weight="1"/>
 <Button
   android:id="@+id/button"
   android:text="播放"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content" />

</LinearLayout>

【2】调用如下两个方法加载指定视频

setVideoPath(String Path);加载路径下的视频
setVideoURL(URL url);加载url所对应的视频。


mVideoView.setVideoPath(Environment.getExternalStorageDirectory()+"/aa.mp4");

【3】权限


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

【4】调用

start()、stop()、pause()控制播放

【5】实际中常常结合MediaController类,它提供一个友好的图像控制界面控制视频播放;


mVideoView.setMediaController(new MediaController(MainActivity.this));

完整程序代码如下


public class MainActivity extends Activity {

private VideoView mVideoView;
 private Button mButton;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);

mVideoView= (VideoView) findViewById(R.id.videoview);
   mButton= (Button) findViewById(R.id.button);
   mButton.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View v) {

//得到sdcard下面aa.mp4的視頻文件
       //两种调用方式
//        File videofile =new File("/mut/extSdCard/DCIM/Camera/20150915_160202.mp4");
//        mVideoView.setVideoPath(videofile.getAbsolutePath());
       mVideoView.setVideoPath(Environment.getExternalStorageDirectory()+"/20150915_160202.mp4");
       mVideoView.setMediaController(new MediaController(MainActivity.this));
       mVideoView.start();
     }
   });
 }

}

来源:https://blog.csdn.net/TaooLee/article/details/48540793

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com