編輯:關於Android編程
1、效果展示

2、布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="app.com.myapplication.LocalVideoActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="200dp">
<VideoView
android:id="@+id/videoView"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>
</RelativeLayout>
3、代碼部分
package app.com.myapplication;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.widget.MediaController;
import android.widget.Toast;
import android.widget.VideoView;
public class LocalVideoActivity extends AppCompatActivity {
private VideoView videoView ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_local_video);
//本地的視頻 需要在手機SD卡根目錄添加一個 fl1234.mp4 視頻
String videoUrl1 = Environment.getExternalStorageDirectory().getPath()+"/fl1234.mp4" ;
//網絡視頻
String videoUrl2 = Utils.videoUrl ;
Uri uri = Uri.parse( videoUrl2 );
videoView = (VideoView)this.findViewById(R.id.videoView );
//設置視頻控制器
videoView.setMediaController(new MediaController(this));
//播放完成回調
videoView.setOnCompletionListener( new MyPlayerOnCompletionListener());
//設置視頻路徑
videoView.setVideoURI(uri);
//開始播放視頻
videoView.start();
}
class MyPlayerOnCompletionListener implements MediaPlayer.OnCompletionListener {
@Override
public void onCompletion(MediaPlayer mp) {
Toast.makeText( LocalVideoActivity.this, "播放完成了", Toast.LENGTH_SHORT).show();
}
}
}
package app.com.myapplication;
/**
* Created by ${zyj} on 2016/4/19.
*/
public class Utils {
public static final String videoUrl = "http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4" ;
}
4、總結
Android使用VideoView播放本地視頻和網絡視頻的方法到這就基本結束了,希望對大家開發Android能有所幫助。如果有疑問可以留言討論。
Android Studio使用教程(五):Gradle命令詳解和導入第三方包
Android Studio + Gradle的組合用起來非常方便,很多第三方開源項目也早都遷移到了Studio,為此今天就來介紹下查看、編譯並導入第三方開源項目的方法。
Android 01 Service 介紹
概要 若讀者之前沒接觸過service,對下面內容有個大致了解即可。待使用過service之後再來閱讀本章內容,會理解更深刻。
你值得擁有的Android Studio開發小技巧
上一次發過了一個介紹Studio的,這裡再發一個補充下。我們都知道,Android Studio的功能是非常強大的,也是很智能的。如果有人告訴你學Android開發要用命
Windows下搭建Android開發環境
工具/原料JDKEclipseADTJDK的安裝和Java環境變量的設置1、JDK下載地址 JDK下載地址:http://www.or