【问题标题】:Blue J Java - Beginner - Playing two tracks at once issueBluej Java - 初学者 - 一次播放两首曲目问题
【发布时间】:2017-08-05 16:49:28
【问题描述】:

我对 Java 比较陌生,只是在阅读朋友借给我的一本书。

问题说

如果您播放两首曲目而不停止第一首,则两者将同时播放。 这不是很有用。更改您的程序,以便自动播放曲目 当另一个轨道开始时停止

这是目前的样子:

public void playTrack(int index)
{
    if(indexValid(index)) {
        Track track = tracks.get(index);
        player.startPlaying(track.getFilename());
        track.incrementCount();
        System.out.println("Now playing: " + track.getArtist() + " - " + 
track.getTitle());
       }

我的计划是添加另一个 else if 语句,然后以某种方式判断一首歌曲是否正在播放,然后停止当前并开始下一首,但我不确定如何执行此操作。

抱歉,这是我第一次上这个论坛,我对 java 很陌生。

这里是这个类的完整代码

import java.util.ArrayList;

/**

 */
            public class MusicOrganizer
            {
                // An ArrayList for storing music tracks.
                private ArrayList<Track> tracks;
                // A player for the music tracks.
                private MusicPlayer player;
                // A reader that can read music files and load them as tracks.
                private TrackReader reader;

                /**
                 * Create a MusicOrganizer
                 */
                public MusicOrganizer()
                {
                    tracks = new ArrayList<>();
                    player = new MusicPlayer();
                    reader = new TrackReader();
                    readLibrary("../audio");
                    System.out.println("Music library loaded. " + getNumberOfTracks() + " tracks.");
                    System.out.println();
                }

                /**
                 * Add a track file to the collection.
                 * @param filename The file name of the track to be added.
                 */
                public void addFile(String filename)
                {
                    tracks.add(new Track(filename));
                }

                /**
                 * Add a track to the collection.
                 * @param track The track to be added.
                 */
                public void addTrack(Track track)
                {
                    tracks.add(track);
                }

                /**
                 * Play a track in the collection.
                 * @param index The index of the track to be played.
                 */
                public void playTrack(int index)
                {
                    if(indexValid(index)) {
                        Track track = tracks.get(index);
                        player.startPlaying(track.getFilename());
                        System.out.println("Now playing: " + track.getArtist() + " - " + track.getTitle());
                    }
                }

                /**
                 * Return the number of tracks in the collection.
                 * @return The number of tracks in the collection.
                 */
                public int getNumberOfTracks()
                {
                    return tracks.size();
                }

                /**
                 * List a track from the collection.
                 * @param index The index of the track to be listed.
                 */
                public void listTrack(int index)
                {
                    System.out.print("Track " + index + ": ");
                    Track track = tracks.get(index);
                    System.out.println(track.getDetails());
                }

                /**
                 * Show a list of all the tracks in the collection.
                 */
                public void listAllTracks()
                {
                    System.out.println("Track listing: ");

                    for(Track track : tracks) {
                        System.out.println(track.getDetails());
                    }
                    System.out.println();
                }

                /**
                 * List all tracks by the given artist.
                 * @param artist The artist's name.
                 */
                public void listByArtist(String artist)
                {
                    for(Track track : tracks) {
                        if(track.getArtist().contains(artist)) {
                            System.out.println(track.getDetails());
                        }
                    }
                }

                /**
                 * Remove a track from the collection.
                 * @param index The index of the track to be removed.
                 */
                public void removeTrack(int index)
                {
                    if(indexValid(index)) {
                        tracks.remove(index);
                    }
                }

                /**
                 * Play the first track in the collection, if there is one.
                 */
                public void playFirst()
                {
                    if(tracks.size() > 0) {
                        player.startPlaying(tracks.get(0).getFilename());
                    }
                }

                /**
                 * Stop the player.
                 */
                public void stopPlaying()
                {
                    player.stop();
                }

                /**
                 * Determine whether the given index is valid for the collection.
                 * Print an error message if it is not.
                 * @param index The index to be checked.
                 * @return true if the index is valid, false otherwise.
                 */
                private boolean indexValid(int index)
                {
                    // The return value.
                    // Set according to whether the index is valid or not.
                    boolean valid;

                    if(index < 0) {
                        System.out.println("Index cannot be negative: " + index);
                        valid = false;
                    }
                    else if(index >= tracks.size()) {
                        System.out.println("Index is too large: " + index);
                        valid = false;
                    }
                    else {
                        valid = true;
                    }
                    return valid;
                }

                private void readLibrary(String folderName)
                {
                    ArrayList<Track> tempTracks = reader.readTracks(folderName, ".mp3");

                    // Put all thetracks into the organizer.
                    for(Track track : tempTracks) {
                        addTrack(track);
                    }

【问题讨论】:

  • 欢迎来到 Stack Overflow!请take the tour 了解该网站的运作方式以及此处的主题问题,并相应地编辑您的问题。另见:Why is "Can someone help me?" not an actual question?
  • 您也可以控制MusicPlayer 代码吗?如果是这样,您可以将MusicPlayer.startPlaying() 更改为首先调用stop() 本身。或者,我想你可以自己打电话给stop()MusicOrganizer.playTrack()

标签: java bluej


【解决方案1】:

在开始 index 指定的轨道之前,您必须通过执行以下操作来停止所有其他轨道:

for(int i = 0; i < tracks.size(); i++) {
    if(i != trackToPlay) {
        player.stop(tracks.get(i));
    }
}

【讨论】:

  • 我已经尝试将它添加到开头,但它需要一个 ;在 i = 0 之后,因为当你说 i 小于 track.size();我++。
  • 哦,打错字了。应该是for(int i = 0; i &lt; tracks.size(); i++)
  • 但现在我必须定义 trackToPlay 变量 - 这是你的意图吗?
  • 这就是你的playTrack方法中名为index的参数。
  • 好吧,我很困惑,因为我们还没有定义变量 trackToPlay,所以我的 IDE 抛出这个错误:'找不到符号 - 变量 trackToPlay'。
猜你喜欢
  • 1970-01-01
  • 2018-12-16
  • 1970-01-01
  • 1970-01-01
  • 2016-12-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多