Showing posts with label Audio. Show all posts
Showing posts with label Audio. Show all posts
Saturday, 9 April 2011
Experimenting with Sound in Flash - DJ Music Mixer
As soon as I realised the possibilities available with Sound Objects in Flash I realised that you could write a simple sound mixing app using ActionScript.
I started this experiment ages ago, but ran into a problem with the sound I couldn't solve straight away. 18 months later I remember it exists and blow the dust off to find the problem still waiting for me.
Just in case any one else has struggled with the same problem... I now have the solution, or at least know why that particular approach wouldn't work and have an alternative.
Once you set up a Sound Object you can either use attachSound to connect an embedded sound from your document Library, or you can use loadSound to load in an external MP3 file. But... and this is where I went wrong... you can't do both. In other words, if the first time you initialise the sound object you use attachSound you can't later change the sound using loadSound and expect it to behave normally. At least that was my experience.
My experimental app was a 2 channel audio mixer that used 2 Sound Objects, one for each channel. The mixing was done very simply by reducing the volume of one Sound Object while simultaneously increasing the volume of the other. So far so good.
However, for the Sound Objects to properly initialise, (and therefore the volume control code work properly) they needed to have a sound loaded into them straight away. The most obvious solution to me was to use attachSound to connect a sound embedded in the movie. So I made a couple of MP3s of 0 second duration and used those embedded within the app. Fine, the Objects initialised OK.
However, to be useful you need to be able to load external MP3s in, or it's only a 2 song show. And that's where the problem arose, because I would need switch from using attachSound to loadSound after the object initialised, and all of a sudden the volume control stops working as expected.
And that's what stumped me. It was only coming back with fresh eyes that allowed me to spot this discrepancy.
So, how to overcome it?
Simples - use loadSound from the first, but get Flash to load a non-existent sound by getting it to load nothing (e.g. ""). Sure, Flash will error, and complain it can't find the file, but the Sound Object will still initialise correctly - and the error is only visible when you test the movie from within the authoring software, not once exported.
Hey presto, when I load an external sound it all works, because I used loadSound all along and didn't change method later on. My music mixing app finally works.
It's possible there are other factors or other solutions - happy to hear about them, but that's how I got round it.
If it helps, happy to help.
Monday, 23 February 2009
Flash Projects - #5 - Custom Video Player Mute Control
Building further on the custom FLV player in previous posts, and definitely inspired by the BBC iPlayer, I decided to add further functionality, including a mute control to turn off the sound for a video currently playing.
The ActionScript is really very simple, and logical. Once you understand how to stream and attach video, attaching sound for control is very straight forward.
Quick recap
The original code required you to create a netConnection object and attach to that a netStream object:
//Net connection for video stream to video object
var myConnection_nc:NetConnection = new NetConnection();
myConnection_nc.connect(null);
var video_ns:NetStream = new NetStream(myConnection_nc);
Then you were instructed to attach the video stream to a video object on the stage:
this.vidHolder_mc.vid_video.attachVideo(video_ns);
Attaching audio
Setting up to control (or mute) the audio is not all that different. Use the following code:
//Attach audio of FLV for volume control
flv_mc.attachAudio(video_ns);
var audio_sound:Sound = new Sound(flv_mc);
Notice that the video and the audio both attach the same netStream object video_ns. While the audio will still be heard without attaching it separately, if we want to control it, we must attach it to a movie clip of its own as above. In this case a movie clip called flv_mc.
Controlling audio
Because we want a mute button that will change depending on the mute status (showing whether the sound is currently muted or not - visual feedback is such a wonderful thing), you need to create a variable to store the current mute status.
Insert the following code right at the top of your ActionScript:
//Mute control status, 0 = mute off sound on
var mutestatus = 0;
Then you need to create a movie clip to act as the mute button. This has just two frames, one containing an image of a speaker making noise, the other containing an image of a speaker not making noise (decide how to depict this however you like). Give the first frame the label muteon and the second frame the label muteoff. Don't forget to put a stop(); action on each of the frames or you will have an animated flickering button. Then drag an instance of the mute button onto the stage and give it the instance name mute_mc.
The following code will then allow you to mute and un-mute the sound, insert this at some point after the line you just inserted above:
//Mute control
_root.mute_mc.onRelease = function() {
if (mutestatus == 0) {
_root.mute_mc.gotoAndStop("muteon");
mutestatus = 1;
audio_sound.setVolume(0);
updateVolume();
} else if (mutestatus == 1) {
_root.mute_mc.gotoAndStop("muteoff");
mutestatus = 0;
audio_sound.setVolume(100);
updateVolume();
}
};
How does it work? Very simple. First it checks to see what the mute status is (whether it is muted yet or not). If it is not muted, it tells the button to look like it is muted (goes to the frame with the muted speaker picture), then sets the mute status to 1, then sets the volume to be 0, then updates the volume to make the 0 take effect. If, however, it is already muted, it tells the button to look like it is not muted (goes to the frame with the noisy speaker picture), then sets the mute status to 0, then sets the volume to be 100, then updates the volume to make the 100 take effect.
And that's a wrap.
The ActionScript is really very simple, and logical. Once you understand how to stream and attach video, attaching sound for control is very straight forward.
Quick recap
The original code required you to create a netConnection object and attach to that a netStream object:
//Net connection for video stream to video object
var myConnection_nc:NetConnection = new NetConnection();
myConnection_nc.connect(null);
var video_ns:NetStream = new NetStream(myConnection_nc);
Then you were instructed to attach the video stream to a video object on the stage:
this.vidHolder_mc.vid_video.attachVideo(video_ns);
Attaching audio
Setting up to control (or mute) the audio is not all that different. Use the following code:
//Attach audio of FLV for volume control
flv_mc.attachAudio(video_ns);
var audio_sound:Sound = new Sound(flv_mc);
Notice that the video and the audio both attach the same netStream object video_ns. While the audio will still be heard without attaching it separately, if we want to control it, we must attach it to a movie clip of its own as above. In this case a movie clip called flv_mc.
Controlling audio
Because we want a mute button that will change depending on the mute status (showing whether the sound is currently muted or not - visual feedback is such a wonderful thing), you need to create a variable to store the current mute status.
Insert the following code right at the top of your ActionScript:
//Mute control status, 0 = mute off sound on
var mutestatus = 0;
Then you need to create a movie clip to act as the mute button. This has just two frames, one containing an image of a speaker making noise, the other containing an image of a speaker not making noise (decide how to depict this however you like). Give the first frame the label muteon and the second frame the label muteoff. Don't forget to put a stop(); action on each of the frames or you will have an animated flickering button. Then drag an instance of the mute button onto the stage and give it the instance name mute_mc.
The following code will then allow you to mute and un-mute the sound, insert this at some point after the line you just inserted above:
//Mute control
_root.mute_mc.onRelease = function() {
if (mutestatus == 0) {
_root.mute_mc.gotoAndStop("muteon");
mutestatus = 1;
audio_sound.setVolume(0);
updateVolume();
} else if (mutestatus == 1) {
_root.mute_mc.gotoAndStop("muteoff");
mutestatus = 0;
audio_sound.setVolume(100);
updateVolume();
}
};
How does it work? Very simple. First it checks to see what the mute status is (whether it is muted yet or not). If it is not muted, it tells the button to look like it is muted (goes to the frame with the muted speaker picture), then sets the mute status to 1, then sets the volume to be 0, then updates the volume to make the 0 take effect. If, however, it is already muted, it tells the button to look like it is not muted (goes to the frame with the noisy speaker picture), then sets the mute status to 0, then sets the volume to be 100, then updates the volume to make the 100 take effect.
And that's a wrap.
Monday, 13 August 2007
Making BBC Listen Again Do What You Want
The continuing problems with the BBC Listen Again Radio Player inspired me to find my own method of listening again.
I am a big fan of BBC Radio listen again and I tap into it regularly to catch all the best programs that I miss while at work or otherwise. Usually this consists of the Art & Drama listings where I can check out what plays have been on and if there are any decent books being read. I also love BBC 7 for The 7th Dimension - devoted to SciFi and stuff like that.
One of the best things about BBC Radio listen again is their groovy web based radio player. Built in HTML it appears to use Javascript to communicate with an embedded Real Player. It's a really good piece of work (though it fails to work for my Ubuntu Studio - no embedded player you see, it is great on my Windows XP machine) for most users, designed to be easy on the eye and the brain, with all available programs no more than 2 clicks away listed by genre or station.
Only problem is, for the last 3 weeks (ish) the BBC Listen Again player has experienced problems that at first seemed to be short term but have now become part of using the system. Not only has their apology message become an almost permanent fixture in the interface (along with the sinking feeling every time I see it is still there) but it has caused considerable disruption to my listening pattern.
The BBC report that the problem is related to a corrupted database, and that "A team of BBC engineers are working full time to fix the database problems and restore normal service." Bless them, they are trying.
In the meantime I found myself being directed to the BBC 7 homepage to find direct links that would let me listen in a standalone Real Player (rather than the BBC player). OK, so here was a workaround. The only drawback was that unlike the BBC player, the listen again page only showed programs from the previous day, and not from earlier the same day.
I thought I would take a look at the construction of the links to the audio files. To my delight they were completely human readable. For instance, the following link plays the program that aired on Friday at 13:15:
http://www.bbc.co.uk/bbc7/listenagain/friday/rams/1315.ram
How simple could it be? There is named the radio station, the day and the time in 24 hour clock.
It only took a little curiosity to see if by pasting the link into the browser address bar, and altering day and time, whether I could listen to program from today (the link for which would not actually be published by the BBC until tomorrow). The experiment was a success:
http://www.bbc.co.uk/bbc7/listenagain/monday/rams/1830.ram
Evidently the BBC publish the audio files in advance, but publish the links to the audio later when they want the public to access them.
So if you want to listen to BBC7 and you know the day and time your program aired just copy the following line, paste it into your browser address bar, and fill in the [day e.g. monday] and [24 hour time e.g. 1800]:
http://www.bbc.co.uk/bbc7/listenagain/[day]/rams/[24%20hour%20time].ram
Happy listening.
I am a big fan of BBC Radio listen again and I tap into it regularly to catch all the best programs that I miss while at work or otherwise. Usually this consists of the Art & Drama listings where I can check out what plays have been on and if there are any decent books being read. I also love BBC 7 for The 7th Dimension - devoted to SciFi and stuff like that.
One of the best things about BBC Radio listen again is their groovy web based radio player. Built in HTML it appears to use Javascript to communicate with an embedded Real Player. It's a really good piece of work (though it fails to work for my Ubuntu Studio - no embedded player you see, it is great on my Windows XP machine) for most users, designed to be easy on the eye and the brain, with all available programs no more than 2 clicks away listed by genre or station.
Only problem is, for the last 3 weeks (ish) the BBC Listen Again player has experienced problems that at first seemed to be short term but have now become part of using the system. Not only has their apology message become an almost permanent fixture in the interface (along with the sinking feeling every time I see it is still there) but it has caused considerable disruption to my listening pattern.
The BBC report that the problem is related to a corrupted database, and that "A team of BBC engineers are working full time to fix the database problems and restore normal service." Bless them, they are trying.
In the meantime I found myself being directed to the BBC 7 homepage to find direct links that would let me listen in a standalone Real Player (rather than the BBC player). OK, so here was a workaround. The only drawback was that unlike the BBC player, the listen again page only showed programs from the previous day, and not from earlier the same day.
I thought I would take a look at the construction of the links to the audio files. To my delight they were completely human readable. For instance, the following link plays the program that aired on Friday at 13:15:
http://www.bbc.co.uk/bbc7/listenagain/friday/rams/1315.ram
How simple could it be? There is named the radio station, the day and the time in 24 hour clock.
It only took a little curiosity to see if by pasting the link into the browser address bar, and altering day and time, whether I could listen to program from today (the link for which would not actually be published by the BBC until tomorrow). The experiment was a success:
http://www.bbc.co.uk/bbc7/listenagain/monday/rams/1830.ram
Evidently the BBC publish the audio files in advance, but publish the links to the audio later when they want the public to access them.
So if you want to listen to BBC7 and you know the day and time your program aired just copy the following line, paste it into your browser address bar, and fill in the [day e.g. monday] and [24 hour time e.g. 1800]:
http://www.bbc.co.uk/bbc7/listenagain/[day]/rams/[24%20hour%20time].ram
Happy listening.
Subscribe to:
Posts (Atom)
