Mediaplayer流在线MP3文件



我有一个Android应用程序,可以流式传输MP3文件并在player中播放该文件,但问题是mediaPlayer.prepare();需要很长时间的缓冲和应用程序冻结所以我尝试使用prepareAsync();,但有了这个功能,我不能让播放器播放下一个文件。

它只是在线播放一个文件;如果我需要播放另一个文件,我必须在播放结束时关闭并重新启动活动。这是我的代码:

public void playMp3(String _link)
{
mediaPlayer.setOnPreparedListener(new OnPreparedListener() {
    public void onPrepared(MediaPlayer mp) {
        if(!mediaPlayer.isPlaying()){
        mediaPlayer.start();
        Progressbar.setVisibility(View.INVISIBLE);
        play.setVisibility(View.GONE);
        stop.setVisibility(View.VISIBLE);
        songProgressBar.setProgress(0);
        songProgressBar.setMax(100);
        }
        updateProgressBar();      
    }
});
mediaPlayer.setOnCompletionListener(new OnCompletionListener() {
    @Override
    public void onCompletion(MediaPlayer mp) {
        // TODO Auto-generated method stub
        mediaPlayer.reset();
        songProgressBar.setProgress(0);
        songProgressBar.setSecondaryProgress(0);
        play.setVisibility(View.VISIBLE);
        stop.setVisibility(View.GONE);
        link = "http://server11.mp3quran.net/hawashi/002.mp3";
        playMp3(link);
    }
});
mediaPlayer.setOnBufferingUpdateListener(new OnBufferingUpdateListener() {
    @Override
    public void onBufferingUpdate(MediaPlayer mp, int percent) {
       // Toast.makeText(getApplicationContext(), "n" + percent, Toast.LENGTH_LONG).show();
        songProgressBar.setSecondaryProgress(percent);
         if(percent==100)
         {
             Progressbar.setVisibility(View.INVISIBLE);
         }else if(percent > songProgressBar.getProgress())
         {
             Progressbar.setVisibility(View.INVISIBLE);
         }else
         {
             Progressbar.setVisibility(View.VISIBLE);
         }
    }
});
mediaPlayer.reset();
Progressbar.setVisibility(View.VISIBLE);
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
    mediaPlayer.setDataSource(_link);
    //mediaPlayer.prepare(); // might take long! (for buffering, etc)   //@@
    mediaPlayer.prepareAsync();
} catch (IllegalArgumentException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (SecurityException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IllegalStateException e) {
    // TODO Auto-generated catch block///
    e.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}}

实际上我不知道代码中的错误在哪里但我会解释我是如何在我的应用程序

public void playMp3(String _link){
                mediaPlayer.reset();
                Progressbar.setVisibility(View.VISIBLE);
                mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            try {
                mediaPlayer.setDataSource(_link);
                mediaPlayer.setOnBufferingUpdateListener(this);
                mediaPlayer.setOnPreparedListener(this);
                //mediaPlayer.prepare(); // might take long! (for buffering, etc)   //@@
                mediaPlayer.prepareAsync();
            } catch (IllegalArgumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (SecurityException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block///
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

然后在你的类中实现OnCompletionListener, OnPreparedListener和OnBufferingUpdateListener

public class PlayerActivity extends Activity implements OnCompletionListener, OnPreparedListener, OnBufferingUpdateListener{
.
.
.

并实现所有方法

public void onPrepared(MediaPlayer mediaplayer) {
    if(!mediaPlayer.isPlaying()){
        mediaPlayer.start();
        Progressbar.setVisibility(View.INVISIBLE);
        play.setVisibility(View.GONE);
        stop.setVisibility(View.VISIBLE);
        songProgressBar.setProgress(0);
        songProgressBar.setMax(100);
        }
        updateProgressBar();      
}
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
    //mediaPlayer.reset();
        songProgressBar.setProgress(0);
        songProgressBar.setSecondaryProgress(0);
        play.setVisibility(View.VISIBLE);
        stop.setVisibility(View.GONE);
        link = "http://server11.mp3quran.net/hawashi/002.mp3";
        playMp3(link);
}
@Override
public void onBufferingUpdate(MediaPlayer mp, int percent) {
     songProgressBar.setSecondaryProgress(percent);
         if(percent==100)
         {
             Progressbar.setVisibility(View.INVISIBLE);
         }else if(percent > songProgressBar.getProgress())
         {
             Progressbar.setVisibility(View.INVISIBLE);
         }else
         {
             Progressbar.setVisibility(View.VISIBLE);
         }
}

我希望这对你有帮助

相关内容

  • 没有找到相关文章

最新更新