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")設定選擇相同顯示方向.