Rexsee API介紹:Android視頻播放,Rexsee的VideoPlayer函數說明與源碼

在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/
相關文章
相關標籤/搜索