如何在安卓手机上循环播放歌曲(音乐应用)?

huangapple go评论147阅读模式
英文:

How to repeat songs on android (music app)

问题

我可以播放下一首歌曲或上一首歌曲,但我只是无法弄清楚如何重复播放同一首歌曲,我已经卡在这个问题上一天了。任何帮助都将不胜感激

播放下一首歌曲:

public void playNext(View view) {
    Song nextSong = songCollection.getNextSong(songId);
    if (nextSong != null) {
        songId = nextSong.getId();
        title = nextSong.getTitle();
        artiste = nextSong.getArtiste();
        fileLink = nextSong.getFileLink();
        coverArt = nextSong.getCoverArt();
        url = BASE_URL + fileLink;
        displaySong(title, artiste, coverArt);
        stopActivities();
        playOrPauseMusic(view);
    }
}

播放上一首歌曲:

public void playPrev(View view) {
    Song prevSong = songCollection.getPrevSong(songId);
    if (prevSong != null) {
        songId = prevSong.getId();
        title = prevSong.getTitle();
        artiste = prevSong.getArtiste();
        fileLink = prevSong.getFileLink();
        coverArt = prevSong.getCoverArt();
        url = BASE_URL + fileLink;
        displaySong(title, artiste, coverArt);
        stopActivities();
        playOrPauseMusic(view);
    }
}

SongCollection:

public class SongCollection {
    // 实例变量:用于存储2个歌曲对象的数组
    private Song songArray[] = new Song[2];

    // SongCollection类的构造函数
    public SongCollection() {
        prepareSongs();
    }

    // 创建歌曲对象并将它们存储到songArray中
    public void prepareSongs() {
        // 创建第一个歌曲对象
        Song theWayYouLookTonight = new Song("S1001", "The Way You Look Tonight", "Michael Buble",
                "a5b8972e764025020625bbf9c1c2bbb06e394a60?cid=2afe87a64b0042dabf51f37318616965", 4.66,
                "michael_buble_collection");
        // 创建第二个歌曲对象
        Song billieJean = new Song("S1002", "Billie Jean", "Michael Jackson",
                "f504e6b8e037771318656394f532dede4f9bcaea?cid=2afe8", 4.9, "billie_jean");

        // 将歌曲对象插入到SongArray中
        songArray[0] = theWayYouLookTonight;
        songArray[1] = billieJean;
    }

    // 搜索并返回具有指定id的歌曲
    public Song searchById(String id) {
        Song song = null;
        for (int index = 0; index < songArray.length; index++) {
            song = songArray[index];
            if (song.getId().equals(id)) {
                return song;
            }
        }

        // 如果在SongArray中找不到歌曲,
        // 将返回null歌曲对象
        return null;
    }

    public Song getNextSong(String currentSongId) {
        Song song = null;
        for (int index = 0; index < songArray.length; index++) {
            String tempSongId = songArray[index].getId();
            if (tempSongId.equals(currentSongId) && (index < songArray.length - 1)) {
                song = songArray[index + 1];
                break;
            }
        }
        return song;
    }

    public Song getPrevSong(String currentSongId) {
        Song song = null;
        for (int index = 0; index < songArray.length; index++) {
            String tempSongId = songArray[index].getId();
            if (tempSongId.equals(currentSongId) && (index > 0)) {
                song = songArray[index - 1];
                break;
            }
        }
        return song;
    }
}
英文:

I can play the next song or previous song but I just can't figure out how to repeat the same song and I've been stuck on this for a day. Any help is greatly appreciated

Playing next song:

 public void playNext (View view){
Song nextSong = songCollection.getNextSong(songId);
if (nextSong != null) {
songId = nextSong.getId();
title = nextSong.getTitle();
artiste = nextSong.getArtiste();
fileLink = nextSong.getFileLink();
coverArt = nextSong.getCoverArt();
url = BASE_URL + fileLink;
displaySong(title,artiste,coverArt);
stopActivities();
playOrPauseMusic(view);

Playing previous song

 public void playPrev (View view ){
Song prevSong = songCollection.getPrevSong(songId);
if ( prevSong != null) {
songId = prevSong.getId();
title = prevSong.getTitle();
artiste = prevSong.getArtiste();
fileLink = prevSong.getFileLink();
coverArt = prevSong.getCoverArt();
url = BASE_URL + fileLink;
displaySong(title, artiste, coverArt);
stopActivities();
}   playOrPauseMusic(view);
}

SongCollection:

public class SongCollection {
// Instance variable: An array to store 2 Song objects
private Song songArray [] = new Song[2];
//Constructor of SongCollection class
public SongCollection()  { prepareSongs ();  }
//Create Song objects and store them into songArray
public void prepareSongs () {
//Create the first Song object
Song theWayYouLookTonight = new Song(&quot;S1001&quot;,&quot;The Way You Look Tonight&quot;,&quot;Michael Buble&quot;,
&quot;a5b8972e764025020625bbf9c1c2bbb06e394a60?cid=2afe87a64b0042dabf51f37318616965&quot;,4.66,
&quot;michael_buble_collection&quot;);
//Create the second Song object
Song billieJean = new Song(&quot;S1002&quot;, &quot;Billie Jean&quot;,&quot;Michael Jackson&quot;,
&quot;f504e6b8e037771318656394f532dede4f9bcaea?cid=2afe8&quot;, 4.9, &quot;billie_jean&quot;);
//Insert the song objects into the SongArray
songArray[0] = theWayYouLookTonight;
songArray[1] = billieJean;
}
//Search and return the song with the specified id.
public Song searchById (String id) {
Song song = null;
for (int index = 0; index &lt; songArray.length; index++) {
song = songArray[index];
if (song.getId().equals(id)) {
return song;
}
}
//If the song cannot be found in the SongArray,
//The null song object will be returned
return null;
}
public Song getNextSong (String currentSongId) {
Song song = null;
for (int index = 0; index &lt; songArray.length; index++){
String tempSongId = songArray[index].getId();
if (tempSongId.equals(currentSongId)&amp;&amp; (index &lt; songArray.length -1)) {
song = songArray[index+1];
break;
}
}
return song;
}
public Song getPrevSong (String currentSongId){
Song song = null;
for (int index = 0; index &lt; songArray.length; index++){
String tempSongId = songArray[index].getId();
if (tempSongId.equals(currentSongId)&amp;&amp; (index &gt; 0)){
song = songArray[index -1];
break;
}
}
return song;
}    
}

答案1

得分: 0

你可以使用这个方法来获取当前的歌曲,但我认为这不是一个好的选项。你的 SongCollection 类应该包含所有的内容,Activity/Fragment 不应该使用 currentSongId 来获取下一首或上一首歌曲。

英文:

You can use this to get the current song but still, I am thinking it is not good option. Your class SongCollection should carry all the stuff and Activity/Fragment shouldn't use currentSongId to get next or previous song.

public Song getCurrentSong (String currentSongId) {
Song song = null;
for (int index = 0; index &lt; songArray.length; index++){
String tempSongId = songArray[index].getId();
if (tempSongId.equals(currentSongId)) {
return songArray[index];
}
}
}

huangapple
  • 本文由 发表于 2020年8月4日 02:09:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/63234736.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定