更改Android MP3服务MediaPlayer中的URL

时间:2013-07-09 09:19:19

标签: android service stream mp3 switch-statement

我有一个服务,它开始在Android的后台播放MP3流。有一个带有频道选择和播放器活动的菜单活动,它开始播放通过按钮点击选择的MP3。 它使用MediaPlayer类。以下是URL的启动方式。

mp3Service.playSong(getBaseContext(),url);

现在,当回到带有URL的菜单时,我想更改URL播放。我不想停止服务,只改变它的URL。可能吗?我已经阅读了MediaPlayer的文档,但不知道该怎么做,尝试了一些变种。

如何切换到其他网址? mp3Service.pauseSong(getBaseContext());

然后STOP,和mp3Service.playSong(getBaseContext(),newurl)?

////////////////////////////// PART OF THE CLASS

 public String currenturl="";

public void playSong(Context c, String url) {
        if (currenturl.equals(""))
        {
        if(!created){
            this.mplayer = MediaPlayer.create(c, Uri.parse(url));
            created = true;
            currenturl=url;
        }
            this.mplayer.start();
        }
        else
            {
            if (!currenturl.equals(url))
            {

                if(!created){
                    this.mplayer.stop();
                    this.mplayer = MediaPlayer.create(c, Uri.parse(url));
                    created = true;
                    currenturl=url;
                }
                    this.mplayer.start();


            }

            };
    }

public void pauseSong(Context c) {
        this.mplayer.pause();
    }

    //
    public void stopSong(Context c) {
        this.mplayer.stop();
    } 

不那么容易......

1 个答案:

答案 0 :(得分:0)

关于你的评论,这是因为created在第一次播放后为真,所以你永远不会执行你的else逻辑。我就是这样做的,我认为没有必要存储单独的created值。

public String currenturl = "";

public void playSong(Context c, String url) {
    if (currenturl.equals(url)) {
        // it's the same
    }
    else {
        // it's not the same
        currenturl = url;
    }
}