iOS shakes the phone and plays the example of a micro-letter rocking Sound

Source: Internet
Author: User
Tags file url

Implement micro-letter shake play sound, the code is as follows:

-(void) Motionbegan: (uieventsubtype) Motion withevent: (uievent *) event
{

if (motion = = Uieventsubtypemotionshake) {
NSLog (@ "Shake a Shake, hahaha");
[Self playsoundeffect:@ "Yaoyiyao"];
}
}
-(void) Playsoundeffect: (NSString *) name{
1. Get System sound ID
Systemsoundid Soundid;
/**
* Infileurl: Audio file URL
* Outsystemsoundid: Sound ID (This function adds the sound file to the System Audio service and returns a long shaping ID)
*/
NSString *audiofile=[[nsbundle Mainbundle] pathforresource:name oftype:@ "m4r"];
Nsurl *fileurl=[nsurl Fileurlwithpath:audiofile];
Audioservicescreatesystemsoundid ((__bridge cfurlref) (FILEURL), &soundid);

2. Play Audio
Audioservicesplaysystemsound (Soundid);

3. Play Vibration
Audioservicesplaysystemsound (ksystemsoundid_vibrate);
Audioservicesplayalertsound (Soundid)/play sound and vibrate

}

Here the Yaoyiyao is the sound name, the suffix name is. m4r, micro-mail shake the sound of the file can be downloaded online. In the simulator, can also like the real machine to achieve the same shaking mobile phone, operation: Hardware-->shake gesture.

Related Article

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.