2011-12-30 7 views
12

Si prega di consultare il codice che sto usando per lo streaming di streaming, funziona con un URL ma NON con l'altro.Android 2.2 MediaPlayer funziona perfettamente con un URL SHOUTcast ma non con l'altro

Questo funziona:

Uri myUri = Uri.parse("http://fr3.ah.fm:9000/"); 

Ma non con questo:

Uri myUri = Uri.parse("http://someOtherURL"); 

SimpleMusicStream.java

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> 

The Log-gatto mostra gli errori:

NuHTTPDataSource(33): Server did not give us the content length! 

Media Player Error: Unknown (1) -2147483648 
Media Player Error: Unknown (1) -1002 

qualcuno può aiutarmi a risolvere il problema?

EDIT:

Solo per condividere con voi la gente, che la nostra current code works with Android 2.1 & minor versions, but not works with Android 2.2 or higher.

Grazie

+1

In base a un po 'di ricerca rapida su Google, la codifica audio '' audio/aacp'' sta causando un certo mal di testa anche per gli altri. – harism

+1

Un'altra discussione davvero buona può essere trovata qui: http://stackoverflow.com/questions/1650983/server-side-aac-audio-with-android – poitroae

+0

help me a questo http://stackoverflow.com/questions/16264225/ andriod-shoutcast-internet-radio-filenotfoundexception – String

risposta

7

Shoutcast mp3 in streaming da Android 2.2 in poi è supportato in modo nativo.

Sotto 2.2 il sistema operativo Android non può riprodurre i flussi shoutcast in modo nativo senza utilizzare un proxy sul lato del flusso o una classe proxy del flusso sul dispositivo per catturare il flusso e passarlo all'audioplayer proprio come fa NPR.

lo streaming audio/aacp non è supportato direttamente. Per questo è possibile utilizzare la libreria ffmpeg, opencore o faad2 per decodificarla in PCM e riprodurre utilizzando audiotrack. Reference

+0

I flussi di Shoutcast sono supportati dalla 2.2 in poi lo so, ma perché funziona con uno di URL e non lavorare l'altro? –

+0

Come ho detto, solo lo streaming mp3 Shoutcast è supportato da Android 2.2. Uno dei tuoi url è del tipo di contenuto "audio/aacp" che, suppongo, non giochi, un altro è "audio/mpeg" che è supportato in Android 2.2 in poi. Per AACP hai salvato il contenuto nel file e poi riproduci O usi eventuali librerie di decodifica che ho menzionato ... ma considera anche i problemi legali durante l'utilizzo. –

+0

@SanthoshShettigar aiutami a questo http://stackoverflow.com/questions/16264225/andriod-shoutcast-internet-radio-filenotfoundexception – String

1

problema è la codifica in formato audio/AACP

Il problema è che non sia reso AACP da Android Media Player correttamente. Dovrai provare qualche altro codec o scrivere il tuo decoder.

+0

per favore aiutatemi a questo http://stackoverflow.com/questions/16264225/andriod-shoutcast-internet-radio-filenotfoundexception @ the100rabh – String

1

Mediaplayer supporta solo these formats come indicato nei documenti dello sviluppatore Android.

Alcuni flussi possono essere soddisfacente, ma un po 'di volontà non

0

C'è un Freeware Advanced Audio (AAC) Decoder for Android. Puoi usarlo per aiuto e guida o usare la classe Multiplayer per riprodurre audio aac. C'è un'app di esempio e una libreria per aiutarti a capire. Ho usato questa libreria per riprodurre i flussi online di Shoutcast. Ho giocato sia AAC e MP3 utilizzando classe Multiplayer previsto nella biblioteca

Spero che questo aiuti

0

Infine, ho avuto semplice applicazione di streaming SHOUTcast. Prova this. Basta cambiare l'URL di streaming in StreamService.java.

Problemi correlati