2012-03-12 22 views
0

我有幾個關於我正在學習的項目的問題。代碼如下。關於Java中的音樂項目的幾個問題

public class Player 
{ 
private PlayList playList; 
private Track track; 
private int tracksPlayed; 
private int totalTrackTime; 
private double averageTrackTime; 

/** 
* Constructor ... 
*/ 
public Player() 
{ 
    playList = new PlayList("audio"); 
    track = playList.getTrack(0); 
    this.tracksPlayed = tracksPlayed; 
    this.totalTrackTime = totalTrackTime; 
    this.averageTrackTime = averageTrackTime; 
} 

/** 
* Return the track collection currently loaded in this player. 
*/ 
public PlayList getPlayList() 
{ 
    return playList; 
} 

/** 
* 
*/ 
public void play() 
{ 
    track.play(); 
    tracksPlayed++; 
    int trackDuration = track.getDuration(); 
    totalTrackTime = totalTrackTime + trackDuration; 
    averageTrackTime = totalTrackTime/tracksPlayed; 
} 

/** 
* 
*/ 
public void stop() 
{ 
    track.stop(); 
} 

/** 
* 
*/ 
public void setTrack(int trackNumber) 
{ 
    int currentTrack = trackNumber; 
    track = playList.getTrack(currentTrack); 
} 

/** 
* 
*/ 
public String getTrackName() 
{ 
    String currentTrack = track.getName(); 
    return currentTrack; 
} 

/** 
* 
*/ 
public String getTrackInfo() 
{ 
    String currentTrack = track.getName(); 
    int trackDuration = track.getDuration(); 
    return currentTrack + " " + "(" + trackDuration + ")"; 
} 

/** 
* 
*/ 
public int getNumberOfTracksPlayed() 
{ 
    return tracksPlayed; 
} 

/** 
* 
*/ 
public int getTotalPlayedTrackLength() 
{ 
    return totalTrackTime; 
} 

/** 
* 
*/ 
public double averageTrackLength() 
{ 
    return averageTrackTime; 
} 
} 


public class Track 
{ 
private Clip soundClip; 
private String name; 
/** 
* Create a track from an audio file. 
*/ 
public Track(File soundFile) 
{ 
    soundClip = loadSound(soundFile); 
    name = soundFile.getName(); 
} 

/** 
* Play this sound track. (The sound will play asynchronously, until 
* it is stopped or reaches the end.) 
*/ 
public void play() 
{ 
    if(soundClip != null) { 
     soundClip.start(); 
    } 
} 

/** 
* Stop this track playing. (This method has no effect if the track is not 
* currently playing.) 
*/ 
public void stop() 
{ 
    if(soundClip != null) { 
     soundClip.stop(); 
    } 
} 

/** 
* Reset this track to its start. 
*/ 
public void rewind() 
{ 
    if(soundClip != null) { 
     soundClip.setFramePosition(0); 
    } 
} 

/** 
* Return the name of this track. 
*/ 
public String getName() 
{ 
    return name; 
} 

/** 
* Return the duration of this track, in seconds. 
*/ 
public int getDuration() 
{ 
    if (soundClip == null) { 
     return 0; 
    } 
    else { 
     return (int) soundClip.getMicrosecondLength()/1000000; 
    } 
} 

/** 
* Set the playback volume of the current track. 
* 
* @param vol Volume level as a percentage (0..100). 
*/ 
public void setVolume(int vol) 
{ 
    if(soundClip == null) { 
     return; 
    } 
    if(vol < 0 || vol > 100) { 
     vol = 100; 
    } 

    double val = vol/100.0; 

    try { 
     FloatControl volControl = 
      (FloatControl) soundClip.getControl(FloatControl.Type.MASTER_GAIN); 
     float dB = (float)(Math.log(val == 0.0 ? 0.0001 : val)/Math.log(10.0) *   20.0); 
     volControl.setValue(dB); 
    } catch (Exception ex) { 
     System.err.println("Error: could not set volume"); 
    } 
} 

/** 
* Return true if this track has successfully loaded and can be played. 
*/ 
public boolean isValid() 
{ 
    return soundClip != null; 
} 

/** 
* Load the sound file supplied by the parameter. 
* 
* @return The sound clip if successful, null if the file could not be decoded. 
*/ 
private Clip loadSound(File file) 
{ 
    Clip newClip; 

    try { 
     AudioInputStream stream = AudioSystem.getAudioInputStream(file); 
     AudioFormat format = stream.getFormat(); 

     // we cannot play ALAW/ULAW, so we convert them to PCM 
     // 
     if ((format.getEncoding() == AudioFormat.Encoding.ULAW) || 
      (format.getEncoding() == AudioFormat.Encoding.ALAW)) 
     { 
      AudioFormat tmp = new AudioFormat(
             AudioFormat.Encoding.PCM_SIGNED, 
             format.getSampleRate(), 
             format.getSampleSizeInBits() * 2, 
             format.getChannels(), 
             format.getFrameSize() * 2, 
             format.getFrameRate(), 
             true); 
      stream = AudioSystem.getAudioInputStream(tmp, stream); 
      format = tmp; 
     } 
     DataLine.Info info = new DataLine.Info(Clip.class, 
             stream.getFormat(), 
             ((int) stream.getFrameLength() * 
             format.getFrameSize())); 

     newClip = (Clip) AudioSystem.getLine(info); 
     newClip.open(stream); 
     return newClip; 
    } catch (Exception ex) { 
     return null; 
    } 
} 
} 

public class PlayList 
{ 
private List<Track> tracks; 

/** 
* Constructor for objects of class TrackCollection 
*/ 
public PlayList(String directoryName) 
{ 
    tracks = loadTracks(directoryName); 
} 

/** 
* Return a track from this collection. 
*/ 
public Track getTrack(int trackNumber) 
{ 
    return tracks.get(trackNumber); 
} 

/** 
* Return the number of tracks in this collection. 
*/ 
public int numberOfTracks() 
{ 
    return tracks.size(); 
} 

/** 
* Load the file names of all files in the given directory. 
* @param dirName Directory (folder) name. 
* @param suffix File suffix of interest. 
* @return The names of files found. 
*/ 
private List<Track> loadTracks(String dirName) 
{ 
    File dir = new File(dirName); 
    if(dir.isDirectory()) { 
     File[] allFiles = dir.listFiles(); 
     List<Track> foundTracks = new ArrayList<Track>(); 

     for(File file : allFiles) { 
      //System.out.println("found: " + file); 
      Track track = new Track(file); 
      if(track.isValid()) { 
       foundTracks.add(track); 
      } 
     } 
     return foundTracks; 
    } 
    else { 
     System.err.println("Error: " + dirName + " must be a directory"); 
     return null; 
    } 
} 

/** 
* Return this playlist as an array of strings with the track names. 
*/ 
public String[] asStrings() 
{ 
    String[] names = new String[tracks.size()]; 
    int i = 0; 
    for(Track track : tracks) { 
     names[i++] = track.getName(); 
    } 
    return names; 
} 
} 
  1. 我明白,要調用播放器類的遊戲方式,我必須初始化一個軌道類變量。我還需要使用PlayList getTrack方法來初始化它。但是,如何在不定義起始索引變量的情況下對其進行初始化(即,我不希望它自動初始化爲索引中的特定歌曲,我希望用戶必須先選擇一首歌曲)?

  2. 而且,我怎麼在播放器類代碼play方法開始一個新的歌曲之前,停止現有的歌曲如果一個人玩,重新啓動一首歌如果打的方法又被稱爲一個同一首歌?

+1

1. http://stackoverflow.com/questions/2707322/what-is-null-in-java 2.這聽起來像是「爲我解決我的家庭作業」。您需要更具體的問題和更多關於您嘗試過的信息或迄今爲止您的反思。 – Krizz 2012-03-12 22:18:52

+0

我建議你在發佈之前閱讀[faq]和[ask]。 – 2012-03-12 23:06:51

回答

0

1.

我怎麼能初始化它沒有定義起始索引變量

public Player (int initTrack){ 
    ... 
    track = playList.getTrack(initTrack); 
    ... 
} 

2. 你應該用一個字段存儲的軌道,這是嘗試目前在玩!