Shake a lottery sound problem

Source: Internet
Author: User

Recently in doing a shake draw small project, encountered to be in the draw at different stages of the sound of the stage sounded. "Lottery part", "shaking", "winning", "not winning" four stages, sounded different voices.

On iOS5, an audio file can only be loaded from a user-triggered touch (click) event, and if the AutoPlay attribute is used in HTML markup, Safari ignores this property and does not play the file when the page is loaded, and Safari also ignores the preload property.

The only thing that can be solved is that the user enters the page and lets the user trigger the touch event:

$ ("#choujiangBtn"). Unbind (). Bind ("Touchstart", function () {
Audiodata.firstrock = true; When you click the Draw button, set Audiodata.firstrock to True to indicate that you can shake your phone to draw
AudioData.audio.load ();
Playaudio ();
});

Audiodata is an object that stores audio:

Audiodata = {
Audio:new Audio ("Soundeff.mp3"),
Firstrock:false,
Start: {
Starttime:1,
Length:4
},
Shake: {
Starttime:6,
Length:3
},
Win: {
Starttime:12,
Length:3
},
Miss: {
Starttime:16,
Length:1
}
}

Here, you use the audio sprite to synthesize all the sounds into a single audio stream, and then play the various parts of the stream. Because the HTML5 Media Elements in iOS Safari are singleton, only one HTML5 audio (and HTML5 video) stream can be played at a time.

To play the event:

function Playaudio () {
Clearinterval (Audiodata.timer);
AudioData.audio.addEventListener ("Canplaythrough", function () {
AudioData.audio.play ();
Repeat the Start section, and the next few stages play only once
Audiodata.timer = setinterval (function () {
if (AudioData.audio.currentTime >= (audioData.shake.startTime)/2&&audiodata.audio.currenttime< AudioData.shake.startTime) {
AudioData.audio.currentTime = AudioData.start.startTime;
}
}, 3000);
}, False);
}

Play Winaudio similarly. It is important to note that changing currenttime is not exactly the right one. Suppose CurrentTime is set to 6.5, and actually gets 6.7. A small amount of space is required between each audio sprite to avoid finding the head of another sprite.

Before accessing any audio sprite, it is important to ensure that the entire audio stream is loaded, because if the audio stream is not fully loaded, then when you want to access any part of a stream that has already been loaded, the stream needs to be buffered, and delays occur during the stream loading process.

When shaking phone events:

Window.addeventlistener (' Devicemotion ',function() {                if(audiodata.firstrock) {varAcceleration = event.accelerationincludinggravity;//get the acceleration of the devicex = acceleration.x;//gets the x-axis of acceleration, which is used to calculate horizontal horizontal accelerationy = acceleration.y;//Gets the y-axis of the acceleration for calculating the vertical acceleration, while calculating the positive value                    //calculates whether the current acceleration is greater than the default acceleration                    if(Math.Abs (X-LASTX) > Speed | | Math.Abs (Y-lasty) >Speed ) {AudioData.audio.currentTime= AudioData.shake.startTime;//change the current playback time, play the shaking partGetValue ();//Call the Lottery interface to determine whether or not to winAudiodata.firstrock =false;//change the Audiodata.firstrock to false and not shake again;Clearinterval (Audiodata.timer);//Clear Loop play start part sound                    }                    //re-record the last value as the next start coordinateLASTX =x; Lasty=y;            }; }, false);

Based on the value obtained by GetValue () to determine whether to win:

if(num) {audioData.audio.currentTime= AudioData.win.startTime;//change the current playback time to play the winning part                //play the winning sound clip and stop playing to prevent the winning sound clip from playingSetTimeout (function() {audioData.audio.pause ();                }, AudioData.win.length); ShowPage2 (num); //go to the winning page}Else{audioData.audio.currentTime= AudioData.miss.startTime;//change the current playback time to play the not winning partShowPage3 (num);//go to the not winning page};

If not, click the button again to set the Audiodata.firstrock to true, go to the homepage, shake again, and extract again.

If winning, click the Fill Information button, enter the personal information fill out the interface, complete the lottery.

Shake a lottery sound problem

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.