温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

5步学会使用VideoView播放视频

发布时间:2020-09-06 08:29:06 来源:脚本之家 阅读:149 作者:TaooLee 栏目:移动开发

我们可以试想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();
      }
    });
  }


}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持亿速云。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI