Back to project page mclib.
The source code is released under:
Apache License, Version 2.0 FoundationProjectsPeopleGet InvolvedDownloadSupport ApacheHome ? Licenses Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITION...
If you think the Android project mclib listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
/* Copyright 2012 Mikhail Chabanov/*w w w. j a va2 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 mc.lib.video; import java.io.File; import java.io.FileDescriptor; import java.io.InputStream; import mc.lib.interfaces.OnCompleteListener; import mc.lib.interfaces.OnProgressListener; import mc.lib.log.Log; import android.media.MediaPlayer; import android.media.MediaPlayer.OnCompletionListener; import android.media.MediaPlayer.OnPreparedListener; import android.net.Uri; import android.os.Handler; import android.widget.VideoView; public class VideoViewPlayer extends BasicVideoPlayer implements OnPreparedListener, OnCompletionListener { private static final String LOGTAG = VideoViewPlayer.class.getSimpleName(); protected static final long UPDATE_PERIOD = 1000 / 24; private OnCompleteListener<Class<?>> completeListener; private OnProgressListener progressListener; private VideoView videoPlayer; private Handler handler; private boolean paused; private int position; private Runnable progressNotifier = new Runnable() { @Override public void run() { if(progressListener != null) { progressListener.notifyProgress(videoPlayer.getCurrentPosition(), getDuration()); } handler.postDelayed(progressNotifier, UPDATE_PERIOD); } }; public VideoViewPlayer(VideoView view) { super(view.getContext()); this.videoPlayer = view; videoPlayer.setOnPreparedListener(this); videoPlayer.setOnCompletionListener(this); videoPlayer.setKeepScreenOn(true); } @Override public void play(String filePath) { videoPlayer.setVideoPath(filePath); } public void play(Uri uri) { videoPlayer.setVideoURI(uri); } @Override public void play(File f, int startPos) { position = startPos; play(f); } @Override public void play(String filePath, int startPos) { position = startPos; play(filePath); } @Override public void play(int rawResId, int startPos) { position = startPos; play(rawResId); } @Override public void play(InputStream is, int startPos) { position = startPos; play(is); } @Override public void play(Uri uri, int startPos) { position = startPos; play(uri); } @Override public void play(FileDescriptor fd, int startPos) { position = startPos; play(fd); } private void runProgressNotifier() { if(progressListener != null) { if(handler == null) handler = new Handler(); handler.post(progressNotifier); } } private void stopProgressNotifier() { if(handler != null) handler.removeCallbacks(progressNotifier); } public void resume() { if(paused) { videoPlayer.start(); runProgressNotifier(); paused = false; } } public void pause() { if(videoPlayer.isPlaying()) { stopProgressNotifier(); videoPlayer.pause(); paused = true; } } public boolean isPaused() { return paused; } public void stop() { if(videoPlayer.isPlaying()) { stopProgressNotifier(); videoPlayer.stopPlayback(); paused = false; } } public void onCompletion(MediaPlayer mp) { Log.i(LOGTAG, "Play completed"); position = 0; paused = false; stopProgressNotifier(); if(completeListener != null) completeListener.complete(getClass()); } public void onPrepared(MediaPlayer mp) { Log.i(LOGTAG, "Play started"); paused = false; runProgressNotifier(); videoPlayer.seekTo(position); videoPlayer.start(); } @Override public int getPlayPosition() { return videoPlayer.getCurrentPosition(); } @Override public void setPlayPosition(int position) { videoPlayer.seekTo(position); } @Override public int getDuration() { return videoPlayer.getDuration(); } @Override public OnCompleteListener<Class<?>> getOnCompleteListener() { return completeListener; } @Override public void setOnCompleteListener(OnCompleteListener<Class<?>> listener) { this.completeListener = listener; } @Override public OnProgressListener getOnProgressListener() { return progressListener; } @Override public void setOnProgressListener(OnProgressListener listener) { this.progressListener = listener; } @Override public void close() { stop(); } }