Android Open Source - MCCDash Media Player Demo_ Video






From Project

Back to project page MCCDash.

License

The source code is released under:

Apache License

If you think the Android project MCCDash 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

/*
 * Copyright (C) 2013 yixia.com/*  w  w w. ja  v a  2  s  .  c o m*/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package edu.bupt.mccdash;

import java.io.IOException;

import android.app.Activity;
import android.graphics.PixelFormat;
import android.media.AudioManager;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.Toast;

import io.vov.vitamio.LibsChecker;
import io.vov.vitamio.MediaPlayer;
import io.vov.vitamio.MediaPlayer.OnBufferingUpdateListener;
import io.vov.vitamio.MediaPlayer.OnCompletionListener;
import io.vov.vitamio.MediaPlayer.OnPreparedListener;
import io.vov.vitamio.MediaPlayer.OnVideoSizeChangedListener;

public class MediaPlayerDemo_Video extends Activity implements OnBufferingUpdateListener, OnCompletionListener, OnPreparedListener, OnVideoSizeChangedListener, SurfaceHolder.Callback {

  private static final String TAG = "MediaPlayerDemo";
  private int mVideoWidth;
  private int mVideoHeight;
  private MediaPlayer mMediaPlayer;
  private SurfaceView mPreview;
  private SurfaceHolder holder;
  private String path;
  private Bundle extras;
  private static final String MEDIA = "media";
  private static final int LOCAL_AUDIO = 1;
  private static final int STREAM_AUDIO = 2;
  private static final int RESOURCES_AUDIO = 3;
  private static final int LOCAL_VIDEO = 4;
  private static final int STREAM_VIDEO = 5;
  private boolean mIsVideoSizeKnown = false;
  private boolean mIsVideoReadyToBePlayed = false;

  /**
   * 
   * Called when the activity is first created.
   */
  @Override
  public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    if (!LibsChecker.checkVitamioLibs(this))
      return;
    setContentView(R.layout.mediaplayer_2);
    mPreview = (SurfaceView) findViewById(R.id.surface);
    holder = mPreview.getHolder();
    holder.addCallback(this);
    holder.setFormat(PixelFormat.RGBA_8888); 
    extras = getIntent().getExtras();

  }

  private void playVideo(Integer Media) {
    doCleanUp();
    try {

      switch (Media) {
      case LOCAL_VIDEO:
        /*
         * TODO: Set the path variable to a local media file path.
         */
        path =  "/sdcard/2.mp4.ts";
        if (path == "") {
          // Tell the user to provide a media file URL.
          Toast.makeText(MediaPlayerDemo_Video.this, "Please edit MediaPlayerDemo_Video Activity, " + "and set the path variable to your media file path." + " Your media file must be stored on sdcard.", Toast.LENGTH_LONG).show();
          return;
        }
        break;
      case STREAM_VIDEO:
        /*
         * TODO: Set path variable to progressive streamable mp4 or
         * 3gpp format URL. Http protocol should be used.
         * Mediaplayer can only play "progressive streamable
         * contents" which basically means: 1. the movie atom has to
         * precede all the media data atoms. 2. The clip has to be
         * reasonably interleaved.
         * 
         */
        path = "http://10.105.39.110/hls-server/output.m3u8";
        if (path == "") {
          // Tell the user to provide a media file URL.
          Toast.makeText(MediaPlayerDemo_Video.this, "Please edit MediaPlayerDemo_Video Activity," + " and set the path variable to your media file URL.", Toast.LENGTH_LONG).show();
          return;
        }

        break;

      }

      // Create a new media player and set the listeners
      mMediaPlayer = new MediaPlayer(this);
      mMediaPlayer.setDataSource(path);
      mMediaPlayer.setDisplay(holder);
      mMediaPlayer.prepare();
      
      
      mMediaPlayer.setOnBufferingUpdateListener(this);
      mMediaPlayer.setOnCompletionListener(this);
      mMediaPlayer.setOnPreparedListener(this);
      mMediaPlayer.setOnVideoSizeChangedListener(this);
      mMediaPlayer.getMetadata();
      setVolumeControlStream(AudioManager.STREAM_MUSIC);

    } catch (Exception e) {
      Log.e(TAG, "error: " + e.getMessage(), e);
    }
  }

  public void onBufferingUpdate(MediaPlayer arg0, int percent) {
    Log.d(TAG, "onBufferingUpdate percent:" + percent);

  }

  public void onCompletion(MediaPlayer arg0) {
    
    Log.d(TAG, "onCompletion called");
    
    String strPath =  "/sdcard/2-1.mp4.ts";
    try {
      mMediaPlayer.reset();  
      mMediaPlayer.setDataSource(strPath);
      mMediaPlayer.prepare();
      mMediaPlayer.getMetadata();
    } catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } 
    
  }

  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();
    }
  }

  public void onPrepared(MediaPlayer mediaplayer) {
    Log.d(TAG, "onPrepared called");
    mIsVideoReadyToBePlayed = true;
    if (mIsVideoReadyToBePlayed && mIsVideoSizeKnown) {
      startVideoPlayback();
    }
  }

  public void surfaceChanged(SurfaceHolder surfaceholder, int i, int j, int k) {
    Log.d(TAG, "surfaceChanged called");

  }

  public void surfaceDestroyed(SurfaceHolder surfaceholder) {
    Log.d(TAG, "surfaceDestroyed called");
  }

  public void surfaceCreated(SurfaceHolder holder) {
    Log.d(TAG, "surfaceCreated called");
    playVideo(extras.getInt(MEDIA));

  }

  @Override
  protected void onPause() {
    super.onPause();
    releaseMediaPlayer();
    doCleanUp();
  }

  @Override
  protected void onDestroy() {
    super.onDestroy();
    releaseMediaPlayer();
    doCleanUp();
  }

  private void releaseMediaPlayer() {
    if (mMediaPlayer != null) {
      mMediaPlayer.release();
      mMediaPlayer = null;
    }
  }

  private void doCleanUp() {
    mVideoWidth = 0;
    mVideoHeight = 0;
    mIsVideoReadyToBePlayed = false;
    mIsVideoSizeKnown = false;
  }

  private void startVideoPlayback() {
    Log.v(TAG, "startVideoPlayback");
    holder.setFixedSize(mVideoWidth, mVideoHeight);
    mMediaPlayer.start();
  }
}




Java Source Code List

edu.bupt.mccdash.HttpDownloader.java
edu.bupt.mccdash.MainActivity.java
edu.bupt.mccdash.MediaPlayerDemo_Video.java
edu.bupt.mccdash.OnBluetoothRecvCompleteListener.java
edu.bupt.mccdash.OnHttpDownloadCompleteListener.java
edu.bupt.mccdash.bluetooth.BluetoothConnectionService.java
edu.bupt.mccdash.bluetooth.DeviceListActivity.java
edu.bupt.mccdash.bluetooth.FileInfo.java
edu.bupt.mccdash.bluetooth.WirelessConnectionService.java
edu.bupt.mccdash.cpuutils.CpuManager.java
edu.bupt.mccdash.io.FileFactory.java
edu.bupt.mccdash.io.Logger.java
edu.bupt.mccdash.m3u8.FakeStreamPlayer.java
edu.bupt.mccdash.m3u8.M3u8Resolver.java