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),就可以直接播放了。

  • 相关阅读:
    数据结构-树与二叉树-思维导图
    The last packet successfully received from the server was 2,272 milliseconds ago. The last packet sent successfully to the server was 2,258 milliseconds ago.
    idea连接mysql报错Server returns invalid timezone. Go to 'Advanced' tab and set 'serverTimezone' property
    redis学习笔记
    AJAX校验注册用户名是否存在
    AJAX学习笔记
    JSON学习笔记
    JQuery基础知识学习笔记
    Filter、Listener学习笔记
    三层架构学习笔记
  • 原文地址:https://www.cnblogs.com/riskyer/p/3323067.html
Copyright © 2011-2022 走看看