⬆️ ⬇️

Event sounds are just

Modern projects are more and more personalized. One of the latest projects assumed the constant presence of the user on the site. The user is the dispatcher and tracks the appearance of new orders, participates in auctions and so forth. In addition to the standard SMS notifications and so forth, I wanted to make a sound, because users are constantly constantly on the project. The easiest way seemed flush. Almost everyone already has a flash, everyone watches YouTube, plays farmer and so on. And despite my anti-Fleshev views, I decided that the flash is best suited for playing sound.

However, when I tried to write a flash drive to play sounds, I suddenly remembered that I had not worked with a flash for 5 years = (and instead of writing my component, I started looking for ready-made ...



Almost immediately, I found the article “ Managing Flash Objects in Javascript ”, where apelsyn quite clearly showed how to build interfaces between JS and Flash just using the example of a similar mini player of sounds, melodies and so on. Since my study of flash, much has changed. Action Script from a primitive macro language has become a powerful scripting language. And despite the fact that apelsyn chewed in enough detail like that and even posted the source code, I could not repeat his experience in Flash CS4 = (I decided to use it with SWF without my own edits, however, an ambush awaited me here. The path to the directory with Ringtones apelsyn wrote in the code and what is most terrible is that it is relative. As a result, on the main page its flash drive reproduced sound without any problems, but when I went deeper into the site, the flash drive spat out the error.

At that time, the idea of ​​“screwing the sound in 5 minutes” was implemented for several hours. I really wanted to get the sound, but I felt like a robot, lost orientation and beating against the wall. On the one hand, the businessman inside me said “Hammer and do next, in the extreme case, order it on free-lance.ru,” but the programmer did not get tired of repeating “Well, you and the lamer, you cannot write a flash drive to play the sound.”

In the end, I finally got angry, opened a new flash project and did write this player, and the devil really was not so scary. With all due respect to apelsyn , the code was 10 times less and it worked, which is important.



Flash file:

import flash.external.ExternalInterface;

ExternalInterface.addCallback( "playMusic" , playMusic);

ExternalInterface.addCallback( "stopMusic" , stopMusic);



import flash.media.Sound;

import flash.media.SoundChannel;

import flash.net.URLRequest;



var _sound:Sound;

var _channel:SoundChannel;



function playMusic(file:String= '' , count: int =0)

{

_sound = new Sound( new URLRequest(file));

_channel=_sound.play(0,count);

}

function stopMusic() {

_channel.stop();

}




* This source code was highlighted with Source Code Highlighter .




and JS code:

function musicLoad()

{

swfobject.embedSWF( "/swf/music.swf" , "sound" , "0" , "0" , "9.0.0" );

}

function musicPlay (file, count)

{

if (!file)

{

file= '/sound/default.mp3' ;

}

if (!count)

{

count=1;

}

document .getElementById( 'sound' ).playMusic(file, count);

}

function musicStop ()

{

document .getElementById( 'sound' ).stopMusic();

}




* This source code was highlighted with Source Code Highlighter .


')

Accordingly, for the script to work, you need to connect swfobject.js, use something like this:

musicPlay( '/sound/123.mp3' );

musicStop();




* This source code was highlighted with Source Code Highlighter .




That seems to be all =)

ps I do not claim to be a flash guru, on the contrary, I will be glad to any criticism and correct all inaccuracies in the code.

Source: https://habr.com/ru/post/67786/



All Articles