`

Android之VideoView窗口/全屏播放

 
阅读更多

package com.FJICC.lzm;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.graphics.PixelFormat;
import android.media.MediaPlayer;
import android.net.Uri;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.MediaController;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.VideoView;


public class multimedia_player extends Activity  {
   private VideoView mVideoView01;
   private String strVideoPath = "";//视频存放的路径
   private String TAG = "HIPPO_VIDEOVIEW";
   private boolean fullscreen = false;//全屏/窗口播放切换标志
  
  
   /* 判断是否安装存储卡flagfalse */
   private boolean bIfSDExist = false;
  
 ////返回键按钮,返回上一级Activity
 @Override
 public void onBackPressed() {
  // TODO Auto-generated method stub
  super.onBackPressed();
  Intent i =new Intent();
  i.setClass(multimedia_player.this,multimedia.class);
  startActivity(i);
  finish();
 }

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  super.onCreate(savedInstanceState);
  setContentView(R.layout.multimedia_player);  
     /* 判断存储卡是否存在*/
     if(android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
     {
       bIfSDExist = true;
       showDialog(1);
     }
     else
     {
       bIfSDExist = false;
       mMakeTextToast
       (
         getResources().getText(R.string.str_err_nosd).toString(),
         true
       );
     }
    
     //获取VideoView对象
     mVideoView01 = (VideoView)findViewById(R.id.myVideoView1); 
     //设置视频播放器,准备函数
     mVideoView01.setOnPreparedListener(new MediaPlayer.OnPreparedListener()
     {
       @Override
       public void onPrepared(MediaPlayer mp)
       {
         // TODO Auto-generated method stub
       //  mTextView01.setText(strVideoPath);
         setTitle(strVideoPath.toString());
       }
     });
     //设置视频播放完毕动作,可以在内部设置是否重复播放
     mVideoView01.setOnCompletionListener(new MediaPlayer.OnCompletionListener()
     {
       @Override
       public void onCompletion(MediaPlayer arg0)
       {
         // TODO Auto-generated method stub
         mMakeTextToast
         (
           getResources().getText(R.string.str_complete).toString(),
           true
         );
       }
     });
   }
 
 //媒体播放动作函数
   private void playVideo(String strPath)
   {
     if(strPath!="")
     {      
      /*调用VideoURI方法,指定解析路径 */
      mVideoView01.setVideoURI(Uri.parse(strPath));
      
       /* 设置控制Bar,显示在Context中*/
      mVideoView01.setMediaController(new MediaController(multimedia_player.this));
      mVideoView01.requestFocus();
      
       /*调用VideoView.start()自动播放*/
      mVideoView01.start();
       if(mVideoView01.isPlaying())
       {
         /* start()后,仍需要preparing() */
         //mTextView01.setText("Now Playing:"+strPath);
        setTitle(strVideoPath.toString());
         Log.i(TAG, strPath);
       }
     }
   }
  
   /**媒体重新从头播放函数  */
   private void resetVideo()
   {
     if(mVideoView01!=null)
     {
       mVideoView01.seekTo(0);
     }
   }
 
   //提示消息Toast弹出消息函数
   public void mMakeTextToast(String str, boolean isLong)
   {
     if(isLong==true)
     {
       Toast.makeText(multimedia_player.this, str, Toast.LENGTH_LONG).show();
     }
     else
     {
       Toast.makeText(multimedia_player.this, str, Toast.LENGTH_SHORT).show();
     }
   }

  
   //添加OptinMenu函数
 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // TODO Auto-generated method stub
  //参数说明:menu.add((int groupId, int itemId, int order, charsequence title).setIcon(drawable ID)
//    1、组别,如果不分组的话就写Menu.NONE,
//       2、Id,这个很重要,Android根据这个Id来确定不同的菜单
//       3、顺序,哪个菜单项在前面由这个参数的大小决定
//       4、文本,菜单项的显示文本
  menu.add(0,2,0,"输入文件名");
  menu.add(0,0,1,"返回");//
     menu.add(0,1,2,"播放视频");
     menu.add(0,3,3,"全屏/窗口切换");
     
  return super.onCreateOptionsMenu(menu);
  
 }

 @Override
 public boolean onOptionsItemSelected(MenuItem item) {
  // TODO Auto-generated method stub
  
  switch(item.getItemId())//菜单序号
     {
      case 0://返回上级
      {
       ////返回上一级Activity
       Intent i =new Intent();
       i.setClass(multimedia_player.this,multimedia.class);
       startActivity(i);
       finish();       
      }
   break;
      case 1://播放视频
       {
        playVideo(strVideoPath);
       }
       break;
      case 2://输入播放的文件名称 在SD卡的/DIMC文件夹下;或者输入网络视频地址
      {
       showDialog(0); 
      }
   break;
      case 3://全屏/窗口切换
      {
       if(!fullscreen){//设置RelativeLayout的全屏模式
        RelativeLayout.LayoutParams layoutParams=
               new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_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);
            mVideoView01.setLayoutParams(layoutParams);
           
            fullscreen = true;//改变全屏/窗口的标记
        }else{//设置RelativeLayout的窗口模式
           RelativeLayout.LayoutParams lp=new  RelativeLayout.LayoutParams(320,240);
           lp.addRule(RelativeLayout.CENTER_IN_PARENT);
             mVideoView01.setLayoutParams(lp);
             fullscreen = false;//改变全屏/窗口的标记

        }   
      }
      break;
     }     
 //    return true;
    return super.onOptionsItemSelected(item);
 }

 @Override
 protected Dialog onCreateDialog(int id) {
  // TODO Auto-generated method stub
  final View v = View.inflate(this, R.layout.multimedia_set, null);
  final EditText videoPath = (EditText) v.findViewById(R.id.videoPath);
  switch(id){
  case 0:
  {
   Dialog dialog0 = new AlertDialog.Builder(this)
   .setTitle("视频文件名称")
   .setView(v).setMessage("请输入视频文件名称")
   .setPositiveButton("确定",
     new DialogInterface.OnClickListener() {
      @Override
      public void onClick(DialogInterface arg0, int arg1) {
       // 播放本地视频
             //视频文件放置在SD卡下的DCIM目录下,这里设置为默认路径
             //resetVideo();//重头播放,这个可以在播放完了后,执行,就可以实现循环播放了
       strVideoPath= "file:///sdcard/DCIM/"+videoPath.getText().toString();//此时只需输入文件名称如:test.3gp;
            
             //播放网络视频
             //播放网络视频需要 输 入 视 频  网 络 地 址://在权限中需加入 INTERNET权限和WAKE_LOCK权限
             //http://www.baudu.media.ooxx/test.mp4
             //strVideoPath=pathTV.getText().toString();//此时输入的为 视频(3gp/mp4/H.264)网络地址
       
       setTitle("Playing" + strVideoPath);
      }
     }).create();
   return dialog0;
  }
  case 1:{
   videoPath.setText(" ");
   videoPath.setEnabled(false);
   Dialog dialog1 = new AlertDialog.Builder(this)
   .setTitle("播放器提示").setMessage("请点击Option键进行视频载入")
   .setView(v)
   .setPositiveButton("确定",
     new DialogInterface.OnClickListener() {
      @Override
      public void onClick(DialogInterface arg0, int arg1) {
       
      }
     }).create();
   return dialog1;
  }
  default:
   return null;
   
  }
  
  
 } 
 }

 

 

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="0.23" >
  <VideoView
            android:id="@+id/myVideoView1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
             android:layout_alignParentRight="true"
              android:layout_alignParentBottom="true"/>

</RelativeLayout>

分享到:
评论
1 楼 hsxwtogether 2014-09-23  
全屏有用,给力

相关推荐

Global site tag (gtag.js) - Google Analytics