2012年4月23日

iPhone iOS vibration and sound


I can't find complete iOS iPhone vibration and sound info on internet 。
After surfing Google, I write it down for my memory:

1st part: Audio play
   Q:how to play a sound?
   Ans:
    1. System Sound Services
      it's a basic sound play method,call AudioServicesPlaySystemSound to play some simple sound.

      note:this method only for some short alert and notice sound,there a lot of limitation: 
      • No longer than 30 seconds in duration
      • In linear PCM or IMA4 (IMA/ADPCM) format
      • Packaged in a .caf, .aif, or .wav file
      • In addition, when you use the AudioServicesPlaySystemSound function:

      • Sounds play at the current system audio volume, with no programmatic volume control available
      • Sounds play immediately
      • Looping and stereo positioning are unavailable
      • Simultaneous playback is unavailable: You can play only one sound at a time
               Detail info please refer to link
    2. AVAudioPlayer
      AVAudioPlayer is one of AVFoundation.framework Class,so that please remember to add AVFoundation.framework before use it
      AVAudioPlayer is one kind of enhance player,it supports more sound encoding format, here is the supported format:
      • AAC
      • AMR(AdaptiveMulti-Rate, aformatforspeech)
      • ALAC(AppleLossless)
      • iLBC(internetLowBitrateCodec, anotherformatforspeech)
      • IMA4(IMA/ADPCM)
      • linearPCM(uncompressed)
      • µ-lawanda-law
      • MP3(MPEG-1audiolayer3)
                Please refer to link for detail
    
    Q: is there any piece of code for SystemSound Services?
    Ans:
   - (void) showReminder{
    SystemSoundID sysSoundId = 0;
    NSString *path = [[NSBundle mainBundlepathForResource:@"alarm" ofType:@"caf"];
    NSURL *filepath = [NSURL fileURLWithPath: path];
    AudioServicesCreateSystemSoundID((__bridge_retained CFURLRef) filepath, &sysSoundId);
    AudioServicesPlaySystemSound(sysSoundId);
    AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);
   }



    Q:Why I got Unknown type name 'SystemSoundID' error in complier?
    Ans:
          Please add this line to .h file
          #include
 
    Q:still has problem after add that line,it complain 
   Undefined symbols for architecture armv7 "_AudioServicesPlayAlertSound", referenced from: xxx 
    Ans:
         Please add AudioToolBox.framework to framework
         procedures:
right click on Project->select Target of BuildPhase -> Link Binary With Libraries
         then add AudioToolBox.framework

     

    Q6:Pass the compiler while don't here any sound still?
    Ans:pelase check your sound file format and encoding,
             please turn on your sound in iPhone setting。
           
             btw,you can use the Developer tool (afconvert) in terminal to convert it to CAF file format to make sure it's works for iPhone
             here is the instruction example
          afconvert -f caff -d LEI16@44100 -c 1 jj01.mp3 jj01.caf

    Q7:is there any AVAudioPlayer example code?
    Ans:

      NSString *soundFilePath = 
    [[NSBundle mainBundlepathForResource@"as01" 
                                    ofType@"caf"]; 
    NSURL *fileURL = [[NSURL allocinitFileURLWithPath: soundFilePath]; 
    AVAudioPlayer *newPlayer = 
    [[AVAudioPlayer allocinitWithContentsOfURL: fileURL
                                           errornil];
    self.player = newPlayer; 
    [self.player prepareToPlay];
    
    self.player.delegate=self;
    self.player.numberOfLoops = -1;    // Loop playback until invoke stop method
    [self.player play];  





2st part:Vibration
Q1: How to do vibrate on iOS iPhone

Ans: please use AudioServicesPlayAlertSound(kSystemSoundID_Vibrate)。


Q2: how to do vibration continuously?

Ans: Please use AudioServicesAddSystemSoundCompletion
,iOS will use AudioServicesAddSystemSoundCompletion Callback to notify us the system had done a vibration, then we can call AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);to keep vibration .

Q3:is there any piece of sample code for AudioServicesAddSystemSoundCompletion

Ans:

- (void) showReminder:(NSString *) text{  SystemSoundID sysSoundId = 0;
  NSString *path = [[NSBundle mainBundle] pathForResource:@"alarm" ofType:@"caf"];
  NSURL *filepath = [NSURL fileURLWithPath: path];
  AudioServicesCreateSystemSoundID((__bridge_retained CFURLRef) filepath, &sysSoundId);
  AudioServicesAddSystemSoundCompletion(sysSoundId, 
     NULL, NULL, completionCallback ,(__bridge void *) self);
  AudioServicesPlaySystemSound(sysSoundId);
  AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);
}

  static void completionCallback (SystemSoundID mySSID, void* data) {
  // Play again after sound play completion
  AudioServicesPlaySystemSound(mySSID);
  AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);
}

沒有留言:

張貼留言