iOS set sound and vibration, individually controlled

Source: Internet
Author: User
Tags uikit

First, today the project involves setting this fast sound vibration and ringing, make the head big, before, just simple call system method can be realized, but now the company requirements, vibration is vibration, bell is ringing, I saw, also separate, do very good, but I wonder, how to do, There are a lot of ways to read online, all of which are the following code. But this can not meet my project needs, I am puzzled, I set the sound and vibration, why in the sound when sounded, he will call the vibration, this let me very puzzled, so I looked at a lot of online .... This is what the online code 90% says.

1         //Online Single Chat2         if([[[[Nsuserdefaults Standarduserdefaults] Objectforkey:@"ISMESSAGE_NOTF"] Isequaltostring:@"0"]) {3             4}Else if([[[[Nsuserdefaults Standarduserdefaults] Objectforkey:@"ISMESSAGE_NOTF"] Isequaltostring:@"1"] && [[[Nsuserdefaults Standarduserdefaults] Objectforkey:@"Isvoice_set"] Isequaltostring:@"1"]){5             6Audioservicesplaysystemsound (1007);//Sound7             8}Else if([[[[Nsuserdefaults Standarduserdefaults] Objectforkey:@"ISMESSAGE_NOTF"] Isequaltostring:@"1"] && [[[Nsuserdefaults Standarduserdefaults] Objectforkey:@"Isvibration_set"] Isequaltostring:@"1"])9         {TenAudioservicesplaysystemsound (ksystemsoundid_vibrate);//Vibration One}

Of course, this is my project in my code, but I also see on the internet, directly pulled down with, sure enough, so write up, was the test department, to fight back, said the vibration closed, how there is vibration, then I am crazy ah, I do not know why Ah, I used to write this is good, hey, so, began to surf the internet, Baidu, Google, Stockoverflow, and so on some web site to see, are useless to find, finally in the blog Park to see, and then according to what he said to write, he wrote a single case, and then vibrate when the call vibration method, the bell call method of ringing, So I wrote a singleton to call, and sure enough solved my project needs ...

1 The code in the LxxPlaySound.h2 3 #import<UIKit/UIKit.h>4 5 #import<AudioToolbox/AudioToolbox.h>6 7  8 9 @interfaceLxxplaysound:nsobjectTen  One { A  - Systemsoundid Soundid; -  the } -  -   -  + /** -  + * @brief to initialize the play vibration effect A  at  * -  - * @return Self -  -  */ -  in-(ID) initforplayingvibrate; -  to   +  - /** the  * * @brief for playback system sound initialization (no audio file required) $ Panax Notoginseng  * -  the * @param resourcename system sound name +  A * @param type system sound the  +  * -  $ * @return Self $  -  */ -  the-(ID) Initforplayingsystemsoundeffectwith: (NSString *) resourcename ofType: (NSString *) type; - Wuyi   the  - /** Wu  - * @brief initialization for playback of specific audio files (audio files required) About  $  * -  - * @param filename Audio file name (added to project) -  A  * +  the * @return Self -  $  */ the  the-(ID) Initforplayingsoundeffectwith: (NSString *) filename; the  the   -  in /** the  the * @brief Play sound About  the  */ the  the-(void) play; +  -   the Bayi @end

1 The code in the LXXPLAYSOUND.M2 3 #import "LxxPlaySound.h"4 5  6 7 @implementationLxxplaysound8 9  Ten  One-(ID) Initforplayingvibrate A  - { -  theSelf =[Super init]; -  -     if(self) { -  +Soundid =ksystemsoundid_vibrate; -  +     } A  at     returnSelf ; -  - } -  -   -  in-(ID) Initforplayingsystemsoundeffectwith: (NSString *) resourcename ofType: (NSString *) Type -  to { +  -Self =[Super init]; the  *     if(self) { $ Panax NotoginsengNSString *path = [[NSBundle bundlewithidentifier:@"Com.apple.UIKit"] Pathforresource:resourcename Oftype:type]; -  the         if(path) { +  A Systemsoundid Thesoundid; the  +Osstatus error = Audioservicescreatesystemsoundid (__bridge cfurlref) [Nsurl Fileurlwithpath:path], &thesoundid); -  $             if(Error = =kaudioservicesnoerror) { $  -Soundid =Thesoundid; -  the}Else { - WuyiNSLog (@"Failed to create sound"); the  -             } Wu  -         } About  $          -  -     } -  A     returnSelf ; +  the } -  $   the  the-(ID) Initforplayingsoundeffectwith: (NSString *) filename the  the { -  inSelf =[Super init]; the  the     if(self) { About  theNsurl *fileurl =[[NSBundle Mainbundle] Urlforresource:filename Withextension:nil]; the  the         if(FileURL! =Nil) +  -         { the Bayi Systemsoundid Thesoundid; the  theOsstatus error = Audioservicescreatesystemsoundid (__bridge cfurlref) FileURL, &thesoundid); -  -             if(Error = =kaudioservicesnoerror) { the  theSoundid =Thesoundid; the  the}Else { -  theNSLog (@"Failed to create sound"); the  the             }94  the         } the  the     }98  About     returnSelf ; - 101 }102 103  104  the-(void) Play106 107 {108 109 Audioservicesplaysystemsound (soundid); the 111 } the 113   the  the-(void) Dealloc the 117 {118 119 Audioservicesdisposesystemsoundid (soundid); - 121 }122 123 @end

Then begin to call him in your vibration and voice. Simply introduce his header file and you're ready to start your setup ... Your settings can be called, you want to vibrate, you want to open the bell ring ....

The following code shows the call vibration

// Vibration first to introduce a header file *playsound =[[   
[PlaySound play];

This is my bitter force for two days to solve the problem, only because I am too food, my eldest brother has been helping me, thank him @ the Dull me .....

Hope can help Some like me, such as the bitter force of the yard, if there is not clear to find me, learn from each other, work together!! Day's Up!!!

iOS set sound and vibration, individually controlled

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.