MainActivity.java:
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private Button play,pause,stop,videoButton;
private VideoView videoView;
private MediaPlayer mediaPlayer;
private boolean fullscreen;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
play=(Button)findViewById(R.id.button);
pause=(Button)findViewById(R.id.button1);
stop=(Button)findViewById(R.id.button2);
videoView=(VideoView)findViewById(R.id.video_view);
videoButton=(Button)findViewById(R.id.videoButton);
play.setOnClickListener(this);
pause.setOnClickListener(this);
stop.setOnClickListener(this);
videoButton.setOnClickListener(this);
initMediaPlayer(); //初始化MediaPlayer
}
private void initMediaPlayer(){
try {
mediaPlayer=MediaPlayer.create(this,R.raw.be_wth_you);
mediaPlayer.prepare();
}catch (IllegalStateException e){
e.printStackTrace();
} catch (IOException e){
e.printStackTrace();
}
}
//实现全屏
public void fullScreen1(){
if(!fullscreen){
RelativeLayout.LayoutParams layoutParams=
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
videoView.setLayoutParams(layoutParams);
fullscreen = true;//改变全屏/窗口的标记
}else{
RelativeLayout.LayoutParams lp=new RelativeLayout.LayoutParams(320,240);
lp.addRule(RelativeLayout.CENTER_IN_PARENT);
videoView.setLayoutParams(lp);
fullscreen=false;
}
}
public void startVideo(){
fullScreen1();//实现全屏
videoView.setMediaController(new MediaController(this)); //控制播放与暂停的控件
//Uri是统一资源标识符,作用:根据这个Uri找到某个资源文件,实际上就是生成了一个路径
Uri rawUri=Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.waiting_for_love);
videoView.setVideoURI(rawUri);
videoView.start();
}
@Override
public void onClick(View v){
switch (v.getId()){
case R.id.button:
if(!mediaPlayer.isPlaying()){
if(!videoView.isPlaying()){
Toast.makeText(MainActivity.this,"play",Toast.LENGTH_SHORT).show();
mediaPlayer.start(); //开始播放
}else{
videoView.pause();
mediaPlayer.start();
}
}
break;
case R.id.button1:
if(mediaPlayer.isPlaying()){
mediaPlayer.pause(); //暂停播放
}
break;
case R.id.button2:
if(mediaPlayer.isPlaying()){
mediaPlayer.reset(); //停止播放
initMediaPlayer();
}
break;
case R.id.videoButton:
if(!videoView.isPlaying()){
if(!mediaPlayer.isPlaying()){
startVideo();
}else{
mediaPlayer.reset();
initMediaPlayer();
startVideo();
}
}
break;
default:
break;
}
}
@Override
protected void onDestroy(){
super.onDestroy();
if(mediaPlayer!=null){
mediaPlayer.stop();
mediaPlayer.release();
}
if(videoView!=null){
videoView.suspend(); //将VideoView所占用的资源释放掉
}
}
}
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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/play"
android:id="@+id/button"
android:textAllCaps="false"
android:layout_weight="1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pause"
android:id="@+id/button1"
android:layout_weight="1"
android:textAllCaps="false"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Stop"
android:id="@+id/button2"
android:layout_weight="2"
android:textAllCaps="false"/>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<VideoView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:id="@+id/video_view"/>
</RelativeLayout>
<Button
android:id="@+id/videoButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAllCaps="false"
android:text="Start video"/>
</LinearLayout>
AndroidMainfest.xml:
android:screenOrientation="sensor" //实现屏幕的横屏竖屏切换,
有以下值可选:
"unspecified"- 默认值. 由系统选择显示方向. 在不同的设备可能会有所不同.
"landscape"- 横向
"portrait"- 纵向
"user"- 用户当前的首选方向
"behind"- 与在活动堆栈下的活动相同方向
"sensor"- 根据物理方向传感器确定方向. 取决于用户手持的方向, 当用户转动设备, 它跟随改变.
"nosensor"- 不经物理方向传感器确定方向. 该传感器被忽略, 所以当用户转动设备, 显示不会跟随改变. 除了这个区别,系统选择使用相同的政策取向对于“未指定”设置. 系统根据“未指定”("unspecified")设定选择相同显示方向.