按流时MP3不播放

时间:2012-02-23 23:08:18

标签: java android mp3

我正在尝试从流媒体播放MP3,但问题是在我按下播放后会自动播放。此外,播放按钮不会播放仅将其设置为流式传输的音频。如何让MP3自动播放?

    play.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            if (!mediaPlayer.isPlaying()) {
                try {
                    mediaPlayer.setDataSource(url); 
                    mediaPlayer.prepareAsync(); 
                } catch (Exception e) {
                    e.printStackTrace();
                }

                mediaFileLengthInMilliseconds = mediaPlayer.getDuration();

                if(!mediaPlayer.isPlaying()){
                    mediaPlayer.start();
                    play.setImageResource(R.drawable.ic_media_pause);
                }else {
                    mediaPlayer.pause();
                    play.setImageResource(R.drawable.ic_media_play);
                }

                primarySeekBarProgressUpdater();
            }
    }});

    sb.setMax(99);
    sb.setOnTouchListener(this);

    mediaPlayer = new MediaPlayer();
    mediaPlayer.setOnBufferingUpdateListener(this);
    mediaPlayer.setOnCompletionListener(this);
}

private void primarySeekBarProgressUpdater() {
    sb.setProgress((int)(((float)mediaPlayer.getCurrentPosition()/mediaFileLengthInMilliseconds)*100));
    if (mediaPlayer.isPlaying()) {
        notification = new Runnable() {
            public void run() {
                primarySeekBarProgressUpdater();
            }
        };
        handler.postDelayed(notification,1000);
    }
}

public boolean onTouch(View v, MotionEvent event) {
    if(v.getId() == R.id.progress_bar){
        /** Seekbar onTouch event handler. Method which seeks MediaPlayer to seekBar primary progress position*/
        if(mediaPlayer.isPlaying()){
            SeekBar sb = (SeekBar)v;
            int playPositionInMillisecconds = (mediaFileLengthInMilliseconds / 100) * sb.getProgress();
            mediaPlayer.seekTo(playPositionInMillisecconds);
        }
    }
    return false;
}

public void onCompletion(MediaPlayer mp) {
    play.setImageResource(R.drawable.ic_media_play);

    mediaPlayer.stop();
}

public void onBufferingUpdate(MediaPlayer mp, int percent) {
    sb.setSecondaryProgress(percent);
}
   }

我做错了吗?我点击播放,我看到我的搜索栏填满了,但它不会播放。

我也收到了这条消息:

E/MediaPlayer(28985): stop called in state 0 E/MediaPlayer(28985):
error (-38, 0) E/MediaPlayer(28985): Error (-38,0)
E/MediaPlayer(28985): stop called in state 0 E/MediaPlayer(28985):
error (-38, 0) E/MediaPlayer(28985): Error (-38,0)
E/MediaPlayer(28985): stop called in state 0 E/MediaPlayer(28985):
error (-38, 0) E/MediaPlayer(28985): Error (-38,0)
E/MediaPlayer(28985): stop called in state 0 E/MediaPlayer(28985):
error (-38, 0) E/MediaPlayer(28985): Error (-38,0)
E/MediaPlayer(28985): stop called in state 0 E/MediaPlayer(28985):
error (-38, 0) E/MediaPlayer(28985): Error (-38,0)
E/MediaPlayer(28985): stop called in state 0 E/MediaPlayer(28985):
error (-38, 0) E/MediaPlayer(28985): Error (-38,0)

1 个答案:

答案 0 :(得分:0)

看起来您需要实施MediaPlayer.OnPreparedListener。由于您使用的是prepareAsync(),因此您需要等到MediaPlayer准备好后再拨打start()。当您获得onPrepared()回调时,此时可以安全地调用start()方法。