使用JS实现Android视频播放,分享Rexsee的原生VideoPlayer源码
在Android上实现VideoPlayer并不是非常困难,可以直接通过MediaPlayer类,也可以用VideoView进行封装。或者就是利用Rexsee的PAI,直接使用JS去实现。
举例:
1. 函数:boolean start(String url,String style,boolean looping)
2. 说明:播放本地或网络视频,如果是网络视频,在缓冲过程中会触发onVideoBufferingUpdated()事件,读取到视频信息时会触发onVideoPlayerInfo()事件,播放完毕会触发onVideoPlayCompleted事件,视频尺寸改变时会触发onVideoSizeChanged事件。
3. 参数:url:要播放的本地视频("file://xxxxx")或网络视频("http://xxxxxx")的路径。style:播放器对话框的样式。looping:是否循环播放。
4. 示例:
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();
……
如下是VideoPlayer的原生源码,还可以去Rexsee的社区查看它的VideoView等源码。不过现在还不是特别方便可以去下载:http://www.rexsee.com/
/* * 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; } }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有高手评价一下不?
lz是仔细研究过Rexsee的么?能留个QQ不?