Compatibility with audio file (mp3) playback in different browsers

Source: Internet
Author: User

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 ..

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.