Android Open Source - MakeWithMotoSampleApp Video Player Fragment






From Project

Back to project page MakeWithMotoSampleApp.

License

The source code is released under:

GNU General Public License

If you think the Android project MakeWithMotoSampleApp 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.makewithmoto.fragments;
/*w  w w . ja  va  2 s.c o m*/

import java.util.Vector;

import android.annotation.SuppressLint;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.MediaController;
import android.widget.VideoView;

import com.makewithmoto.example.R;


@SuppressLint("NewApi")
public class VideoPlayerFragment extends Fragment {

  private View v;
  private VideoView mVideoView;
  Vector<VideoListener> listeners = new Vector<VideoListener>();
  Runnable r;
  protected Handler handler;
  protected MediaPlayer mp_;
  
  public interface VideoListener {

    public void onReady(boolean ready);
    public void onFinish(boolean finished); 
    public void onTimeUpdate(int ms, int totalDuration);
  }


  /**
   * Called when the activity is first created.
   * 
   * @return
   */

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    super.onCreateView(inflater, container, savedInstanceState);

    v = inflater.inflate(R.layout.fragment_videoplayer, container, false); 
    mVideoView = (VideoView) v.findViewById(R.id.video_view); 
    
    final FrameLayout fl = (FrameLayout) v.findViewById(R.id.video_parent);
    
    fl.setOnClickListener(new OnClickListener() {
      
      @Override
      public void onClick(View v) {
        fl.animate().scaleX(0.5f).scaleY(0.5f).setDuration(5000);        
      }
    });
    Log.d("mm", "onCreateView");

    handler = new Handler(); 
    
    return v;
    
  }  
  
  public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    Log.d("mm", "onActivityCreated");

    for (VideoListener l : listeners) {
      l.onReady(true);
    }
    
  }

  
  public void loadExternalVideo(String videoFile) {
    String path = Environment.getExternalStorageDirectory() + "/arprototype/video"; 
    loadVideo(path);
  } 
  
  
  public void loadResourceVideo(String videoFile) {
    String path = "android.resource://" + getActivity().getPackageName() + videoFile; 
    loadVideo(path);
  }  
  
  public void loadVideo(final String path) { 
    /*
     * Alternatively,for streaming media you can use
     * mVideoView.setVideoURI(Uri.parse(URLstring));
     */
    
    mVideoView.setVideoPath(path);
    MediaController mediaController = new MediaController(getActivity());
    mediaController.setAnchorView(mVideoView);
    mVideoView.setMediaController(mediaController);
    
    mVideoView.requestFocus();
    mVideoView.setKeepScreenOn(true);

    mVideoView.start(); 
    
    mVideoView.setOnClickListener(new OnClickListener() {

      @Override
      public void onClick(View v) {
        close();
      }
    });

    mVideoView.setOnPreparedListener(new OnPreparedListener() {
      @Override
      public void onPrepared(MediaPlayer mp) { 
        mp_ = mp;  
        mp_.setLooping(true);
        

        //mVideoView.animate().rotation(200).alpha((float) 0.5)
        //    .scaleX(0.2f).scaleY(0.2f).setDuration(2000);

        r = new Runnable() {
          
          @Override
          public void run() {
            for (VideoListener l : listeners) {
              l.onTimeUpdate(mp_.getCurrentPosition(), mp_.getDuration()); 
            } 
            handler.postDelayed(this, 1000);
          }
        };
        

        handler.post(r);
      }
    });
    
    //mp_.setO
    
    mVideoView.setOnCompletionListener(new OnCompletionListener() {

      public void onCompletion(MediaPlayer mp) {

        //finish();
        for (VideoListener l : listeners) {
          l.onFinish(true);
        }
        

      }
    });
    

  }

  public void setVolume(float volume) {
    if (mp_ != null) {
      mp_.setVolume(volume, volume);
      
    }  
  }
  
  public void setLoop(boolean b) { 
    mp_.setLooping(b);
  }
  
  public void close() {
    handler.removeCallbacks(r);
    //mVideoView.stopPlayback();

  }

  public boolean onTouchEvent(MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN) {

    }
    return true;
  }
  
  public void addListener(VideoListener videoListener) {
    listeners.add(videoListener);
  }

  public void removeListener(VideoListener videoListener) {
    listeners.remove(videoListener);
  }


  public void seekTo(int ms) { 
    mp_.seekTo(ms);
  }
}




Java Source Code List

android_serialport_api.SerialPort.java
com.makewithmoto.boards.MAKr.java
com.makewithmoto.boards.MIOIOListener.java
com.makewithmoto.bt.BluetoothSerialService.java
com.makewithmoto.bt.BluetoothViewer.java
com.makewithmoto.bt.DeviceListActivity.java
com.makewithmoto.bt.SimpleBT.java
com.makewithmoto.example.ActivityBT.java
com.makewithmoto.example.ActivityMAKr.java
com.makewithmoto.example.ActivityMOIO.java
com.makewithmoto.example.Application.java
com.makewithmoto.example.LauncherActivity.java
com.makewithmoto.extras.AccelerometerManager.java
com.makewithmoto.extras.CustomSensorListener.java
com.makewithmoto.extras.CustomSensorManager.java
com.makewithmoto.extras.WhatIsRunningInterface.java
com.makewithmoto.extras.WhatIsRunning.java
com.makewithmoto.fragments.CameraFragment.java
com.makewithmoto.fragments.DebugFragment.java
com.makewithmoto.fragments.EmptyFragment.java
com.makewithmoto.fragments.GraphFragment.java
com.makewithmoto.fragments.ImageFragment.java
com.makewithmoto.fragments.Utils.java
com.makewithmoto.fragments.VideoPlayerFragment.java
com.makewithmoto.fragments.VideoTextureFragment.java
com.makewithmoto.views.CanvasUtils.java
com.makewithmoto.views.PlotView.java