1通过意图来开启视频
package com.example.media_video_text;
import java.io.File;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void dian(View v){
// parse加载的是网络上的
// Uri uri = Uri.parse("http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4");
// Environment.getExternalStorageDirectory()是外部存储空间
// fromFile加载本地外部视频
Uri uri2 = Uri.fromFile(new File(Environment.getExternalStorageDirectory().getPath()+"/nvjing.mp4"));
Intent i = new Intent(Intent.ACTION_VIEW);
i.setDataAndType(uri2, "video/mp4");
startActivity(i);
//通过隐式意图开启要注意:加载的是网络还是本地
}
public void dian2(View b){
startActivity(new Intent(MainActivity.this,Main2Activity.class));
}
}
2在布局文件中声明VideoView控件
package com.example.media_video_text;
import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.MediaController;
import android.widget.VideoView;
public class Main2Activity extends Activity {
VideoView main_video;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
main_video=(VideoView) findViewById(R.id.main_video);
}
public void kaishi(View v){
main_video.setVideoURI(Uri.parse(Environment.getExternalStorageDirectory().getPath()+"/nvjing.mp4"));
// 设置视频控制条
main_video.setMediaController(new MediaController(this));
// 开始播放
main_video.start();
}
}
xml
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<VideoView
android:id="@+id/main_video"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
注意:如果不嵌套LinearLayout的话,他的操作栏就会在屏幕下面.(要明白视频的大小)
3用MediaPalyer需要
mediaPlayer.setDisplay(holder);// 设置显示和holder = mSurface.getHolder();// 活得SurfaceHolder
holder.addCallback(new Callback() {的设置监听
package com.example.media_video_text;
import java.io.IOException;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Environment;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
import android.view.View;
public class Main3Activity extends Activity {
private SurfaceView mSurface;
private SurfaceHolder holder;
private MediaPlayer mediaPlayer;
boolean isy = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
mSurface = (SurfaceView) findViewById(R.id.mSurfaceView);
holder = mSurface.getHolder();// 活得SurfaceHolder
holder.addCallback(new Callback() {
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// mSurface被销毁时
if (mediaPlayer != null) {
mediaPlayer.pause();
}
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
// mSurface被创建时
if (mediaPlayer != null) {
mediaPlayer.setDisplay(holder);// 设置显示
mediaPlayer.start();
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format,
int width, int height) {
// mSurface被改变时
}
});
}
public void dian(View v) {
if (isy == false) {
mediaPlayer = new MediaPlayer();
mediaPlayer.reset();
try {
mediaPlayer.setDataSource(Environment
.getExternalStorageDirectory().getPath()
+ "/nvjing.mp4");
mediaPlayer.prepare();
mediaPlayer.setDisplay(holder);// 设置显示
mediaPlayer.start();
} catch (IOException e) {
e.printStackTrace();
}
isy = true;
} else {
mediaPlayer.setDisplay(holder);// 设置显示
mediaPlayer.start();
}
}
public void pause(View v) {
if (mediaPlayer != null) {
mediaPlayer.pause();
}
}
public void stop(View v) {
if (mediaPlayer != null) {
mediaPlayer.stop();
mediaPlayer.release();
isy=false;
}
}
}
xml
<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"
tools:context="${relativePackage}.${activityClass}" >
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="dian"
android:text="播放" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/button2"
android:layout_marginTop="20dp"
android:text="暂停"
android:onClick="pause"
/>
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/button1"
android:layout_marginTop="44dp"
android:text="停止"
android:onClick="stop" />
<SurfaceView
android:id="@+id/mSurfaceView"
android:layout_width="wrap_content"
android:layout_height="200dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true" />
</RelativeLayout>