When building a website, the customer asked for a painful "voice prompt for new orders", so he spent some time trying to play MP3, WMA, in an inaccessible browser, these WAV formats are not ideal after tests, so I thought of the idea of playing MP3 with flash. This should be the most common one .. However, a SWF-format audio player is required. I have found many online audio players with buttons, but I only need to put MP3 directly. So I wrote one with Flex, and the code was very simple .. As follows:
<?xml version="1.0" encoding="utf-8"?><s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" initialize="init()" width="1" height="1" xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" xmlns:local="*"><fx:Declarations><!-- Place non-visual elements (e.g., services, value objects) here --></fx:Declarations><fx:Script><![CDATA[import flash.media.*;private function init():void{var s:Sound = new Sound(new URLRequest(parameters.audiofile));s.play(0,0,null);}]]></fx:Script></s:Application>
Then, a playback method is written in HTML with the flashvars parameter to pass the path of the MP3 file:
Function play () {If (issoundon) {// if the sound is enabled, play the video; otherwise, ignore it $ (". alert "). remove (); $ ("body "). append ("<embed src = '.. /jinanweb. web/alert/alert.swf 'class = 'alert 'style = 'position: absolute; margin-top:-1; width: 1px; Height: 1px; Z-index: 0; 'flashvars = 'audiofile = .. /jinanweb. web/alert/alert.mp3 '> </embed> "); // flashvars is obtained by SWF and cannot be renamed. audiofile cannot be changed, because the above flex Code specifies} var oldcount = 0; var issoundon = true; Function C Hecknewdanzi (type) {// your function. You can use Ajax round-robin to check whether there is a new list. If there is a new list, call play (). If there is no list, do nothing. // play (); (function playalert () {var func = This; window. setTimeout (function () {$. ajax ({URL :".. /jinanweb. web/alert. ashx? T = "+ type, cache: false, success: function (e) {If (parseint (e)> 0) {If (oldcount = 0) {oldcount = E ;} else {If (oldcount! = E) {play (); oldcount = e ;}} playalert () ;}}) ;}, 3000) ;}() ;}$ (function () {// sound switch control $ (". alertbg "). live ("click", function () {if ($ (this ). ATTR ("src "). indexof ("soundon")>-1) {$ (this ). hide (100 ). ATTR ("src ",'.. /jinanweb. web/images/soundoff.png '). show (100); issoundon = false;} else {$ (this ). hide (100 ). ATTR ("src ",'.. /jinanweb. web/images/soundon.png '). show (100); issoundon = true ;}}); // Add a sound image during initialization, and fix it to the lower left corner of the browser $ ("body "). append (" ");});
If there are any deficiencies, I hope to correct them ..