5

制作应用程序并从站点流式传输音频。我有一个菜单,当我单击按钮打开无线电活动时,加载可能需要 8-20 秒,有时会强制关闭。任何帮助都会非常感谢。

代码:

  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

1 回答 1

5

使用prepareAsync()andsetOnPreparedListener()而不是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 准备好播放。

否则我认为网络是你的瓶颈。加快速度的最快方法是确保您的服务器/客户端通信快速。您的代码似乎没有任何固有的缓慢。

于 2013-07-30T16:02:09.323 回答