2013-07-30 71 views
4

製作應用程序並從網站流式傳輸音頻。我有一個菜單,當我點擊按鈕打開無線電活動時,可能需要8-20秒才能加載,有時會強制關閉。任何幫助將是非常感謝。任何方式來加快此代碼。流媒體音頻Android

代碼:

public class Radio extends Activity { 

private MediaPlayer mp; 
private ImageButton pauseicon; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.player_1); 

    pauseicon = (ImageButton) findViewById(R.id.pauseicon); 
    getActionBar().setDisplayHomeAsUpEnabled(true); 

    /** 
    * Play button click event plays a song and changes button to pause 
    * image pauses a song and changes button to play image 
    * */ 

    String res = "http://216.235.91.36/play?s=magic24point7&d=LIVE365&r=0&membername=&session=magic24point7:0&AuthType=NORMAL&app_id=live365%3ABROWSER&SaneID=24.79.96.172-13316781890137014897763&tag=live365"; 

    mp = new MediaPlayer(); 
    try { 
     mp.setAudioStreamType(AudioManager.STREAM_MUSIC); 
     mp.setDataSource(res); 
     mp.prepare(); 
     mp.start(); 

    } catch (IllegalArgumentException e) { 
     e.printStackTrace(); 
    } catch (IllegalStateException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 

    } 



    pauseicon.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      // No need to check if it is pauseicon 

      if (mp.isPlaying()) { 
       mp.pause(); 
       ((ImageButton) v).setImageResource(R.drawable.playicon); 

      } else { 
       mp.start(); 
       ((ImageButton) v).setImageResource(R.drawable.pauseicon); 
      } 
     } 
    }); 

} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 
    case android.R.id.home: 
     NavUtils.navigateUpFromSameTask(this); 

     if (mp != null) 
      if (mp.isPlaying()) 
       mp.stop(); 

     mp.release(); 

     return true; 
    default: 
     return super.onOptionsItemSelected(item); 

    } 
} 

@Override 
public void onBackPressed() { 
    if (mp != null) { 
     if (mp.isPlaying()) 
      mp.stop(); 

     mp.release(); 
    } 

    // there is no reason to call super.finish(); here 
    // call super.onBackPressed(); and it will finish that activity for you 
    super.onBackPressed(); 

} 

} 

回答

4

使用prepareAsync()setOnPreparedListener()代替prepare()prepare()會阻止UI線程,直到它返回並且不推薦用於流。這可能是你的崩潰的原因。

mp = new MediaPlayer(); 
try { 
    mp.setAudioStreamType(AudioManager.STREAM_MUSIC); 
    mp.setDataSource(res); 
    mp.setOnPreparedListener(new OnPreparedListener() { 
     @Override 
     public void onPrepared(MediaPlayer player) { 
      mp.start(); 
     } 
    }); 
    mp.prepareAsync(); 
} catch (IllegalArgumentException e) { 
    e.printStackTrace(); 
} catch (IllegalStateException e) { 
    e.printStackTrace(); 
} catch (IOException e) { 

} 

http://developer.android.com/reference/android/media/MediaPlayer.html#prepare()

準備的播放器進行播放,同步。設置數據源和顯示界面後,您需要調用prepare()或prepareAsync()。對於文件,可以調用prepare(),這會阻塞,直到MediaPlayer準備好播放。

否則,我認爲網絡就是你的瓶頸。加快速度的最快方法是確保您的服務器/客戶端通信很快。你的代碼似乎沒有任何內在的緩慢。

+0

感謝您的幫助,您能告訴我該放哪裏。對不起一種新到Android – user2407147

+0

哦,我會只是代替準備() – user2407147

+0

我已經編輯我的代碼 –