2015-08-13 52 views
0

我只想播放指定序列開始和結束的歌曲的一部分。我是新的在Java中使用歌曲。我寫了下面的代碼,但有些錯誤是因爲播放了整首歌曲。我應該修改什麼?如何在Java中播放歌曲的一部分?

public class Song implements LineListener { 
 
     
 
       
 
    \t /** 
 
     * this flag indicates whether the playback completes or not. 
 
     */ 
 
     boolean playCompleted; 
 
      
 
     /** 
 
     * Play a given audio file. 
 
     * @param audioFilePath Path of the audio file. 
 
     */ 
 
     
 
    
 
    void playFrame(String songName) { 
 
     
 
    \t File audioFile = new File(songName); 
 
      
 
    \t try { 
 
      AudioInputStream audioStream = AudioSystem.getAudioInputStream(audioFile); 
 
    
 
      AudioFormat format = audioStream.getFormat(); 
 
    
 
      DataLine.Info info = new DataLine.Info(Clip.class, format); 
 
    
 
      Clip audioClip = (Clip) AudioSystem.getLine(info); 
 
    
 
      audioClip.addLineListener(this); 
 
    
 
      audioClip.open(audioStream); 
 
       
 
      int nrFrames = audioClip.getFrameLength(); 
 
       
 
      audioClip.setLoopPoints(nrFrames/3, nrFrames/3*2); 
 
       
 
      audioClip.start(); 
 
       
 
      while (!playCompleted) { 
 
       // wait for the playback completes 
 
       try { 
 
        Thread.sleep(1000); 
 
       } catch (InterruptedException ex) { 
 
        ex.printStackTrace(); 
 
       } 
 
      } 
 
       
 
      audioClip.close(); 
 
       
 
     } catch (UnsupportedAudioFileException ex) { 
 
      System.out.println("The specified audio file is not supported."); 
 
      ex.printStackTrace(); 
 
     } catch (LineUnavailableException ex) { 
 
      System.out.println("Audio line for playing back is unavailable."); 
 
      ex.printStackTrace(); 
 
     } catch (IOException ex) { 
 
      System.out.println("Error playing the audio file."); 
 
      ex.printStackTrace(); 
 
     } 
 
} 
 
    @Override 
 
     public void update(LineEvent event) { 
 
    LineEvent.Type type = event.getType(); 
 
     
 
    if (type == LineEvent.Type.START) { 
 
     System.out.println("Playback started."); 
 
     
 
    } else if (type == LineEvent.Type.STOP) { 
 
     playCompleted = true; 
 
     System.out.println("Playback completed."); 
 
    } 
 

 
} 
 
     
 
    
 
    public static void main(String[] args) { 
 
     
 
     String fileName1 = "song1.wav"; 
 
\t \t String fileName2 = "song2.wav"; 
 
     Song player = new Song(); 
 
     player.playFrame(fileName1); 
 
     
 
     Song player2 = new Song(); 
 
     player2.playFrame(fileName2); 
 
    } 
 
    
 
}

+0

根據的Javadoc,似乎'當你玩用'loop'方法的循環,而不是用'start'方法setLoopPoints'會工作。我無法回答你的問題,但... – dotvav

+0

@Diana,由於您使用的是RIFF波形,請閱讀您的自我樣本並將它們泵入您的音頻後端。您確切知道您的DSP管道中發生了什麼。 – user877329

回答

0

你啓動循環點,但隨後不啓動 '環':

int nrFrames = audioClip.getFrameLength() 
audioClip.setLoopPoints(nrFrames/3, nrFrames/3*2); 

,並從檢查剪輯DOC:http://docs.oracle.com/javase/7/docs/api/javax/sound/sampled/Clip.html#setLoopPoints(int,%20int)

似乎就像你需要使用.loop(int count)方法,而不是.start()方法。您只需在這種情況下將計數設置爲1即可播放一次。

編輯:剛纔看到這個被製成由別人評論,而我打字起來,對不起!