2010-08-29 19 views
11

The Stagefrigh media framework (Android 2.2) supporta lo streaming progressivo HTTP.
Cosa significa? Cioè è una realizzazione del protocollo HTTP Live Streaming?Android 2.2 Streaming HTTP progressivo = HTTP Live Streaming?

E come utilizzare HTTP Live Streaming su Android, intendo qual è la realizzazione del client - browser web, MediaPlayer o solo "in-SDK" e devo ereditare da qualche classe?

risposta

22

Una grande differenza pratica è che il framework multimediale di Stagefright supporta lo streaming mpeg3, che il vecchio motore non ha. Quindi puoi usare (shoutcast) mp3streams per esempio.

Ecco un semplice esempio di implementazione, che trasmette un URL shoutcast: http://fr3.ah.fm:9000. Nota che funziona solo su Android 2.2 e versioni successive.

import android.app.Activity; 
import android.media.AudioManager; 
import android.media.MediaPlayer; 
import android.net.Uri; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.widget.Button; 

public class SimpleMusicStream extends Activity implements 
    MediaPlayer.OnCompletionListener, MediaPlayer.OnPreparedListener, 
    MediaPlayer.OnErrorListener, MediaPlayer.OnBufferingUpdateListener { 

private String TAG = getClass().getSimpleName(); 
private MediaPlayer mp = null; 

private Button play; 
private Button pause; 
private Button stop; 

@Override 
public void onCreate(Bundle icicle) { 
    super.onCreate(icicle); 
    setContentView(R.layout.main); 

    play = (Button) findViewById(R.id.play); 
    pause = (Button) findViewById(R.id.pause); 
    stop = (Button) findViewById(R.id.stop); 

    play.setOnClickListener(new View.OnClickListener() { 
    public void onClick(View view) { 
    play(); 
    } 
    }); 

    pause.setOnClickListener(new View.OnClickListener() { 
    public void onClick(View view) { 
    pause(); 
    } 
    }); 

    stop.setOnClickListener(new View.OnClickListener() { 
    public void onClick(View view) { 
    stop(); 
    } 
    }); 
} 

private void play() { 
    Uri myUri = Uri.parse("http://fr3.ah.fm:9000/"); 
    try { 
    if (mp == null) { 
    this.mp = new MediaPlayer(); 
    } else { 
    mp.stop(); 
    mp.reset(); 
    } 
    mp.setDataSource(this, myUri); // Go to Initialized state 
    mp.setAudioStreamType(AudioManager.STREAM_MUSIC); 
    mp.setOnPreparedListener(this); 
    mp.setOnBufferingUpdateListener(this); 

    mp.setOnErrorListener(this); 
    mp.prepareAsync(); 

    Log.d(TAG, "LoadClip Done"); 
    } catch (Throwable t) { 
    Log.d(TAG, t.toString()); 
    } 
} 

@Override 
public void onPrepared(MediaPlayer mp) { 
    Log.d(TAG, "Stream is prepared"); 
    mp.start(); 
} 

private void pause() { 
    mp.pause(); 
} 

private void stop() { 
    mp.stop(); 

} 

@Override 
public void onDestroy() { 
    super.onDestroy(); 
    stop(); 

} 

public void onCompletion(MediaPlayer mp) { 
    stop(); 
} 

public boolean onError(MediaPlayer mp, int what, int extra) { 
    StringBuilder sb = new StringBuilder(); 
    sb.append("Media Player Error: "); 
    switch (what) { 
    case MediaPlayer.MEDIA_ERROR_NOT_VALID_FOR_PROGRESSIVE_PLAYBACK: 
    sb.append("Not Valid for Progressive Playback"); 
    break; 
    case MediaPlayer.MEDIA_ERROR_SERVER_DIED: 
    sb.append("Server Died"); 
    break; 
    case MediaPlayer.MEDIA_ERROR_UNKNOWN: 
    sb.append("Unknown"); 
    break; 
    default: 
    sb.append(" Non standard ("); 
    sb.append(what); 
    sb.append(")"); 
    } 
    sb.append(" (" + what + ") "); 
    sb.append(extra); 
    Log.e(TAG, sb.toString()); 
    return true; 
} 

public void onBufferingUpdate(MediaPlayer mp, int percent) { 
    Log.d(TAG, "PlayerService onBufferingUpdate : " + percent + "%"); 
} 

    } 

main.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"> 
<Button 
    android:text="Play" 
    android:id="@+id/play" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"></Button> 
<Button 
    android:text="Pause" 
    android:id="@+id/pause" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"></Button> 
<Button 
    android:text="Stop" 
    android:id="@+id/stop" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"></Button> 
</LinearLayout> 

Leggi tutto http://developer.android.com/guide/topics/media/index.html controllo asincrono Preparazione

+0

Molti grazie per l'esempio di codice! Ma la mia domanda riguarda il protocollo. A mio parere, un download progressivo significa che è possibile guardare il video mentre il resto del video si sta caricando nel lettore e sta memorizzando nella cache i file temporanei di Internet degli utenti. I.e. Streaming significa che puoi fare clic in qualsiasi punto del video e saltare dove hai cliccato, mentre progressivo significa che devi aspettare che la barra sia davanti all'indicatore per giocarci. Ma cos'è lo streaming progressivo? – headwire

+0

In altre parole, utilizzando Android HTTP Progressive Streaming, sarò in grado di fare clic e passare al segmento scaricato? – headwire

+0

E domanda pratica: posso utilizzare Apple file/stream segmenter per rendere questi file "segmentati" per Live Streaming (tramite server Web Apache) su piattaforma Android? – headwire

3

HTTP streaming progressivo è come giocare file multimediale nel frattempo il download.

Non è realizzazione. 1 ° stagefright non contiene alcun gestore di file .m3u8. 2nd, stagefright non supporta il formato di file .ts.

al momento, è possibile reimplementare il tuo mediaplayer per la riproduzione di live streaming.

+0

Quindi, non c'è streaming reale. Perché "riprodurre file multimediale nel frattempo scarica il file" e "nessun gestore M3U8" significa download progressivo (http). – headwire

Problemi correlati