zoukankan      html  css  js  c++  java
  • MediaPlayer视频播放器

    android视频播放 根据apidemo重写。代码如下:


    package com.jamdeo.tv.livetv.player;
    
    
    import android.media.AudioManager;
    import android.media.MediaPlayer;
    import android.media.MediaPlayer.OnBufferingUpdateListener;
    import android.media.MediaPlayer.OnCompletionListener;
    import android.media.MediaPlayer.OnPreparedListener;
    import android.media.MediaPlayer.OnVideoSizeChangedListener;
    import android.util.Log;
    import android.view.SurfaceHolder;
    import android.view.SurfaceView;
    
    public class MyPlayer extends MediaPlayer  implements
    OnBufferingUpdateListener, OnCompletionListener,
    OnPreparedListener, OnVideoSizeChangedListener {
    	public static final MyPlayer mPlayer = new MyPlayer();
    	private SurfaceHolder mHolder;
    	private SurfaceView mSurfaceView;
        private boolean mIsVideoSizeKnown = false;
        private boolean mIsVideoReadyToBePlayed = false;
        private static final String TAG = "MyPlayer";
        private int mVideoWidth;
        private int mVideoHeight;
        private String mCurrentPath;
        private String mPreviousPath;
    	private MyPlayer(){
    		
    	}
    	
    	public static MyPlayer getInstance(){
    		return mPlayer;
    	}
    	
    	private  void init(){
    	     mPlayer.setOnBufferingUpdateListener(this);
    	     mPlayer.setOnCompletionListener(this);
    	     mPlayer.setOnPreparedListener(this);
    	     mPlayer.setOnVideoSizeChangedListener(this);
    	     mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    	}
    
    	public void setSurfaceView(SurfaceView _surfaceView){
    		init();
    		this.mSurfaceView =  _surfaceView;
    		this.mHolder = this.mSurfaceView.getHolder();
    		//此方法请在setDataSource(..)之前调用,我起初就在setDataSource(...)方法后调用导致显示界面偏移
    		mPlayer.setDisplay(this.mHolder);
    	}
    	
    	public void setPath(String _path){
    		this.mPreviousPath = this.mCurrentPath;
    		this.mCurrentPath = _path;
    		 if(this.mCurrentPath.equals(this.mPreviousPath)){
          	   return;
             }else{
            	 PlayVideo();
             }
    		Log.d(TAG, "previous: " + this.mPreviousPath + "current: "+mCurrentPath);
    	}
    	public void PlayVideo(){
    		try {
    			Log.d(TAG, "previous: " + this.mPreviousPath + "current: "+mCurrentPath);
    			doCleanUp();
    			mPlayer.reset();
    			mPlayer.setDataSource(mCurrentPath);
    			mPlayer.prepareAsync();
    	
    		} catch (Exception e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    	}
    
    	@Override
    	public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
    		   Log.v(TAG, "onVideoSizeChanged called");
    	        if (width == 0 || height == 0) {
    	            Log.e(TAG, "invalid video width(" + width + ") or height(" + height + ")");
    	            return;
    	        }
    	        mIsVideoSizeKnown = true;
    	        mVideoWidth = width;
    	        mVideoHeight = height;
    	        if (mIsVideoReadyToBePlayed && mIsVideoSizeKnown) {
    	            startVideoPlayback();
    	        }
    		
    	}
    
    	@Override
    	public void onPrepared(MediaPlayer mp) {
    		 Log.d(TAG, "onPrepared called");
    	        mIsVideoReadyToBePlayed = true;
    	        if (mIsVideoReadyToBePlayed && mIsVideoSizeKnown) {
    	            startVideoPlayback();
    	        }
    		
    	}
    
    	@Override
    	public void onCompletion(MediaPlayer mp) {
    		 Log.d(TAG, "onCompletion called");
    		
    	}
    
    	@Override
    	public void onBufferingUpdate(MediaPlayer mp, int percent) {
    		  Log.d(TAG, "onBufferingUpdate percent:" + percent);
    		
    	}
    	
    	   private void releaseMediaPlayer() {
    	        if (mPlayer != null) {
    	        	mPlayer.release();
    	        }
    	    }
    
    	    private void doCleanUp() {
    	        mVideoWidth = 0;
    	        mVideoHeight = 0;
    	        mIsVideoReadyToBePlayed = false;
    	        mIsVideoSizeKnown = false;
    	    }
    
    	    private void startVideoPlayback() {
    	        Log.v(TAG, "startVideoPlayback");
    	        Log.d(TAG, "size: "+mVideoWidth +" x "+ mVideoHeight);
    	        mHolder.setFixedSize(mVideoWidth, mVideoHeight);
    	        mPlayer.start();
    	    }
    
    }
    



    在外部使用的时候只需要传一个SurfaceView进去然后setPath(String url),就可以直接播放了。

  • 相关阅读:
    深入正则表达式(0):正则表达式概述
    讲透学烂二叉树(二):图中树的定义&各类型树的特征分析
    讲透学烂二叉树(一):图的概念和定义—各种属性特征浅析
    Gzip之后继者Brotli浅析之CDN厂商的智能压缩,服务器Brotli设置
    ECMAScript进化史(1):​话说Web脚本语言王者JavaScript的加冕历史
    nginx网站限速限流配置——网站被频繁攻击,nginx上的设置limit_req和limit_conn
    linux添加用户,修改用户密码,修改用户权限,设置root用户操作
    nginx 限制ip访问,禁止非法域名指向本机ip——防止被别人绑定域名到自己IP的方法
    centos8 新增ssh自定义端口与屏蔽默认22端口。
    1g云主机升级centos8不满足centos 8 至少2g内存要求,linux虚拟内存来凑
  • 原文地址:https://www.cnblogs.com/riskyer/p/3323067.html
Copyright © 2011-2022 走看看