在Android上实现视频播放并不是非常困难,可以直接通过MediaPlayer类,也可以用VideoView进行封装。这里介绍的是Rexsee的VideoPlayer扩展,支持直接使用js。
【函数】
boolean start(String url,String style,boolean looping)
【说明】
播放本地或网络视频,如果是网络视频,在缓冲过程中会触发onVideoBufferingUpdated()事件,读取到视频信息时会触发onVideoPlayerInfo()事件,播放完毕会触发onVideoPlayCompleted事件,视频尺寸改变时会触发onVideoSizeChanged事件。
【返回】
true或false。
【参数】
url:要播放的本地视频("file://xxxxx")或网络视频("http://xxxxxx")的路径。
style:播放器对话框的样式。
looping:是否循环播放。
【示例】
rexseeDownload.download('http://www.rexsee.com/images/test.wmv');
function onDownloadFinished(url,path){
rexseeVideoPlayer.start(path,'window-dim-amount:0;window-moveable:true;window-modeless:true;window-cancelable:false;width:300;height:200;border-width:0px;',true);
}
rexseeVideoPlayer.start('http://www.rexsee.com/images/test.wmv','window-dim-amount:0;window-moveable:true;window-modeless:true;window-cancelable:false;width:300;height:200;border-width:0px;',false);
rexseeVideoPlayer.pause();
rexseeVideoPlayer.resume();
rexseeVideoPlayer.stop();
【函数】
boolean seekTo(int position)
【说明】
定位到视频的某一点,定位完毕会会触发onVideoSeekCompleted()事件,注意,在线播放视频时不适用。
【返回】
true或false。
【参数】
position:毫秒数,应小于getDuration()的值并大于0。
【示例】
rexseeVideoPlayer.seekTo(2000);
【函数】
boolean pause()
【说明】
暂停播放视频。
【返回】
true或false。
【函数】
boolean resume()
【说明】
从暂停状态恢复播放视频。
【返回】
true或false。
【函数】
boolean stop()
【说明】
停止播放视频。
【返回】
true或false。
【函数】
int getDuration()
【说明】
读取当前播放的视频的长度。
【返回】
视频的长度,毫秒数。
【示例】
alert(rexseeVideoPlayer.getDuration());
【函数】
boolean isPlaying()
【说明】
是否正在播放视频。
【返回】
true或false。
【示例】
alert(rexseeVideoPlayer.isPlaying());
如下是rexseeVideoPlayer的原生源码,还可以去Rexsee的社区查看它的VideoView等源码:
http://www.rexsee.com/CN/helpReference.php
/*
* Copyright (C) 2011 The Rexsee Open Source Project
*
* Licensed under the Rexsee License, Version 1.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.rexsee.com/CN/legal/license.html
*
* 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 rexsee.multimedia;
import rexsee.core.browser.JavascriptInterface;
import rexsee.core.browser.RexseeBrowser;
import android.content.Context;
import android.media.AudioManager;
public class RexseeVideoPlayer implements JavascriptInterface {
private static final String INTERFACE_NAME = "VideoPlayer";
@Override
public String getInterfaceName() {
return mBrowser.application.resources.prefix + INTERFACE_NAME;
}
@Override
public JavascriptInterface getInheritInterface(RexseeBrowser childBrowser) {
return this;
}
@Override
public JavascriptInterface getNewInterface(RexseeBrowser childBrowser) {
return new RexseeVideoPlayer(childBrowser);
}
private final RexseeBrowser mBrowser;
private final Context mContext;
private VideoDialog mVideoDialog = null;
public RexseeVideoPlayer(RexseeBrowser browser) {
mBrowser = browser;
mContext = browser.getContext();
}
private boolean checkStatus() {
if (mVideoDialog == null || mVideoDialog.videoView.mediaPlayer == null) {
mBrowser.exception(getInterfaceName(), "The video player is not started, please call start().");
return false;
} else {
return true;
}
}
//JavaScript Interface
public boolean start(String url, String style, boolean looping) {
stop();
url = mBrowser.urlListeners.getAbsoluteUrl(url);
mVideoDialog = new VideoDialog(mBrowser, url, style, looping);
mVideoDialog.start();
return true;
}
public boolean stop() {
if (mVideoDialog == null) return true;
try {
if (mVideoDialog.videoView.mediaPlayer != null) {
mVideoDialog.videoView.mediaPlayer.stop();
mVideoDialog.videoView.mediaPlayer.release();
mVideoDialog.videoView.mediaPlayer = null;
}
mVideoDialog.dismiss();
mVideoDialog = null;
return true;
} catch (Exception e) {
mBrowser.exception(getInterfaceName(), e);
return false;
}
}
public boolean pause() {
if (!checkStatus()) return false;
if (!mVideoDialog.videoView.mediaPlayer.isPlaying()) return true;
try {
mVideoDialog.videoView.mediaPlayer.pause();
return true;
} catch (Exception e) {
mBrowser.exception(getInterfaceName(), e);
return false;
}
}
public boolean resume() {
if (!checkStatus()) return false;
try {
mVideoDialog.videoView.mediaPlayer.start();
return true;
} catch (Exception e) {
mBrowser.exception(getInterfaceName(), e);
return false;
}
}
public boolean seekTo(int milliseconds) {
if (!checkStatus()) return false;
try {
mVideoDialog.videoView.mediaPlayer.seekTo(milliseconds);
return true;
} catch (Exception e) {
mBrowser.exception(getInterfaceName(), e);
return false;
}
}
public int getDuration() {
if (!checkStatus()) return -1;
try {
return mVideoDialog.videoView.mediaPlayer.getDuration();
} catch (Exception e) {
mBrowser.exception(getInterfaceName(), e);
return -1;
}
}
public int getCurrentPosition() {
if (!checkStatus()) return -1;
try {
return mVideoDialog.videoView.mediaPlayer.getCurrentPosition();
} catch (Exception e) {
mBrowser.exception(getInterfaceName(), e);
return -1;
}
}
public int getVideoWidth() {
if (!checkStatus()) return -1;
try {
return mVideoDialog.videoView.mediaPlayer.getVideoWidth();
} catch (Exception e) {
mBrowser.exception(getInterfaceName(), e);
return -1;
}
}
public int getVideoHeight() {
if (!checkStatus()) return -1;
try {
return mVideoDialog.videoView.mediaPlayer.getVideoHeight();
} catch (Exception e) {
mBrowser.exception(getInterfaceName(), e);
return -1;
}
}
public boolean isPlaying() {
if (!checkStatus()) return false;
return mVideoDialog.videoView.mediaPlayer.isPlaying();
}
public boolean isLooping() {
if (!checkStatus()) return false;
return mVideoDialog.videoView.mediaPlayer.isLooping();
}
public boolean setVolume(float volume) {
if (!checkStatus()) return false;
if (volume < 0 || volume > 1) {
mBrowser.exception(getInterfaceName(), "The volumn must between 0 and 1.");
return false;
}
AudioManager mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, Math.round((mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC) * volume)), 0);
return true;
}
}
仅对Rexsee的源码和函数事件做了整理,相关的demo或源码解析可以在Rexsee社区了解,目前Rexsee已提供了近2000个扩展,覆盖Android原生功能超过90%,且全部开放:
http://www.rexsee.com/
来源:oschina
链接:https://my.oschina.net/u/194999/blog/50340