Playing from a stream

Hey!



I wana play from an internet radio stream such as one from this webpage :



http://www.shoutcast.com/radio/Hip-Hop/Rap



So im using jME3 - and see there is an AudioStream class - but im not sure how to use it - any guides somebody can point me at?

Yeah, except that file is an MP3 if you try to download it, so it won't work with that particular file.

The songs are probably copyrighted on that site, so you can't use them in your program unless you want to get sued.

That was an example - i have developer access and have been given permission to use napsters streaming api. So , can somebody show how I would do it?

renegadeandy said:

That was an example - i have developer access and have been given permission to use napsters streaming api. So , can somebody show how I would do it?


Then why give an irrelevant example..  :| I imagine it depends on "napsters streaming api", of which, I have no knowledge...

well i presumed if i have a URL which is a stream… there would be an API from jME to play audio from an audiostream…so lets just say i have a stream at this url:



www.donkey.com/music/live/gogogostream



How would I play this using jME, or should I use some other audio libs :P?

Where in the process are you stuck at?  It seems to me that there are really two main steps to this:


  1. Decode the stream into raw PCM audio data.


  2. Send that data to a sound library (Java Sound, JOAL, LWJGL Open AL, SoundSystem, etc) for manipulation and playback.



    Step one is going to require one or more external libraries designed to load PCM data from an online stream.  How to do this depends on the stream itself - there are hundreds of different varieties and formats of audio and video streams out there, and likely an equal number of different libraries for accessing them.  As InfernoZeus stated, in your case you probably need to figure out the "napsters streaming api".  None of the above sound libraries I mentioned have a built-in mechanism for doing this.



    Step two is much more up my ally.  Decide which sound library you will use, and if its one I've used before I can provide you with some code examples for how to stream that data to the sound-card.  Obviously, step two is pointless until you figure out step one.

AH i see.



I thought jME had its own audio playback system and audio streaming playback - it has an AudioStream class??? Which I think is why I am confused.



SO if i wanted to play an OGG file from an InputStream pointing at the web then? Is that something jME supports or have i got it all wrong, and jME doesnt have its own sound implementation?

Look in the tests jme3test.audio.TestOgg and TestReverb. They show how to play music and sound effects respectively.

If you have an OGG/Vorbis file on a server, you can just use UrlLocator to link to it and stream it from there.

If you're using an API, then you probably have access to the PCM data, in that case you would implement InputStream that sends PCM in the format that you gave in AudioStream.setupFormat call.

hmm so if my ogg was at url :



"http://api.jamendo.com/get2/stream/track/redirect/?id=172017&streamencoding=ogg2"



I would do :



   AssetManager   manager = JmeSystem.newAssetManager();
   
  manager.registerLocator("http://api.jamendo.com/get2/stream/track/redirect/",
                                UrlLocator.class.getName());
   Listener   listener = new Listener();
    ar.setListener(listener);
   AudioNode src = new AudioNode(manager,"?id=172017&streamencoding=ogg2", true);



? That right?

Gotcha - was because i wasnt decoding & to & on that URL.



Right so after that step i go



   AppSettings settings = new AppSettings(true);
            settings.setAudioRenderer(AppSettings.LWJGL_OPENAL);
            AudioRenderer ar = JmeSystem.newAudioRenderer(settings);
            ar.initialize();
  Listener listener = new Listener();
            ar.setListener(listener);
            AudioNode src = new AudioNode(manager, track.getIdentifer(), true);
            ar.playSource(src);



AS done in the examples - but then it null pointers on the ar.playSource(src); I am making a mistake! I think its to do with not having implemented an update(float tpf) method but I cannot find any interface im meant to implement?

You don't need to create an audio renderer yourself, just use the one that is provided inside the Application class.

Sweet music to my ears! Thank you!



Tryna make a kinda playlist player -



while(true){



pollNextSong



audioRenderer.play(song)



Thread.sleep(length of song)



}



However - when i try to obtain the length of my song : song.getAudioData().getDuration() it isnt very happy and returns -1 - i reckon its cos its streaming. SO , in order to pause my thread i need to know whether a song is playing, or the length of the song…



Any ideas?

Well you can try to manually read/decode the file's header,



else streaming and having size does not really fit together.

Only after decoding you knwo the time it plays, but you can only decode it completly to determine it when you donwlaoded it fullly.

Hmm so i could try that - would have no idea where to start… anyone else got any smart solution using jME. Can I not tell if some sound is playing through the audioRenderer?

There's the AudioNode.getStatus() == Status.Playing way to check. The reason that the duration is not available in streaming OGG files, is because you have to seek to the end of the file to find how long it is, but the jME3 asset system uses inputstreams, which don't support seeking.

Yeah i see - well i guess i can just check the thread every 2 seconds to see if its still playing! If not - move onto next one :slight_smile:



Cheers momoko

renegadeandy said:

Yeah i see - well i guess i can just check the thread every 2 seconds to see if its still playing! If not - move onto next one :)

Cheers momoko

http://en.wikipedia.org/wiki/Valediction#Valedictions_in_e-mail

:-o what!? I said "Cheers momoko?"