/**
* 字幕,调整视频缩放类型
* @author 白乾涛
*/
public class VideoViewSubtitle extends Activity implements OnPreparedListener, OnClickListener, OnTimedTextListener {
//HTTP,无效
//path= "http://www.modrails.com/videos/passenger_nginx.mov";
//path= "http://wsmp32.bbc.co.uk/";
//RTSP,无效
//path= "http://m.livestream.com";
//path= "rtsp://xgrammyawardsx.is.livestream-api.com/livestreamiphone/grammyawards";
//MMS,无效
//path= "mms://112.230.192.196/zb12";
//path = "mms://ting.mop.com/mopradio";
//组播 ,无效
//path = "udp://236.1.0.1:2000";
private String path = "http://dlqncdn.miaopai.com/stream/MVaux41A4lkuWloBbGUGaQ__.mp4";
private String subtitle_path = "";
private VideoView mVideoView;
private TextView mSubtitleView;
private Button btn_changeLayout;
private Button bt_play1, bt_play2, bt_play3, bt_play4;
private long mPosition = 0;
private int mVideoLayout = 0;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
Vitamio.isInitialized(getApplicationContext());
setContentView(R.layout.subtitle2);
mVideoView = (VideoView) findViewById(R.id.surface_view);
btn_changeLayout = (Button) findViewById(R.id.btn_changeLayout);
bt_play1 = (Button) findViewById(R.id.bt_play1);
bt_play2 = (Button) findViewById(R.id.bt_play2);
bt_play3 = (Button) findViewById(R.id.bt_play3);
bt_play4 = (Button) findViewById(R.id.bt_play4);
btn_changeLayout.setOnClickListener(this);
bt_play1.setOnClickListener(this);
bt_play2.setOnClickListener(this);
bt_play3.setOnClickListener(this);
bt_play4.setOnClickListener(this);
mVideoView.setVideoPath(path);
mVideoView.setMediaController(new MediaController(this));
mVideoView.requestFocus();
mVideoView.setOnPreparedListener(this);
mVideoView.setOnTimedTextListener(this);
}
@Override
protected void onPause() {
mPosition = mVideoView.getCurrentPosition();
mVideoView.stopPlayback();
super.onPause();
}
@Override
protected void onResume() {
if (mPosition > 0) {
mVideoView.seekTo(mPosition);
mPosition = 0;
}
super.onResume();
mVideoView.start();
}
@Override
public void onPrepared(MediaPlayer mp) {
mp.setPlaybackSpeed(1.0f);
mVideoView.addTimedTextSource(subtitle_path);//不知道哪来的API
mVideoView.setTimedTextShown(true);//不知道哪来的API
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.btn_changeLayout) {
changeLayout(v);
} else {
switch (v.getId()) {
case R.id.bt_play1:
path = "http://112.253.22.157/17/z/z/y/u/zzyuasjwufnqerzvyxgkuigrkcatxr/hc.yinyuetai.com /D046015255134077DDB3ACA0D7E68D45.flv";
break;
case R.id.bt_play2://HLS m3u8
path = "http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8";
break;
case R.id.bt_play3:
path = "http://7xt0mj.com1.z0.glb.clouddn.com/xia.v.1280.720.f4v";
break;
case R.id.bt_play4:
path = "http://7xt0mj.com1.z0.glb.clouddn.com/lianaidaren.v.640.480.mp4";
break;
}
mVideoView.setVideoPath(path);
mVideoView.requestFocus();
mVideoView.start();
}
}
@Override
public void onTimedText(String text) {
mSubtitleView.setText(text);
}
@Override
public void onTimedTextUpdate(byte[] pixels, int width, int height) {
}
public void changeLayout(View view) {
mVideoLayout++;
if (mVideoLayout == 4) mVideoLayout = 0;//循环
switch (mVideoLayout) {
case 0:
mVideoLayout = VideoView.VIDEO_LAYOUT_ORIGIN;//原始画面
view.setBackgroundResource(R.drawable.mediacontroller_sreen_size_100);
break;
case 1:
mVideoLayout = VideoView.VIDEO_LAYOUT_SCALE;//全屏
view.setBackgroundResource(R.drawable.mediacontroller_screen_fit);
break;
case 2:
mVideoLayout = VideoView.VIDEO_LAYOUT_STRETCH;//拉伸
view.setBackgroundResource(R.drawable.mediacontroller_screen_size);
break;
case 3:
mVideoLayout = VideoView.VIDEO_LAYOUT_ZOOM;//裁剪
view.setBackgroundResource(R.drawable.mediacontroller_sreen_size_crop);
break;
}
mVideoView.setVideoLayout(mVideoLayout, 0);//第二个参数为【宽高比】,为0将自动检测
}
}
<?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" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="400dp"
android:layout_gravity="center_horizontal"
android:orientation="vertical" >
<io.vov.vitamio.widget.CenterLayout
android:id="@+id/dd"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<io.vov.vitamio.widget.VideoView
android:id="@+id/surface_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
</io.vov.vitamio.widget.CenterLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="@string/res_audio" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/bt_play1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="flv" />
<Button
android:id="@+id/bt_play2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="m3u8" />
<Button
android:id="@+id/bt_play3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="f4v" />
<Button
android:id="@+id/bt_play4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="mp4" />
<Button
android:id="@+id/btn_changeLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/mediacontroller_sreen_size_100" />
</LinearLayout>
</LinearLayout>