android am怎样通过脚本播放视频,5步学会使用VideoView播放视频

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

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

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

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

tools:context=".MainActivity">

android:id="@+id/videoview"

android:layout_width="match_parent"

android:layout_height="0dp"

android:layout_weight="1"/>

android:id="@+id/button"

android:text="播放"

android:layout_width="wrap_content"

android:layout_height="wrap_content" />

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

setVideoPath(String Path);加载路径下的视频

setVideoURL(URL url);加载url所对应的视频。

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

【3】权限

【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();

}

});

}

}

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值