android平台调用vlc播放器的例子

来源:互联网 发布:淘宝店铺规划 编辑:程序博客网 时间:2024/05/18 03:12

做为一个android平台vlc的测试程序,例子只有简单的播放、停止功能,界面只有四个控件,播放、停止按钮,设置url,显示surface。

稳定性遗憾的是vlc不能对接上openmax硬解。

vlc版本:vlc-2.0.0

package org.videolan.vlc;

import java.io.IOException;
import android.app.Activity;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.SurfaceHolder.Callback;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.EditText;


public class vlcplayerActivity extends Activity {
    /** Called when the activity is first created. */
 public class ClickEvent implements OnClickListener {

  @Override
  public void onClick(View v) {
   // TODO Auto-generated method stub
   if(v==mplayerbtn)
   {
    try { 
     mLibVLC.readMedia(murlPath.getText().toString());
             
          } catch (IllegalArgumentException e) { 
              // TODO Auto-generated catch block 
              e.printStackTrace(); 
          } catch (IllegalStateException e) { 
              // TODO Auto-generated catch block 
              e.printStackTrace(); 
          }
   }
   else if(v==mstopbtn)
   {
    mLibVLC.stop();
   }
   
  }
 
 }
 
 public final static String TAG = "VLC/VideoPlayerActivity";
 private Button mplayerbtn = null;
 private Button mstopbtn = null;
 private SurfaceView  mvideoserface = null; 
    private SurfaceHolder surfaceHolder;
   
    private EditText murlPath = null;
    private LibVLC mLibVLC;
   
 // size of the video
    private int mVideoHeight;
    private int mVideoWidth;
   
    private static final int SURFACE_SIZE = 3;
   
    private static final int SURFACE_BEST_FIT = 0;
    private static final int SURFACE_FIT_HORIZONTAL = 1;
    private static final int SURFACE_FIT_VERTICAL = 2;
    private static final int SURFACE_FILL = 3;
    private static final int SURFACE_16_9 = 4;
    private static final int SURFACE_4_3 = 5;
    private static final int SURFACE_ORIGINAL = 6;
    private int mCurrentSize = SURFACE_BEST_FIT;
   
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mplayerbtn = (Button)findViewById(R.id.btnplay);  
  mstopbtn = (Button)findViewById(R.id.btnstop);
  murlPath = (EditText)findViewById(R.id.urlpath);
  //String str="/mnt/sdcard/2.mp4"
  murlPath.setText("/mnt/sdcard/2.mp4");
  mvideoserface = (SurfaceView)findViewById(R.id.surfaceView1);
  
  mplayerbtn.setOnClickListener(new ClickEvent());
  mstopbtn.setOnClickListener(new ClickEvent());
  
  surfaceHolder=mvideoserface.getHolder();
  surfaceHolder.setFormat(PixelFormat.RGBX_8888);
        surfaceHolder.addCallback(mSurfaceCallback);
       
       
       
        try {
            LibVLC.useIOMX(this);
            mLibVLC = LibVLC.getInstance();
        } catch (LibVlcException e) {
            e.printStackTrace();
        }
    }
   
    private SurfaceHolder.Callback mSurfaceCallback = new Callback() {
        public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
         mLibVLC.attachSurface(holder.getSurface(), vlcplayerActivity.this, width, height);
        }

        public void surfaceCreated(SurfaceHolder holder) {
         try { 
               
            } catch (Exception e) { 
               Log.e("mediaPlayer", "error", e); 
            } 
           
        }

        public void surfaceDestroyed(SurfaceHolder holder) {
            mLibVLC.detachSurface();
        }
    };
   
    public void setSurfaceSize(int width, int height) {
        // store video size
        mVideoHeight = height;
        mVideoWidth = width;
        Message msg = mHandler.obtainMessage(SURFACE_SIZE);
        mHandler.sendMessage(msg);
    }
   
    /**
     * Handle resize of the surface and the overlay
     */
    private Handler mHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                /*case FADE_OUT:
                    hideOverlay(false);
                    break;
                case SHOW_PROGRESS:
                    int pos = setOverlayProgress();
                    if (!mDragging && mShowing && mLibVLC.isPlaying()) {
                        msg = obtainMessage(SHOW_PROGRESS);
                        sendMessageDelayed(msg, 1000 - (pos % 1000));
                    }
                    break;*/
                case SURFACE_SIZE:
                    changeSurfaceSize();
                    break;
                /*case FADE_OUT_INFO:
                    if (mInfo.getVisibility() == View.VISIBLE)
                        mInfo.startAnimation(AnimationUtils.loadAnimation(
                                VideoPlayerActivity.this, android.R.anim.fade_out));
                    mInfo.setVisibility(View.INVISIBLE);*/
            }
        }
    };
   
    private void changeSurfaceSize() {
        // get screen size
        int dw = getWindowManager().getDefaultDisplay().getWidth();
        int dh = getWindowManager().getDefaultDisplay().getHeight();

        // calculate aspect ratio
        double ar = (double) mVideoWidth / (double) mVideoHeight;
        // calculate display aspect ratio
        double dar = (double) dw / (double) dh;

        switch (mCurrentSize) {
            case SURFACE_BEST_FIT:
                if (dar < ar)
                    dh = (int) (dw / ar);
                else
                    dw = (int) (dh * ar);
                break;
            case SURFACE_FIT_HORIZONTAL:
                dh = (int) (dw / ar);
                break;
            case SURFACE_FIT_VERTICAL:
                dw = (int) (dh * ar);
                break;
            case SURFACE_FILL:
                break;
            case SURFACE_16_9:
                ar = 16.0 / 9.0;
                if (dar < ar)
                    dh = (int) (dw / ar);
                else
                    dw = (int) (dh * ar);
                break;
            case SURFACE_4_3:
                ar = 4.0 / 3.0;
                if (dar < ar)
                    dh = (int) (dw / ar);
                else
                    dw = (int) (dh * ar);
                break;
            case SURFACE_ORIGINAL:
                dh = mVideoHeight;
                dw = mVideoWidth;
                break;
        }

        surfaceHolder.setFixedSize(mVideoWidth, mVideoHeight);
        LayoutParams lp = mvideoserface.getLayoutParams();
        lp.width = dw;
        lp.height = dh;
        mvideoserface.setLayoutParams(lp);
        mvideoserface.invalidate();
    }
}

原创粉丝点击