在MediaPlayer中載入媒體資源

直接執行個體化MediaResource並載入要播放的視訊內容,以載入資源。 這是載入介質資源的一種方法。

  1. 使用要播放的新資源設定MediaPlayer對象的可播放項。

    呼叫MediaPlayer.replaceCurrentResource並傳遞現有的MediaResource例項,以取代您現有的MediaPlayer目前可播放的項目。

  2. 請至少檢查下列變更:

    • 已初始化

    • 準備好

    • 錯誤

      通過這些事件,MediaPlayer對象可以在媒體資源成功載入時通知您的應用程式。

  3. 當媒體播放器的狀態更改為「已初始化」時,您可以調用MediaPlayer.prepareToPlay

    「已初始化」狀態表示介質已成功載入。 呼叫prepareToPlay會啟動廣告解析度和位置處理程式(如果有的話)。

  4. 當媒體播放器狀態變更為PREPARED時,媒體串流已成功載入並準備播放。

    載入媒體串流時,會建立MediaPlayerItem

如果發生故障,MediaPlayer會切換至「錯誤」狀態。 它還會將STATUS_CHANGED事件分派到您的MediaPlayerStatusChangeEvent回呼,以通知您的應用程式。

這會傳遞數個參數:

  • 類型字串的type參數,其值為ERROR

  • MediaError參數,您可用來取得包含錯誤事件診斷資訊的通知。

以下簡化的范常式式碼說明載入媒體資源的程式:

// mediaResource is a properly configured MediaResource instance
// mediaPlayer is a MediaPlayer instance
// register an event listener with the MediaPlayer instance
mediaPlayer.addEventListener(MediaPlayerStatusChangeEvent.STATUS_CHANGED,
                             onStatusChanged);
private function onStatusChanged(event:MediaPlayerStatusChangeEvent):void {
   switch(event.status) {
      case MediaPlayerStatus.INITIALIZED:
          // at this point, the resource is successfully loaded
          // the media player will provide a reference to the current
          // "playable item" ( is guarantee to be valid and not-null).
          var playerItem: MediaPlayerItem = mediaPlayer.currentItem;
          // we can take a look at the media item characteristics like
          // alternate audio tracks, profile information, if is a live stream
          // if is drm protected
          mediaPlayer.prepareToPlay();
          break;
    case MediaPlayerStatus.PREPARED:
         // at this point, the resource is successfully processed all
         // advertisement placements have been executed and the the
         // MediaPlayer is ready to start the playback
        if (autoPlay) {
            mediaPlayer.play();
        }
        break;
    case MediaPlayerStatus.ERROR:
        // something bad happened - the resource cannot be loaded
        // details about the problem are provided via the event.error property
        break;
        // implementation of the other methods in the PlaybackEventListener interface
        ...
    }
}

本頁內容