Android 视频播放有三种方法:
1. 调用系统的播放器
2. SurfaceView + MediaPlayer
3. VideoView + MidiaController
每种方法本质是一样的,就是实现的方法有所不同,首先介绍VideoView + MediaController的方法:
先说缺点,如果用系统默认的VideoView, 播放视频的时候,一些小分辨率的视频无法填充到整个窗口,这就导致,屏幕一侧会有一条白边。视频无法填充到整个屏幕。
所以,如果你对这个很在意,那么只能自定义一个VideoView,下面我来介绍如何实现。
整个工程有这么5个文件:
TTPlayer.class // APK的主界面, 里面之定义了3个按钮,其中一个按钮是用来启动另外一个activity来播放视频,其他2个在本例中没有用。
activity_ttplayer.xml // TTPlayer.class 的布局
CustomVideoView.class // 自定义的VidwoView, 用来实现视频的全屏
PlayWindowVideoV.class //承载视频播放的Activity,我们自定义的VideoView要画在这上面。
activity_play_window_video_v.xml //PlayWindowVideoV.class 的布局文件
AndroidManifest.xml //不用说了
下面贴代码:
TTPlayer.class
package com.goafter.testtemp;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
public class TTPlayer extends AppCompatActivity {
Button btnPlay, btnPause, btnStop;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ttplayer);
btnPlay = (Button) findViewById(R.id.btnPlay);
btnPause = (Button) findViewById(R.id.btnPause);
btnStop = (Button) findViewById(R.id.btnStop);
btnPlay.setOnClickListener(new myclicklistener());
btnPause.setOnClickListener(new myclicklistener());
btnStop.setOnClickListener(new myclicklistener());
}
class myclicklistener implements View.OnClickListener {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnPlay:
Intent intent = new Intent(TTPlayer.this, PlayWindow.class);
Bundle bundle = new Bundle();
bundle.putString("uri", "file:///sdcard/video1.mp4");
intent.putExtras(bundle);
startActivity(intent);
break;
case R.id.btnPause:
Intent intentv = new Intent(TTPlayer.this, PlayWindowVideoV.class);
Bundle bundlev = new Bundle();
bundlev.putString("uri", "file:///sdcard/video1.mp4");
intentv.putExtras(bundlev);
startActivity(intentv);
break;
default:
break;
}
}
}
}
activity_ttplayer.xml
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.goafter.testtemp.TTPlayer">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/btnPlay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SurfaceView" />
<Button
android:id="@+id/btnPause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="VideoView" />
<Button
android:id="@+id/btnStop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="STOP" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
CustomVideoView.class ( 这段代码是网上摘抄的)
package com.goafter.testtemp;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.VideoView;
public class CustomVideoView extends VideoView {
private int mVideoWidth;
private int mVideoHeight;
public CustomVideoView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public CustomVideoView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
public CustomVideoView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// TODO Auto-generated method stub
// Log.i("@@@@", "onMeasure");
//下面的代码是让视频的播放的长宽是根据你设置的参数来决定
int width = getDefaultSize(mVideoWidth, widthMeasureSpec);
int height = getDefaultSize(mVideoHeight, heightMeasureSpec);
setMeasuredDimension(width, height);
}
}
package com.goafter.testtemp;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.MediaController;
public class PlayWindowVideoV extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);//去掉信息栏
Window window = getWindow();
WindowManager.LayoutParams params = window.getAttributes();
params.systemUiVisibility = View.SYSTEM_UI_FLAG_LOW_PROFILE;
window.setAttributes(params);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play_window_video_v);
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
CustomVideoView videoV = (CustomVideoView) findViewById(R.id.videoV);
videoV.setVideoPath(bundle.getString("uri"));
MediaController mController = new MediaController(this);
videoV.setMediaController(mController);
videoV.start();
}
}
<?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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.goafter.testtemp.PlayWindowVideoV">
<com.goafter.testtemp.CustomVideoView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/videoV"/>
</LinearLayout>