Android Open Source - TwoVideoPlayerInOneScreenDemo Player






From Project

Back to project page TwoVideoPlayerInOneScreenDemo.

License

The source code is released under:

Apache License

If you think the Android project TwoVideoPlayerInOneScreenDemo listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.example.playerdemo;
/*from  ww w .j av a2s .c  o  m*/

import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnBufferingUpdateListener;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.SeekBar;

public class Player implements OnBufferingUpdateListener,
    OnCompletionListener, MediaPlayer.OnPreparedListener,
    SurfaceHolder.Callback {
  private int videoWidth;
  private int videoHeight;
  public MediaPlayer mediaPlayer;
  private SurfaceHolder surfaceHolder;
  private SeekBar skbProgress;
  private Timer mTimer=new Timer();
  public Player(SurfaceView surfaceView,SeekBar skbProgress)
  {
    this.skbProgress=skbProgress;
    surfaceHolder=surfaceView.getHolder();
    surfaceHolder.addCallback(this);
    surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    mTimer.schedule(mTimerTask, 0, 1000);
  }
  
  /*******************************************************
   * ??????Handler??????????
   ******************************************************/
  TimerTask mTimerTask = new TimerTask() {
    @Override
    public void run() {
      if(mediaPlayer==null)
        return;
      if (mediaPlayer.isPlaying() && skbProgress.isPressed() == false) {
        handleProgress.sendEmptyMessage(0);
      }
    }
  };
  
  Handler handleProgress = new Handler() {
    public void handleMessage(Message msg) {

      int position = mediaPlayer.getCurrentPosition();
      int duration = mediaPlayer.getDuration();
      
      if (duration > 0) {
        long pos = skbProgress.getMax() * position / duration;
        skbProgress.setProgress((int) pos);
      }
    };
  };
  //*****************************************************
  
  public void play()
  {
    mediaPlayer.start();
  }
  
  public void playUrl(String videoUrl)
  {
    
    try {
      mediaPlayer = new MediaPlayer();
      mediaPlayer.setDisplay(surfaceHolder);
      mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
      
      
      mediaPlayer.setOnBufferingUpdateListener(this);
      mediaPlayer.setOnCompletionListener(this);
      mediaPlayer.setOnPreparedListener(this);
      mediaPlayer.setScreenOnWhilePlaying(true);
      mediaPlayer.setAudioStreamType(3);
      
      mediaPlayer.setOnBufferingUpdateListener(this);
      mediaPlayer.setOnPreparedListener(this);
    } catch (Exception e) {
      Log.e("mediaPlayer", "error", e);
    }
    
    try {
      
      mediaPlayer.setDataSource(videoUrl);
      mediaPlayer.prepare();//prepare????????
    } catch (IllegalArgumentException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (IllegalStateException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }

  
  public void pause()
  {
    mediaPlayer.pause();
  }
  
  public void stop()
  {
    if (mediaPlayer != null) { 
      mediaPlayer.stop();
            mediaPlayer.release(); 
            mediaPlayer = null; 
        } 
  }
  
  @Override
  public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
    Log.e("mediaPlayer", "surface changed");
  }

  @Override
  public void surfaceCreated(SurfaceHolder arg0) {
    
    Log.e("mediaPlayer", "surface created");
  }

  @Override
  public void surfaceDestroyed(SurfaceHolder arg0) {
    Log.e("mediaPlayer", "surface destroyed");
  }

  
  @Override
  /**
   * ??onPrepared??
   */
  public void onPrepared(MediaPlayer arg0) {
    videoWidth = mediaPlayer.getVideoWidth();
    videoHeight = mediaPlayer.getVideoHeight();
    if (videoHeight != 0 && videoWidth != 0) {
      arg0.start();
    }
    Log.e("mediaPlayer", "onPrepared");
  }

  @Override
  public void onCompletion(MediaPlayer arg0) {
    // TODO Auto-generated method stub
    
  }

  @Override
  public void onBufferingUpdate(MediaPlayer arg0, int bufferingProgress) {
    skbProgress.setSecondaryProgress(bufferingProgress);
    int currentProgress=skbProgress.getMax()*mediaPlayer.getCurrentPosition()/mediaPlayer.getDuration();
    Log.e(currentProgress+"% play", bufferingProgress + "% buffer");
    
  }

}




Java Source Code List

com.example.playerdemo.MainActivity.java
com.example.playerdemo.Player.java