[gameprogrammer] Re: Java applet streaming music?

cool .. it is a great way of learning the api. my target is streaming
(since i'm expecting modem users of my applet) but i'll fall back to
your load-all-bytes thread if that doesn't work out,

thanks alot stephen,

/Olof

On 5/3/06, Stephen <gp@xxxxxxxxxxxxxxxxxxxxxxxx> wrote:
Well, I wrote this simple wrapper class that plays an mp3 in a thread
which might be useful.  It doesn't do any streaming or anything though
I'm afraid.


public class MP3Player extends Thread {

    private String mp3_filename;
    private volatile boolean stop_now = false;

    public MP3Player(String fname) {
        this.mp3_filename = fname;
        this.start();
    }

    public void run() {
        AudioInputStream din = null;
        try {
            File file = new File(this.mp3_filename);
            AudioInputStream in = AudioSystem.getAudioInputStream(file);
            AudioFormat baseFormat = in.getFormat();
            AudioFormat decodedFormat = new AudioFormat(
                    AudioFormat.Encoding.PCM_SIGNED,
                    baseFormat.getSampleRate(), 16,
baseFormat.getChannels(),
                    baseFormat.getChannels() * 2,
baseFormat.getSampleRate(),
                    false);
            din = AudioSystem.getAudioInputStream(decodedFormat, in);
            DataLine.Info info = new DataLine.Info(SourceDataLine.class,
decodedFormat);
            SourceDataLine line = (SourceDataLine)
AudioSystem.getLine(info);
            if(line != null) {
                line.open(decodedFormat);
                byte[] data = new byte[4096];
                // Start
                line.start();

                int nBytesRead;
                while ((nBytesRead = din.read(data, 0, data.length)) !=
-1 && stop_now == false) {
                    line.write(data, 0, nBytesRead);
                }
                // Stop
                line.drain();
                line.stop();
                line.close();
                din.close();
            }

        }
        catch(Exception ex) {
            System.err.println("Cannot play " + this.mp3_filename);
            ex.printStackTrace();
        }
        finally {
            if(din != null) {
                try { din.close(); } catch(IOException e) { }
            }
        }
    }

    public void stopNow() {
        this.stop_now = true;
    }

}




Olof Bjarnason wrote:

> thanks alot ... judging by the mp3-applet-mini-player he's got on his
> page too, it actually does fit the bill perfectly :)
>
> he states it's based on the java sound platform, which he says is
> available from jre 1.3 up, which is my target minimum java platform
>
> hope it is easy to learn .. do you have any hints on learning the api
> stephen?
>
> /Olof
>
> On 5/2/06, Stephen <gp@xxxxxxxxxxxxxxxxxxxxxxxx> wrote:
>
>> I don't know if it's exactly what you're looking for, but I use JLayer
>> (http://www.javazoom.net/javalayer/javalayer.html) to play MP3's in my
>> Java apps.  It's a free mp3 software library.  I'm afraid I don't know
>> if it works in Applets (I assume it does).
>>
>> Stephen
>>
>>
>> Olof Bjarnason wrote:
>>
>> > Hi there !
>> >
>> > Anyone done streaming music in a java applet?
>> >
>> > I'm trying to create some small games at my website. Graphics is
>> > working out fine now, some I'm turning to sound and music.
>> >
>> > I know that the basic, from-1.1-java-.au format works in applets, and
>> > it's 8bit,mono,8KHz. That's OK for oldskool pixelgames when it comes
>> > to sfx, but really off-the-edge when it comes to background tunes..
>> >
>> > I've surfed around some, and know that the java 1.5 supports more
>> > fancy sound & music, even streaming mp3s. But my target is java 1.3
>> > since I get the feeling this is a realistic estimate of what java
>> > version "my neighbour" has installed on his computer.
>> >
>> > So, what is possible / not possible in java 1.3 when it comes to
>> > sound/music? What formats? Streaming/not streaming? mp3? ogg? stereo?
>> > 16bit? 22KHz? etc. etc. Dream scenario is streaming mp3/stereo/44KHz.
>> >
>> > How do you go around this problem if it is not possible in java 1.3
>> > applets? Some flash app placed "next to" the applet on the web page?
>> > How do you control it?
>> >
>> > Any pointers / ideas?
>> >
>> > Cya around,
>> >
>> > /Olof
>> >
>> >
>> > ---------------------
>> > To unsubscribe go to http://gameprogrammer.com/mailinglist.html
>> >
>> >
>> >
>> >
>> >
>> >
>>
>>
>>
>>
>>
>> ---------------------
>> To unsubscribe go to http://gameprogrammer.com/mailinglist.html
>>
>>
>>
>
>
> ---------------------
> To unsubscribe go to http://gameprogrammer.com/mailinglist.html
>
>
>
>
>
>





---------------------
To unsubscribe go to http://gameprogrammer.com/mailinglist.html





---------------------
To unsubscribe go to http://gameprogrammer.com/mailinglist.html


Other related posts: