android呼叫視訊介面
阿新 • • 發佈:2019-01-08
//1.MainActivity.java
package com.example.dell.video; import android.app.Activity; import android.media.MediaPlayer; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.MediaController;import android.widget.VideoView; public class MainActivity extends Activity { private VideoView video; private EditText et; private Button btn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); }private void initView() { et = (EditText) findViewById(R.id.et1); video = (VideoView) findViewById(R.id.video); btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { MediaController mediaController= new MediaController(MainActivity.this); //本地視訊 //String uri = "android.resource://" + getPackageName() + "/" + R.raw.big_buck_bunny; //網路視訊 需要開通網路訪問許可權 String uri = "http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"; video.setVideoURI(Uri.parse(uri)); video.setMediaController(mediaController); mediaController.setMediaPlayer(video); video.requestFocus(); video.start(); } }); } }
//2.新增網路許可 <uses-permission android:name="android.permission.INTERNET" />
//3.xml檔案 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main2" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.video.Main2Activity" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <EditText android:id="@+id/et1" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="3" android:hint="請輸入檔名"/> <Button android:id="@+id/btn" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:text="確定"/> </LinearLayout> <VideoView android:id="@+id/video" android:layout_width="match_parent" android:layout_height="300dp" android:layout_marginTop="10dp"/> </LinearLayout>