tags:

views:

568

answers:

2

Hi. I'm playing around with javafx and I have modified the code of the MediaPleyer demo trying to reproduce a wav file. It doesn't work.

/*
 * 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
    }
}

The wav file is not reproduced without giving any exception. If I change the repeatCount property to

repeatCount:  javafx.scene.media.MediaPlayer.REPEAT_FOREVER

eventually gives a heap space exception:

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

There is any problem in the code above? There is a way to reproduce wav files? I think this is essential for javafx since wavs are a very spread audio format.

Thanks.

A: 

The JavaFx documentation is strange on that one. On one page it says playing wav files placed in jar files works on another it says it does not work.

For me it does not work like for you. (What works is playing .wav files which are not placed in jar files.)

Here is my solution for the problem (My own audioplayer)

    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();
    }
   }

Play the audio using:

PlayAudio {
  source: "{__DIR__}audio/audio.wav"
  }.play();
alexander.egger
Thanks for you help, Alexander.
Averroes
A: 

I also experimented with .wav files. I put my sample wav in the sourceode directory and it works perfectly for me. Since I do not know how to put sourcecode in here, see the sourcecode here. But sometimes I've troubles when using '..' in my pathes - I suspect that when the wav file is in the jar file, you should try to avoid '..'.

Ulrich Biberger