2009-10-05 9 views
1

javafx를 가지고 놀고 있는데 wav 파일을 재생하려고하는 MediaPleyer 데모 코드를 수정했습니다. 작동하지 않습니다.Javafx 및 wav 파일

/* 
* Copyright (c) 2009, SUN Microsystems, Inc. 
* All rights reserved. 
*/ 
package javafx.tools.fxd.demos.mediaplayer; 

import javafx.scene.*; 
import javafx.scene.media.*; 
import javafx.stage.*; 



var player = javafx.scene.media.MediaPlayer { 
    repeatCount: 1 
    media: Media { 
     source: "{__DIR__}Door_Open.wav" 
    }; 
}; 

class MyMediaPlayerUI extends MediaPlayerUI { 
    override protected function contentLoaded() { 
     super.contentLoaded(); 
     var s = player.media.source; 
     var i = s.lastIndexOf ("/"); 
     if (i >= 0) { 
      s = s.substring (i + 1); 
     } 
     fileName.content = s; 
    } 
} 

var stage : Stage; 
var ui = MyMediaPlayerUI {}; 

var skins = [ "{__DIR__}MediaPlayer1.fxz", "{__DIR__}MediaPlayer2.fxz" ]; 
var index = 0; 

ButtonController { 
    pressed: bind ui.playPressed 
    hovered: bind ui.playHovered 
    normal: bind ui.playNormal 
    activeArea: bind ui.playActiveArea 
    action: function() { 
     player.play(); 
    } 
} 

ButtonController { 
    pressed: bind ui.pausePressed 
    hovered: bind ui.pauseHovered 
    normal: bind ui.pauseNormal 
    activeArea: bind ui.pauseActiveArea 
    action: function() { 
     player.pause(); 
    } 
} 

ButtonController { 
    pressed: bind ui.switchPressed 
    hovered: bind ui.switchHovered 
    normal: bind ui.switchNormal 
    activeArea: bind ui.switchActiveArea 
    action: function() { 
     index = (index + 1) mod skins.size(); 
     ui.url = skins[index]; 
    } 
} 

stage = Stage { 
    title: "Media Player" 
    //visible: true 
    resizable: false 
    onClose: function() { java.lang.System.exit (0); } 
    scene: Scene { 
     content: ui 
    } 
} 

wav 파일은 예외없이 복제되지 않습니다. 내가

repeatCount: javafx.scene.media.MediaPlayer.REPEAT_FOREVER 

에 반복 횟수 속성이 결국 힙 공간 예외 준다 변경하는 경우 :

Exception in thread "PlayerLoop" java.lang.OutOfMemoryError: Java heap space 

위의 코드에 문제가 있습니다를? wav 파일을 재현하는 방법이 있습니까? wav는 매우 널리 퍼져있는 오디오 포맷이므로 javafx에서는 이것이 필수적이라고 생각합니다.

감사합니다.

+0

Windows에서 "chord.wav"를 시도했기 때문에 파일과 관련이 있어야합니다. – Averroes

+0

이 경우 사운드가 최초 재생을 완료하고 다시 재생을 클릭하면이 예외가 발생합니다. 스레드 "AWT-EventQueue-1"의 예외 com.sun.media.jmc.OperationUnsupportedException : 미디어 플레이어 피어가 미디어 시간을 설정할 수 없습니다 시간 : 1.207031191 매우 실망 스럽습니다. – Averroes

답변

1

JavaFx 설명서가 이상합니다. 한 페이지에서 jar 파일에있는 wav 파일을 재생하는 것은 작동하지 않는다고 말하는 다른 파일에서 작동합니다.

나를 위해 그것은 당신을 위해 작동하지 않습니다. (무엇 이 jar 파일에 배치되지 있습니다 .wav 파일을 재생 작동합니다.)

여기

import java.net.URL; 
    import javax.sound.sampled.AudioFormat; 
    import javax.sound.sampled.AudioInputStream; 
    import javax.sound.sampled.AudioSystem; 
    import javax.sound.sampled.DataLine; 
    import javax.sound.sampled.SourceDataLine; 

    public class AudioPlayer { 

     private static final int EXTERNAL_BUFFER_SIZE = 128000; 
     private URL url_; 

     public AudioPlayer(URL filename) { 
      url_ = filename; 
     } 

     public void play() throws Exception { 

      AudioInputStream audioInputStream = null; 
      audioInputStream = AudioSystem.getAudioInputStream(url_); 

      AudioFormat audioFormat = audioInputStream.getFormat(); 

      SourceDataLine line = null; 
      DataLine.Info info = new DataLine.Info(SourceDataLine.class, 
        audioFormat); 
      line = (SourceDataLine) AudioSystem.getLine(info); 
      line.open(audioFormat); 
      line.start(); 

      int nBytesRead = 0; 
      byte[] abData = new byte[EXTERNAL_BUFFER_SIZE]; 

      while (nBytesRead != -1) { 
       nBytesRead = audioInputStream.read(abData, 0, abData.length); 
       if (nBytesRead >= 0) { 
        line.write(abData, 0, nBytesRead); 
       } 
      } 

      line.drain(); 
      line.close(); 

     } 
    } 


    import javafx.async.RunnableFuture; 

    public class PlayAudioImpl implements RunnableFuture { 

     private AudioPlayer audio; 

     public PlayAudioImpl(AudioPlayer audio) { 
      this.audio = audio; 
     } 

     @Override 
     public void run() throws Exception { 
      audio.play(); 
     } 
    } 



    import javafx.async.JavaTaskBase; 
    import javafx.async.RunnableFuture; 

    import java.net.URL; 

    public class PlayAudio extends JavaTaskBase { 
    public-init var source:String; 

    public override function create() : RunnableFuture { 
     var player = new AudioPlayer(new URL(source)); 
     return new PlayAudioImpl(player); 
    } 

    public function play() : Void { 
     start(); 
    } 
    } 

를 사용하여 오디오를 재생 문제에 대한 내 솔루션 (내 자신의 audioplayer)입니다 :

PlayAudio { 
    source: "{__DIR__}audio/audio.wav" 
    }.play(); 
+0

도와 줘서 고마워, 알렉산더. – Averroes