2012-01-04 71 views
0

我希望有人能幫助我解決這個問題。當我更換音頻流站時,Android應用程序變得很慢

這裏是y問題:我有一個帶有幾個通道的icecast服務器。我開發了一個應用程序來收聽這些電臺,但是當我從一個電臺切換到另一個電臺時,該應用程序變得非常慢。

這裏的改變車站代碼:

public static void setAndPlay(MediaPlayer player, String source) { 
     player.reset(); 
     try { 
      player.setDataSource(source); 
      player.prepare(); 
     } catch (IOException e) { 
      //TODO: handle exception 
     } 
} 

下面是在MediaPlayer開始:

player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { 
      public void onPrepared(MediaPlayer player) { 
       player.start(); 
      } 
     }); 
+0

是什麼'player.reset()'做? – 2012-01-04 20:52:26

+0

重置()後,該對象就像剛剛創建。 – nico 2012-01-05 17:16:03

+0

也許這是有幫助的[鏈接] – nico 2012-01-05 17:16:45

回答

1
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> 
+0

你可以在這裏找到它:http://stackoverflow.com/questions/8681550/android-2-2- mediaplayer-is-working-fine-with-one-shoutcast-url-but-not-with- – 2012-01-06 12:06:43

+0

謝謝,它解決了部分問題。現在該應用程序不會變慢,但我需要音樂開始更快。我怎樣才能做到這一點? – nico 2012-01-06 20:47:55

相關問題