附加注释
从列表中删除歌曲时,不能在数组中留下空点。你将如何处理这件事?不要使用ArrayList来管理您的歌曲,您必须使用数组。不要为此赋值扩展ArrayList。我们稍后会这样做;-)如果您想这样做,您可以自由地编写额外的“帮助器”方法。我列出的方法是作业的最低要求。如果有帮助,您可以随时添加更多的方法。
为歌曲、播放列表和驱动程序类提交什么.java文件。
public class Song {
public String name;
public String artist;
public String album;
public int length;
public Song(String songName, String artistName, String albumName, int trackLength) {
this.name = name;
this.artist = artist;
this.album = album;
this.length = length;
}
public void setName(String songName) {
name = songName;
}
public String getName() {
return name;
}
public void setArtist(String artistName) {
artist = artistName;
}
public String getArtist() {
return artist;
}
public void setAlbum(String albumName) {
album = albumName;
}
public String getAlbum() {
return album;
}
public void setLength(int h, int m, int s) {
length = (h*3600 + m*60 + s);
if(h==0) {
length = (m*60+s);
}
}
public int getLength() {
return length;
}
public String toString() {
return "Title: " + getName() + ", Artist: " + getArtist()
+ ", Album: " + getAlbum() + ", Track Length: " + getLength();
}
}
public class Playlist {
private Song[] songs;
private int count;
private String playlistName;
public Playlist() {
songs = new Song[12];
count = 0;
}
public String getPlaylistName() {
return playlistName;
}
public void setPlayListName() {
this.playlistName = playlistName;
}
public void add(Song a) {
if(count == songs.length) {
System.out.println("ERROR: Collection is full. Songs were not added to the Playlist.");
}
songs[count] = a;
count++;
}
public Song get(int i) {
if(count > i) {
return songs[i];
}
else {
return null;
}
}
public Song remove(String name) {
boolean found = false;
int indexToRemove = 0;
while(indexToRemove < count && !found) {
if(songs[indexToRemove].getName().equals(name)) {
found = true;
}
else {
indexToRemove++;
}
}
if(indexToRemove < count) {
for(int from = indexToRemove + 1; from < count; from++) {
songs[from-1] = songs[from];
}
songs[count-1] = null;
count--;
}
return null;
}
public void print() {
String result = "NumSongs = " + count
+ " / PlayList song limit = " + songs.length + "\n";
for (int i=0; i<count; i++) {
result += ("songList[" + i + "] = <"
+ songs[i] + ">\n");
}
System.out.println(result.toString() + "\n");
}
public int size() {
return count;
}
public int totalTime() {
int totalTime = 0;
for (int i=0; i<count; i++) {
totalTime = songs[i].getLength();
}
return totalTime;
}
public String formattedTotalTime() {
long h, m, s;
String lengthString;
s = Song.length;
m = s/60;
s = s%60;
h = m/60;
m = m%60;
lengthString = String.format("%02d",h) + ":" +
String.format("%02d",m) + ":" +
String.format("%02d",s);
return lengthString;
}
public void clear() {
for (int i=0; i<songs.length; i++) {
songs[i] = null;
count = 0;
}
return ;
}
}
驱动程序类
public class SongDriver {
public static void main(String[] args) {
Playlist one = new Playlist();
Song song1 = new Song("Hotline Bling", "Drake", "Hotline Bing - Single", 267000);
Song song2 = new Song("What Do You Mean?", "Justin Bieber", "What Do You Mean? - Single", 207000);
Song song3 = new Song("Watch Me", "Silento", "Watch Me (Whip / Nae Nae) - Single", 185000);
Song song4 = new Song("679", "Fetty Wap ft. Remy Boyz", "Fetty Wap", 185000);
Song song5 = new Song("Can't Feel My Face", "The Weeknd", "Beauty Behind the Madness", 213000);
Song song6 = new Song("Good for You", "Selena Gomez ft. A$AP Rocky", "Good for You - Single", 221000);
Song song7 = new Song("If You", "Big Bang", "MADE", 264000);
one.add(song1);
one.add(song2);
one.add(song3);
one.add(song4);
one.add(song5);
one.add(song6);
one.add(song7);
Playlist.print();
one.remove("679");
one.remove("Watch Me");
Playlist.print();
Playlist.clear();
}
}
我的问题是我的输出没有按照我想要的方式出来...
----jGRASP exec: java SongDriver
NumSongs = 7 / PlayList song limit = 12
songList[0] = <Title: null, Artist: null, Album: null, Track Length: 0>
songList[1] = <Title: null, Artist: null, Album: null, Track Length: 0>
songList[2] = <Title: null, Artist: null, Album: null, Track Length: 0>
songList[3] = <Title: null, Artist: null, Album: null, Track Length: 0>
songList[4] = <Title: null, Artist: null, Album: null, Track Length: 0>
songList[5] = <Title: null, Artist: null, Album: null, Track Length: 0>
songList[6] = <Title: null, Artist: null, Album: null, Track Length: 0>
Exception in thread "main" java.lang.NullPointerException
at Playlist.remove(Playlist.java:38)
at SongDriver.main(SongDriver.java:24)
----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.
列表没有显示出来,所有的东西都是空的,我不明白为什么。音轨长度不是以HH:MM:SS表示的,全部都是零。我将歌曲的每段长度转换为毫秒,并将其作为毫秒放在Driver类中。我也不明白为什么它一直抛出nullpointerException
if(songs[indexToRemove].getName().equals(name)) {
任何帮助都会得到的!多谢了。
----jGRASP exec: javac -g @Playlist_source_files_1673083996069575159jgr
SongDriver.java:22: error: non-static method print() cannot be referenced from a static context
Playlist.print();
^
SongDriver.java:27: error: non-static method print() cannot be referenced from a static context
Playlist.print();
^
SongDriver.java:28: error: non-static method clear() cannot be referenced from a static context
Playlist.clear();
^
Playlist.java:78: error: non-static variable length cannot be referenced from a static context
s = Song.length;
^
4 errors
----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.
public void setLength(int trackLength) {
length = trackLength;
}
public String formattedTotalTime() {
long time = totalTime();
String lengthString;
double overflow = time;
long h = time / HOURS;
long overFlow = time % HOURS;
long m = overFlow / MINS;
overFlow = time % MINS;
long s = overFlow / SECS;
lengthString = String.format("%02d",h) + ":" +
String.format("%02d",m) + ":" +
String.format("%02d",s);
return lengthString;
}
public static long HOURS = 60 * 60 * 1000;
public static long MINS = 60 * 1000;
public static long SECS = 1000;
> ----jGRASP exec: java SongDriver
NumSongs = 7 / PlayList song limit = 12
songList[0] = <Title: Hotline Bling, Artist: Drake, Album: Hotline Bing - Single, Track Length: 267000>
songList[1] = <Title: What Do You Mean?, Artist: Justin Bieber, Album: What Do You Mean? - Single, Track Length: 207000>
songList[2] = <Title: Watch Me, Artist: Silento, Album: Watch Me (Whip / Nae Nae) - Single, Track Length: 185000>
songList[3] = <Title: 679, Artist: Fetty Wap ft. Remy Boyz, Album: Fetty Wap, Track Length: 185000>
songList[4] = <Title: Can't Feel My Face, Artist: The Weeknd, Album: Beauty Behind the Madness, Track Length: 213000>
songList[5] = <Title: Good for You, Artist: Selena Gomez ft. A$AP Rocky, Album: Good for You - Single, Track Length: 221000>
songList[6] = <Title: If You, Artist: Big Bang, Album: MADE, Track Length: 264000>
NumSongs = 6 / PlayList song limit = 12
songList[0] = <Title: Hotline Bling, Artist: Drake, Album: Hotline Bing - Single, Track Length: 267000>
songList[1] = <Title: What Do You Mean?, Artist: Justin Bieber, Album: What Do You Mean? - Single, Track Length: 207000>
songList[2] = <Title: Watch Me, Artist: Silento, Album: Watch Me (Whip / Nae Nae) - Single, Track Length: 185000>
songList[3] = <Title: Can't Feel My Face, Artist: The Weeknd, Album: Beauty Behind the Madness, Track Length: 213000>
songList[4] = <Title: Good for You, Artist: Selena Gomez ft. A$AP Rocky, Album: Good for You - Single, Track Length: 221000>
songList[5] = <Title: If You, Artist: Big Bang, Album: MADE, Track Length: 264000>
NumSongs = 5 / PlayList song limit = 12
songList[0] = <Title: Hotline Bling, Artist: Drake, Album: Hotline Bing - Single, Track Length: 267000>
songList[1] = <Title: What Do You Mean?, Artist: Justin Bieber, Album: What Do You Mean? - Single, Track Length: 207000>
songList[2] = <Title: Can't Feel My Face, Artist: The Weeknd, Album: Beauty Behind the Madness, Track Length: 213000>
songList[3] = <Title: Good for You, Artist: Selena Gomez ft. A$AP Rocky, Album: Good for You - Single, Track Length: 221000>
songList[4] = <Title: If You, Artist: Big Bang, Album: MADE, Track Length: 264000>
NumSongs = 0 / PlayList song limit = 12
----jGRASP: operation complete.
public class Playlist {
public static long HOURS = 60 * 60 * 1000;
public static long MINS = 60 * 1000;
public static long SECS = 1000;
private Song[] songs;
private int count;
private String playlistName;
public Playlist() {
songs = new Song[12];
count = 0;
}
public String getPlaylistName() {
return playlistName;
}
public void setPlayListName() {
this.playlistName = playlistName;
}
public void add(Song a) {
if(count == songs.length) {
System.out.println("ERROR: Collection is full. Songs were not added to the Playlist.");
}
songs[count] = a;
count++;
}
public Song get(int i) {
if(count > i) {
return songs[i];
}
else {
return null;
}
}
public Song remove(String name) {
boolean found = false;
int indexToRemove = 0;
while(indexToRemove < count && !found) {
if(songs[indexToRemove].getName().equals(name)) {
found = true;
}
else {
indexToRemove++;
}
}
if(indexToRemove < count) {
for(int from = indexToRemove + 1; from < count; from++) {
songs[from-1] = songs[from];
}
songs[count-1] = null;
count--;
}
return null;
}
public void print() {
String result = "NumSongs = " + count
+ " / PlayList song limit = " + songs.length + "\n";
for (int i=0; i<count; i++) {
result += ("songList[" + i + "] = <"
+ songs[i] + ">\n");
}
System.out.println(result.toString() + "\n");
}
public int size() {
return count;
}
public int totalTime() {
int totalTime = 0;
for (int i=0; i<count; i++) {
totalTime += songs[i].getLength();
}
return totalTime;
}
public String formattedTotalTime() {
long time = totalTime();
String lengthString;
double overflow = time;
long h = time / HOURS;
long overFlow = time % HOURS;
long m = overFlow / MINS;
overFlow = time % MINS;
long s = overFlow / SECS;
lengthString = String.format("%02d",h) + ":" +
String.format("%02d",m) + ":" +
String.format("%02d",s);
return lengthString;
}
public void clear() {
for (int i=0; i<songs.length; i++) {
songs[i] = null;
count = 0;
}
return ;
}
}
public class Song {
public String name;
public String artist;
public String album;
public int length;
public Song(String songName, String artistName, String albumName, int trackLength) {
this.name = songName;
this.artist = artistName;
this.album = albumName;
this.length = trackLength;
}
public void setName(String songName) {
name = songName;
}
public String getName() {
return name;
}
public void setArtist(String artistName) {
artist = artistName;
}
public String getArtist() {
return artist;
}
public void setAlbum(String albumName) {
album = albumName;
}
public String getAlbum() {
return album;
}
public void setLength(int trackLength) {
length = trackLength;
}
public int getLength() {
return length;
}
public String toString() {
return "Title: " + getName() + ", Artist: " + getArtist()
+ ", Album: " + getAlbum() + ", Track Length: " + getLength();
}
}
驱动程序类:
public class SongDriver {
public static void main(String[] args) {
Playlist one = new Playlist();
Song song1 = new Song("Hotline Bling", "Drake", "Hotline Bing - Single", 267000);
Song song2 = new Song("What Do You Mean?", "Justin Bieber", "What Do You Mean? - Single", 207000);
Song song3 = new Song("Watch Me", "Silento", "Watch Me (Whip / Nae Nae) - Single", 185000);
Song song4 = new Song("679", "Fetty Wap ft. Remy Boyz", "Fetty Wap", 185000);
Song song5 = new Song("Can't Feel My Face", "The Weeknd", "Beauty Behind the Madness", 213000);
Song song6 = new Song("Good for You", "Selena Gomez ft. A$AP Rocky", "Good for You - Single", 221000);
Song song7 = new Song("If You", "Big Bang", "MADE", 264000);
one.add(song1);
one.add(song2);
one.add(song3);
one.add(song4);
one.add(song5);
one.add(song6);
one.add(song7);
one.print();
one.remove("679");
one.print();
one.remove("Watch Me");
one.print();
one.clear();
one.print();
}
}
首先,删除对类字段的静态
引用
public class Song {
public static String name;
public static String artist;
public static String album;
public static int length;
这基本上意味着song
的每个实例将具有彼此完全相同的值(那些最后应用的值)
接下来,在song
的构造函数中,实际为字段分配参数...
public Song(String songName, String artistName, String albumName, int trackLength) {
this.name = name;
this.artist = artist;
this.album = album;
this.length = length;
}
public Song(String songName, String artistName, String albumName, int trackLength) {
this.name = songName;
this.artist = artistName;
this.album = albumName;
this.length = trackLength;
}
接下来,我将删除方法的所有static
修饰符...
public static String getArtist() {
public String getArtist() {
但现在只有最后一件事了。音轨长度时间应为HH:MM:SS格式,但仍以毫秒为单位
我已经很久不需要这么做了,但在过去,我用过类似...
public static long HOURS = 60 * 60 * 1000;
public static long MINS = 60 * 1000;
public static long SECS = 1000;
public static void main(String[] args) {
long time = (1 * HOURS) + (30 * MINS);
double overflow = time;
long h = time / HOURS;
long overFlow = time % HOURS;
long m = overFlow / MINS;
overFlow = time % MINS;
long s = overFlow / SECS;
System.out.printf("%02d:%02d.%02d%n", h, m, s);
}
package javaapplication620;
public class SongDriver {
public static void main(String[] args) {
Playlist one = new Playlist();
Song song1 = new Song("Hotline Bling", "Drake", "Hotline Bing - Single", 267000);
Song song2 = new Song("What Do You Mean?", "Justin Bieber", "What Do You Mean? - Single", 207000);
Song song3 = new Song("Watch Me", "Silento", "Watch Me (Whip / Nae Nae) - Single", 185000);
Song song4 = new Song("679", "Fetty Wap ft. Remy Boyz", "Fetty Wap", 185000);
Song song5 = new Song("Can't Feel My Face", "The Weeknd", "Beauty Behind the Madness", 213000);
Song song6 = new Song("Good for You", "Selena Gomez ft. A$AP Rocky", "Good for You - Single", 221000);
Song song7 = new Song("If You", "Big Bang", "MADE", 264000);
one.add(song1);
one.add(song2);
one.add(song3);
one.add(song4);
one.add(song5);
one.add(song6);
one.add(song7);
one.print();
one.remove("679");
one.remove("Watch Me");
one.print();
one.clear();
}
public static long HOURS = 60 * 60 * 1000;
public static long MINS = 60 * 1000;
public static long SECS = 1000;
public static class Playlist {
private Song[] songs;
private int count;
private String playlistName;
public Playlist() {
songs = new Song[12];
count = 0;
}
public String getPlaylistName() {
return playlistName;
}
public void setPlayListName() {
this.playlistName = playlistName;
}
public void add(Song a) {
if (count == songs.length) {
System.out.println("ERROR: Collection is full. Songs were not added to the Playlist.");
}
songs[count] = a;
count++;
}
public Song get(int i) {
if (count > i) {
return songs[i];
} else {
return null;
}
}
public Song remove(String name) {
boolean found = false;
int indexToRemove = 0;
while (indexToRemove < count && !found) {
if (songs[indexToRemove].getName().equals(name)) {
found = true;
} else {
indexToRemove++;
}
}
if (indexToRemove < count) {
for (int from = indexToRemove + 1; from < count; from++) {
songs[from - 1] = songs[from];
}
songs[count - 1] = null;
count--;
}
return null;
}
public void print() {
String result = "NumSongs = " + count
+ " / PlayList song limit = " + songs.length + "\n";
for (int i = 0; i < count; i++) {
result += ("songList[" + i + "] = <"
+ songs[i] + ">\n");
}
result += " / " + formattedTotalTime();
System.out.println(result.toString());
}
public int size() {
return count;
}
public int totalTime() {
int totalTime = 0;
for (int i = 0; i < count; i++) {
totalTime += songs[i].getLength();
}
return totalTime;
}
public String formattedTotalTime() {
return formatTime(totalTime());
}
public void clear() {
for (int i = 0; i < songs.length; i++) {
songs[i] = null;
count = 0;
}
return;
}
}
public static class Song {
public String name;
public String artist;
public String album;
public int length;
public Song(String songName, String artistName, String albumName, int trackLength) {
this.name = songName;
this.artist = artistName;
this.album = albumName;
this.length = trackLength;
}
public void setName(String songName) {
name = songName;
}
public String getName() {
return name;
}
public void setArtist(String artistName) {
artist = artistName;
}
public String getArtist() {
return artist;
}
public void setAlbum(String albumName) {
album = albumName;
}
public String getAlbum() {
return album;
}
public void setLength(int h, int m, int s) {
length = (h * 3600 + m * 60 + s);
if (h == 0) {
length = (m * 60 + s);
}
}
public int getLength() {
return length;
}
public String toString() {
return "Title: " + getName() + ", Artist: " + getArtist()
+ ", Album: " + getAlbum() + ", Track Length: " + formatTime(getLength());
}
}
public static String formatTime(long time) {
long overflow = time;
long h = time / HOURS;
overflow = time % HOURS;
long m = overflow / MINS;
overflow = time % MINS;
long s = overflow / SECS;
return String.format("%02d:%02d.%02d", h, m, s);
}
}
此测试代码当前打印出
NumSongs = 7 / PlayList song limit = 12
songList[0] = <Title: Hotline Bling, Artist: Drake, Album: Hotline Bing - Single, Track Length: 00:04.27>
songList[1] = <Title: What Do You Mean?, Artist: Justin Bieber, Album: What Do You Mean? - Single, Track Length: 00:03.27>
songList[2] = <Title: Watch Me, Artist: Silento, Album: Watch Me (Whip / Nae Nae) - Single, Track Length: 00:03.05>
songList[3] = <Title: 679, Artist: Fetty Wap ft. Remy Boyz, Album: Fetty Wap, Track Length: 00:03.05>
songList[4] = <Title: Can't Feel My Face, Artist: The Weeknd, Album: Beauty Behind the Madness, Track Length: 00:03.33>
songList[5] = <Title: Good for You, Artist: Selena Gomez ft. A$AP Rocky, Album: Good for You - Single, Track Length: 00:03.41>
songList[6] = <Title: If You, Artist: Big Bang, Album: MADE, Track Length: 00:04.24>
/ 00:25.42
NumSongs = 5 / PlayList song limit = 12
songList[0] = <Title: Hotline Bling, Artist: Drake, Album: Hotline Bing - Single, Track Length: 00:04.27>
songList[1] = <Title: What Do You Mean?, Artist: Justin Bieber, Album: What Do You Mean? - Single, Track Length: 00:03.27>
songList[2] = <Title: Can't Feel My Face, Artist: The Weeknd, Album: Beauty Behind the Madness, Track Length: 00:03.33>
songList[3] = <Title: Good for You, Artist: Selena Gomez ft. A$AP Rocky, Album: Good for You - Single, Track Length: 00:03.41>
songList[4] = <Title: If You, Artist: Big Bang, Album: MADE, Track Length: 00:04.24>
/ 00:19.32
所以我用javafx制作了一个mp3播放器,它有一个,在这里我拖放歌曲,然后选择一首歌曲并按play播放,你就明白了。问题是,用我当前的代码,我不能播放两首以上歌曲的序列:/(播放选定的歌曲,播放它旁边的歌曲,然后停止)。代码如下:
我正在制作音频播放器(android)。问题是我什么时候启动我的应用程序
我有一个创建和播放我播放列表的文字JS对象。在HTML页面中,我有一个包含我所有曲目的列表。当我点击某些曲目时,一切正常,但当我点击一个曲目,等待曲目结束时,下一个曲目就不播放了。这是我的代码的部分: 还有一件事,当第一首歌曲结束并调用下一首歌曲的播放时,曲目没有加载(onload事件没有消息返回)。谢谢,对不起我的英语不好。A. 好的,我试着在设置上添加“FlashVersion:9”,这就解决
我正在学习如何为某些曲目创建媒体播放器的教程...每首歌都按我的要求播放然后停止...我想让整个播放列表播放...那是为了在前一个赛道结束后,下一个赛道自动开始... 多谢...
我正在开发一个移动应用程序,可以检测iPhone和Android上各种媒体播放器播放的歌曲。我们目前支持多个应用程序在这两个平台。我们的应用程序的工作方式与Last.fm检测和自动删除播放的方式类似。 假设我对要收集其历史记录的每个用户都有spotify凭据。 谢谢
我制作了一个HTML5 iPod。 你可以在这里试试。 http://inventikasolutions.com/demo/iPod 在PC上使用Chrome时。如果我导航到一首歌,它会自动开始播放。但在Android上使用Chrome时,它不会播放这首歌。我必须再次按下播放/暂停按钮才能播放音频。 这是当您选择要播放的歌曲时运行的代码: 这是播放/暂停代码。 它是否与“autoplay”变量有