我們可以試想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的
主站蜘蛛池模板:
吐鲁番市|
靖西县|
鹤壁市|
旬邑县|
德庆县|
社旗县|
伊金霍洛旗|
宜阳县|
蓝山县|
鄂托克前旗|
乌苏市|
治多县|
阜康市|
乌什县|
大姚县|
盐亭县|
昌邑市|
酒泉市|
观塘区|
沙坪坝区|
天全县|
陈巴尔虎旗|
岑巩县|
四平市|
利川市|
宜兰县|
唐河县|
丰都县|
驻马店市|
九龙坡区|
沾益县|
遂昌县|
绍兴县|
阿尔山市|
二连浩特市|
分宜县|
江安县|
哈巴河县|
韩城市|
龙里县|
开化县|