zoukankan      html  css  js  c++  java
  • Android VideoView

    布局文件

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:orientation="vertical" android:layout_width="fill_parent"
     android:layout_height="fill_parent">
     <VideoView android:id="@+id/videoview" android:layout_width="fill_parent"
      android:layout_height="fill_parent" />
     <SurfaceView android:focusable="true" android:id="@+id/gameSurfaceView"
      android:layout_width="fill_parent" android:clickable="true"
      android:focusableInTouchMode="true" android:layout_height="fill_parent"></SurfaceView>
    </LinearLayout>

    类文件

    package com.cgw;

    import android.app.Activity;
    import android.content.pm.ActivityInfo;
    import android.media.MediaPlayer;
    import android.media.MediaPlayer.OnCompletionListener;
    import android.media.MediaPlayer.OnErrorListener;
    import android.net.Uri;
    import android.os.Bundle;
    import android.view.Window;
    import android.view.WindowManager;
    import android.widget.MediaController;
    import android.widget.VideoView;
    public class VideoViewDemo extends Activity implements OnErrorListener,OnCompletionListener{
     private VideoView mVideoView;
     private Uri mUri;
     private int mPositionWhenPaused = -1;
     private MediaController mMediaController;//视频的控制条 如果不想要就不用设置。
     @Override
     protected void onCreate(Bundle savedInstanceState) {
      // TODO Auto-generated method stub
      super.onCreate(savedInstanceState);
      requestWindowFeature(Window.FEATURE_NO_TITLE);
      // 全屏模式
      getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
        WindowManager.LayoutParams.FLAG_FULLSCREEN);
      this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);  
      setContentView(R.layout.main);
      mVideoView = (VideoView)findViewById(R.id.videoview);
      mVideoView.setOnCompletionListener(this);
      mVideoView.setOnErrorListener(this);
      //文件路径
      mUri = Uri.parse("android.resource://" + getPackageName() + "/" +R.raw.movie);//mp4格式的文件也一样可以播放。
    //  System.out.println(mUri.getPath());
    //  mMediaController = new MediaController(this);
      //设置MediaController
    //  mVideoView.setMediaController(mMediaController);
     }

     //监听MediaPlayer上报的错误信息
     @Override
     public boolean onError(MediaPlayer mp, int what, int extra) {
      // TODO Auto-generated method stub
      return false;
     }

     //Video播完的时候得到通知
     @Override
     public void onCompletion(MediaPlayer mp) {
      this.finish();
     }

     //开始
     public void onStart() {
      // Play Video
      mVideoView.setVideoURI(mUri);
      
      mVideoView.requestFocus();
      mVideoView.start();

      super.onStart();
     }
     public void onPause() {
      mPositionWhenPaused = mVideoView.getCurrentPosition();
      mVideoView.stopPlayback();
      super.onPause();
     }
     public void onResume() {
      // Resume video player
      if(mPositionWhenPaused >= 0) {
       mVideoView.seekTo(mPositionWhenPaused);
       mPositionWhenPaused = -1;
      }
      super.onResume();
     }
    }

    如果想简单的播放视频文件,用上面的代码就可以了,我要说的是在使用过程中遇到的问题!

    1、当布局文件中的SurfaceView和VideoView这两个控件的顺序颠倒以后就会出现看不到视频的问题,或者将SurfaceView设置为不可见。如果你先用了VideoView 然后又要用SurfaceView ,那么只要将VideoView隐藏就可以了,否则在SurfaceView上画的东西是看不到的。

    2、 Uri.parse("android.resource://" + getPackageName() + "/" +R.raw.movie);//只能获得res下的文件。如果哪位大侠知道如何获得assets的Uri 请留言 谢谢。

  • 相关阅读:
    BOM与DOM
    CSS中的长度单位及颜色表示
    关于display:grid layout
    关于position
    简单的注册表单
    We重邮
    APP定制开发的完整流程
    国内移动广告平台的混战大盘点
    Mobile App Monetization, Analysis & Mediation – Google AdMob
    代码优化
  • 原文地址:https://www.cnblogs.com/cgw0827/p/2618736.html
Copyright © 2011-2022 走看看