Photoshop Tutorials, Flash Tutorials, 3Ds Studio Max Tutorials, Web Design Tutorials |
Home | Submit Tutorial | Top Sites | Free Templates (NEW) | Website templates | Privacy Policy | Link Exchange | Contact Us |
|
Welcome To ProDesignHost.com Flash Tutorials Area - Making a simple MediaplayerOne of the most frequently asked questions down in the forum is how
to make a media player. This is done pretty easily in Flash. In this
tutorial, we'll start with a simple player, and then make it more and
more advanced. So, let's begin! So, before you start any project, you need to make yourself some goals. This is what we're going to do in this tutorial. So, without further delay... let's get coding. First, make a folder
in your libary called 'mediaplayer'. All the symbols that the
mediaplayer uses will be stored in this folder (this is only to order
the symbols. You don't have to change anything when using folders). stop(); this.onRollOver = function () { this.gotoAndStop(2); } this.onPress = function () { this.gotoAndStop(3); } this.onRelease = function () { this.gotoAndStop(2); _parent.switchTrack(this.nr); } this.onRollOut = function () { this.gotoAndStop(1); } So, I owe you a bit of explenation. This code makes the movieclip
work like a button. The on... statements set the button to a certain
frame when the user clicks or rolls the mouse over the button. In the
onRelease statement a function called switchTrack is executed. That
function will change the playing track. You need to put _parent in
front of the name, because the function is defined in the mediaplayer
movieclip. And because these buttons are made to be put in that
movieclip, _parent will point to the mediaplayer mc. When your done,
delete the button of the stage. It's in the libary and that's just
where we want it.
Now that we have the buttons fixed, we can start with the actual
mediaplayer. We set as one of our goals that we want the sounds to be
loaded from a external, so they don't have to be kept inside the swf,
thus keeping the filesize low. soundUrls = new Array("loop1.mp3"); btn1.nr = 0; btn1.number_txt.text = btn1.nr+1; snd = new Sound(); snd.onLoad = function (succes) { if (succes) { snd.start(0, 999); } else { trace("loading failed"); } } switchTrack = function (trackNr) { snd.stop(); snd.loadSound(soundUrls[trackNr]); }
First we define an array to hold the url's of our tracks. In my case,
the first one is called 'loop1.mp3'. Then we set the label and number
of our button. We need to set the number so the button can pass it on
to the switchTrack function to change the track into the correct one. soundUrls = new Array("loop1.mp3", "loop2.mp3", "loop3.mp3"); btn1.nr = 0; btn1.number_txt.text = btn1.nr+1; btn2.nr = 1; btn2.number_txt.text = btn2.nr+1; btn3.nr = 2; btn3.number_txt.text = btn3.nr+1; snd = new Sound(); snd.onLoad = function (succes) { if (succes) { snd.start(0, 999); } else { trace("loading failed"); } } switchTrack = function (trackNr) { snd.stop(); snd.loadSound(soundUrls[trackNr]); } Now for the stop button. Just create a button in the way you want it
to look like, and then give it the same actions as the track choose
button only the onRelease function has _parent.snd.stop() instead of
the other actions.
We've completed these goals: soundUrls = new Array("loop1.mp3", "loop2.mp3", "loop3.mp3", "loop4.mp3"); nexty = 28; nextx = 0; for (var i=0; i!=soundUrls.length; i++) { mc = this.attachMovie("button_mc", "btn"+i, i); mc._x = nextx; mc._y = nexty; nextx += mc._width+10; mc.nr = i; mc.number_txt.text = mc.nr+1; } this.attachMovie("stop_mc", "stop_mc", i+1); stop_mc._x = nextx; stop_mc._y = nexty; snd = new Sound(this); snd.onLoad = function (succes) { if (succes) { snd.start(0, 999); } else { trace("loading failed"); } } switchTrack = function (trackNr) { snd.stop(); snd.loadSound(soundUrls[trackNr]); } Well, the bold area is what's changed. The code now looks into the array, sees how many elements there are, and then makes the buttons accordingly. First you define the starting x and y coordinates in the variables nextx and nexty. Then we make a loop that loops the same amount of times as the number of elements in the soundUrls array. In the loop we take the symbol button_mc out of the libary and stick it onto the stage with the function attachMovie. The first parameter is the linkage identifier and the second one the instance name on the stage. The third parameter is the depth (remember, NO two movieclips can have the same depth. If they do, neither of them will appear). Then we set the x and y coordinates of the newly created movieclip. After that we set the new x coordinate and the button number and text. When the loop is done iterating we finally set the stop button on the stage and then the code is the same as the last! The preloaderThis is the last of our goals. To test your swf on a certain
connection speed, press ctrl+enter (or just do Test Movie) and then
view-Streaming Graph. And then view-Simulate download. Then you can
take a look at how things will load. soundUrls = new Array("loop1.mp3", "loop2.mp3", "loop3.mp3", "loop4.mp3"); nexty = 28; nextx = 0; for (var i=0; i!=soundUrls.length; i++) { mc = this.attachMovie("button_mc", "btn"+i, i); mc._x = nextx; mc._y = nexty; nextx += mc._width+10; mc.nr = i; mc.number_txt.text = mc.nr+1; } this.attachMovie("stop_mc", "stop_mc", i+1); stop_mc._x = nextx; stop_mc._y = nexty; snd = new Sound(this); snd.onLoad = function (succes) { if (succes) { snd.start(0, 999); } else { trace("loading failed"); } } switchTrack = function (trackNr) { snd.stop(); snd.loadSound(soundUrls[trackNr]); bt = snd.getBytesTotal(); this.onEnterFrame = function () { bl = snd.getBytesLoaded(); pr = bl/bt*100; percent_txt.text = Math.floor(pr)+'%'; bar_mc._xscale = pr; if (pr == 100) { delete this.onEnterFrame; } } } If you have ever written a preloader this code looks very familiar. It works in exactly the same way, it only works with the sound class instead of the movieclip. First we make a variable that holds the total number of bytes of the mp3 and then we create a onEnterFrame handler that calculates the percentage loaded and scales the bar accordingly. If it's done, it deletes the onEnterFrame again to spare processing power. If you check your objectives, we've finished everything. You can hardly call this a simple mediaplayer anymore :) . But don't let this tutorial limit you. Keep learning and expand your reach. If there's anything you can do for me it's this: rate the tutorial by clicking on a square at the top of the page. If you click on the first square it's a low rating if you click on the last it's a high rating. MoreAuthor: MicrOchip
|
|
Premium Partners | |||||
|
|||||
Free website templates and paid web templates are great tools to make your websites look perfect! You will save time and money with our flash templates and free website templates
Our visitors are satisfied with the quality of our free and paid website templates! Please visit our free website templates and paid website templates sections. We offer free web templates, free web layouts, free web page templates and other stuff for free download. All templates come with the html and external css file so you may easily edit HTML with your favorite HTML editor. Feel free to download our free web templates for your personal websites. Terms of use depend upon the website template vendor. |
Home | Submit Tutorial | Top Sites | Free Templates | Website templates | Privacy Policy | Contact Us |