如何在android(音乐应用程序)上重复歌曲



我可以播放下一首或上一首歌,但我不知道如何重复同一首歌。非常感谢任何帮助

播放下一首歌曲:

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);
}

歌曲集:

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("S1001","The Way You Look Tonight","Michael Buble",
"a5b8972e764025020625bbf9c1c2bbb06e394a60?cid=2afe87a64b0042dabf51f37318616965",4.66,
"michael_buble_collection");
//Create the second Song object
Song billieJean = new Song("S1002", "Billie Jean","Michael Jackson",
"f504e6b8e037771318656394f532dede4f9bcaea?cid=2afe8", 4.9, "billie_jean");
//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 < 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 < 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;
}    
}

您可以使用它来获取当前歌曲,但我仍然认为这不是一个好的选择。你的课程SongCollection应该携带所有的东西,而活动/片段不应该使用currentSongId来获得下一首或上一首歌曲。

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

最新更新