streaming mp3 in AS3

Actionscript 3.0 questions.

streaming mp3 in AS3

Postby hasarapasa » Mon Jan 28, 2008 7:27 pm

does anyone know how to stream an external mp3 in as3? I need to build an online player.

I tried following lee's tutorial but as2 is something quite different than as3 - not really a matter of translation.

Bests
hasarapasa
 
Posts: 11
Joined: Thu Jan 24, 2008 11:49 pm

Re: streaming mp3 in AS3

Postby hamferus » Mon Jan 28, 2008 10:16 pm

There is whole section in Flash help called working with sound so you could start from there.
http://livedocs.adobe.com/flash/9.0/main/00000283.html

This is basic mp3 loading with as3
Code: Select all
var s:Sound = new Sound();
s.load(new URLRequest("loop.mp3"));

s.play();
User avatar
hamferus
Contributing Member
 
Posts: 537
Joined: Thu Mar 30, 2006 8:37 pm
Location: Zagreb - Croatia
Flash Version: Adobe Flash CS5.5
Authoring Environment: Microsoft Windows

Re: streaming mp3 in AS3

Postby hasarapasa » Tue Jan 29, 2008 12:12 am

thank you again!!

I hope I don't ask an unnecessary question but: the same manual claims that stopping a streaming is done by sound.close(); command, this doesn't seem to work as the comments say though... do you think it's a bug then?
http://livedocs.adobe.com/flash/9.0/main/wwhelp/wwhimpl/common/html/wwhelp.htm?context=LiveDocs_Parts&file=00000288.html

Best
hasarapasa
 
Posts: 11
Joined: Thu Jan 24, 2008 11:49 pm

Re: streaming mp3 in AS3

Postby hamferus » Tue Jan 29, 2008 9:45 am

I didn't find where is sound.close() command mentioned. I think that method is used for video. You can use stop() or pause() command for stoping sound. Inside working with sound there is playing sounds section and there you can read how to pause or stop sound.
User avatar
hamferus
Contributing Member
 
Posts: 537
Joined: Thu Mar 30, 2006 8:37 pm
Location: Zagreb - Croatia
Flash Version: Adobe Flash CS5.5
Authoring Environment: Microsoft Windows

Re: streaming mp3 in AS3

Postby hasarapasa » Tue Jan 29, 2008 10:13 am

Flash CS3 Documentation
Programming ActionScript 3.0 > Working with sound > Playing sounds > Stopping streaming sounds



Stopping streaming sounds

There is a quirk in the playback process for sounds that are streaming--that is, for sounds that are still loading while they are being played. When your application calls the SoundChannel.stop() method on a SoundChannel instance that is playing back a streaming sound, the sound playback stops for one frame, and then on the next frame, it restarts from the beginning of the sound. This occurs because the sound loading process is still underway. To stop both the loading and the playback of a streaming sound, call the Sound.close() method.


this does not seem to work though - the error is that there is no stream to be stopped, while the sound actually loads and plays.

best
hasarapasa
 
Posts: 11
Joined: Thu Jan 24, 2008 11:49 pm

Re: streaming mp3 in AS3

Postby hamferus » Tue Jan 29, 2008 11:07 am

I see what you mean. You have to use close() method when this glich occurs(mp3 file loading and playing in same time). If you want to stop sound that is playing you have to use SoundChannel.stop() method.

Something like this
Code: Select all
var sc:SoundChannel = new SoundChannel();
var s:Sound = new Sound();
s.load(new URLRequest("loop.mp3"));

play_btn.addEventListener(MouseEvent.CLICK,playSound);
stop_btn.addEventListener(MouseEvent.CLICK,stopSound);

function playSound(e:MouseEvent):void
{
   sc = s.play();
}
function stopSound(e:MouseEvent):void
{
   sc.stop();
}
User avatar
hamferus
Contributing Member
 
Posts: 537
Joined: Thu Mar 30, 2006 8:37 pm
Location: Zagreb - Croatia
Flash Version: Adobe Flash CS5.5
Authoring Environment: Microsoft Windows

Re: streaming mp3 in AS3

Postby hasarapasa » Tue Jan 29, 2008 11:29 am

thanks again for your swift reply, unfortunately this goes even further.

when I try to close the stream with the sound.close(); method I get the Error #2029: This URLStream object does not have a stream opened.

when I try to shut down the sound with the SoundChannel.stop(); it stops all right, but when I want to reopen the stream it shows up the Error #2037: Functions called in incorrect sequence, or earlier call was unsuccessful.

Here's my player code:
Code: Select all
var context:SoundLoaderContext = new SoundLoaderContext(2000, true);
var req:URLRequest = new URLRequest("http://www.makaruk.net/test1.mp3");
var s:Sound = new Sound();
var kanal1:SoundChannel = new SoundChannel;

import flash.media.Sound;
import flash.media.SoundLoaderContext;
import flash.net.URLRequest;
s.addEventListener(ProgressEvent.PROGRESS, onLoadProgress);
function onLoadProgress(event:ProgressEvent):void {
   var loadedPct:uint =
           Math.round(100 * (event.bytesLoaded / event.bytesTotal));
   trace("The sound is " + loadedPct + "% loaded.");
}

function stopClick(evt:Event):void {
   s.close();
// kanal1.stop(); <-- add this line to see what happens when the channel is stopped
}
bStop.addEventListener(MouseEvent.CLICK, stopClick);

function playMusic(evt:Event):void {
   s.load(req,context);
   kanal1 = s.play();
}
bPlayMusic.addEventListener(MouseEvent.CLICK, playMusic);

hasarapasa
 
Posts: 11
Joined: Thu Jan 24, 2008 11:49 pm

Re: streaming mp3 in AS3

Postby hamferus » Tue Jan 29, 2008 11:54 am

I'm not shure why its not playing second time you try to playMusic function but if you move this line outside of playMusic function and it should work fine
Code: Select all
s.load(req,context);


And in playMusic and stopClick function you should put (e:MouseEvent) not (e:Event).
User avatar
hamferus
Contributing Member
 
Posts: 537
Joined: Thu Mar 30, 2006 8:37 pm
Location: Zagreb - Croatia
Flash Version: Adobe Flash CS5.5
Authoring Environment: Microsoft Windows

Re: streaming mp3 in AS3

Postby hasarapasa » Thu Jan 31, 2008 7:49 pm

thank you!!! it worked!!

basically what I'm trying to acheive is a podcast-like player similar to the one downloadable here http://livedocs.adobe.com/flash/9.0/main/samples/Programming_ActionScript3.0_samples.zip

now I'm trying to make it a little bit simpler to fit some less demanding needs.

Best
Dariusz
hasarapasa
 
Posts: 11
Joined: Thu Jan 24, 2008 11:49 pm

Re: streaming mp3 in AS3

Postby straris » Wed Jan 30, 2013 9:49 pm

Hi, i am having a similar but more weird problem :P
i am creating an online radio with a simple, one button player, the code works for 5 times (play/stop)! After that the stream won't play anymore.

I am not sure if this is relevant but i am also getting SecurityError: Error #2000: No active security context. when i close the swf player.

Code: Select all
import flash.events.MouseEvent;
var streaming:Boolean;
this.addEventListener(MouseEvent.CLICK, music_on)

function music_on(event:MouseEvent):void
{   

var sndChannel:SoundChannel=new SoundChannel();
var s:Sound = new Sound();



    if (!streaming)
    {   gotoAndStop(2);             
        var req:URLRequest = new URLRequest("http://127.0.0.1:8000/listen.mp3");
        var context:SoundLoaderContext = new SoundLoaderContext(2000,false);
        s.load(req, context);
        sndChannel=s.play();
        streaming=true;         
    }
    else
    {
        gotoAndStop(1);

        streaming=false;
        sndChannel.stop();
        SoundMixer.stopAll();
        trace ("not playing");
    }
}
stop();
straris
 
Posts: 1
Joined: Wed Jan 30, 2013 9:45 pm
Flash Version: Adobe Flash CS4


Return to Actionscript 3.0

Who is online

Users browsing this forum: Bing [Bot] and 1 guest