Caricare una risorsa multimediale in MediaPlayer

Ultimo aggiornamento: 2023-09-21

Carica una risorsa creando direttamente un’istanza di MediaResource e caricando il contenuto video da riprodurre. Questo è uno dei modi per caricare una risorsa multimediale.

  1. Imposta l'elemento riproducibile del lettore multimediale con la nuova risorsa da riprodurre.

    Sostituisci l'elemento attualmente riproducibile del lettore multimediale esistente chiamando MediaPlayer.replaceCurrentItem e il passaggio di un MediaResource dell'istanza.

  2. Registra un'implementazione di MediaPlayer.PlaybackEventListener interfaccia con MediaPlayer dell'istanza.

    • onPrepared
    • onStateChanged, e verificare se sono stati impostati i valori INITIALIZED ed ERROR.
  3. Quando lo stato del lettore multimediale diventa INITIALIZED, è possibile chiamare MediaPlayer.prepareToPlay

    Lo stato INIZIALIZZATO indica che il supporto è stato caricato correttamente. Chiamata prepareToPlay avvia il processo di risoluzione e posizionamento pubblicitario, se presente.

  4. Quando TVSDK chiama onPrepared callback, il flusso multimediale è stato caricato correttamente ed è pronto per la riproduzione.

    Quando il flusso multimediale viene caricato, MediaPlayerItem viene creato.

Se si verifica un errore, il MediaPlayer passa allo stato ERROR. Notifica inoltre l'applicazione chiamando il PlaybackEventListener.onStateChangedcallback.

In questo modo vengono trasmessi diversi parametri:

  • A state parametro di tipo MediaPlayer.PlayerState con il valore di MediaPlayer.PlayerState.ERROR.

  • A notification parametro di tipo MediaPlayerNotification che contiene informazioni di diagnostica sull’evento di errore.

Il seguente codice di esempio semplificato illustra il processo di caricamento di una risorsa multimediale:

// mediaResource is a properly configured MediaResource instance
// mediaPlayer is a MediaPlayer instance
// register a PlaybackEventListener implementation with the MediaPlayer
instancemediaPlayer.addEventListener(
  MediaPlayer.Event.PLAYBACK,
  new MediaPlayer.PlaybackEventListener()) {
    @Overridepublic void onPrepared() {
        // at this point, the resource is successfully loaded and available
        // and the MediaPlayer is ready to start the playback
        // once the resource is loaded, the MediaPlayer is able to
        // provide a reference to the current "playable item"

        MediaPlayerItem playerItem = mediaPlayer.CurrentItem();

        if (playerItem != null) {
            // here we can take a look at the properties of the
            // loaded stream
        }
    } @Overridepublic void onStateChanged(
        MediaPlayer.PlayerState state,
        MediaPlayerNotification notification) {
        if (state == MediaPlayer.PlayerState.ERROR) {
            // something bad happened - the resource cannot be loaded
            // details about the problem are provided via the
            // MediaPlayerNotification instance
        }
        elseif (state == MediaPlayer.PlayerState.INITIALIZED) {
            mediaPlayer.prepareToPlay();
        }
    }
    // implementation of the other methods in the PlaybackEventListener interface...
}

In questa pagina