使用VideoView播放App中的資原始檔
阿新 • • 發佈:2019-01-08
佈局檔案
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<VideoView
android:id="@+id/vv_video"
android:layout_width ="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
先在res下新建一個資料夾raw,然後將視訊複製到該資料夾下面。
具體實現程式碼
public class VideoActivity extends Activity{
VideoView videoView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video);
initView();
}
public void initView(){
videoView= (VideoView) findViewById(R.id.vv_video);
playVideo();
}
public void playVideo(){
//String file=Environment.getExternalStorageDirectory().getPath()+"/oppo.3gp";//oppo.3gp視訊播放的名字
String uri = "android.resource://" + getPackageName() + "/" + R.raw.oppo;
videoView.setVideoURI(Uri.parse(uri));
MediaController mc = new MediaController(this);
//設定控制器 控制的是那一個videoview
mc.setAnchorView(videoView);
//設定videoview的控制器為mc
videoView.setMediaController(mc);
videoView.start();
}
}