IOS實現靜音和震動以及搖一搖功能

導入頭文件

#import <AudioToolbox/AudioToolbox.h>

添加一個自己的音頻

  • inFileURL : 音頻文件路徑
  • outSystemSoundID:該音頻標記的ID,為后面播放此音頻時的查找
AudioServicesCreateSystemSoundID(   CFURLRef                    inFileURL,
                                    SystemSoundID*              outSystemSoundID)
  • 實現案列
AudioServicesCreateSystemSoundID((__bridge CFURLRef)([NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"shake_sound_male.wav" ofType:@""]]), &shakingSoundID);

替換系統的播放音頻

  • inSystemSoundID : 添加音頻到系統時標記的ID
 AudioServicesPlaySystemSound(SystemSoundID inSystemSoundID)    
  • 播放上面的一段音頻文件
AudioServicesPlaySystemSound(shakingSoundID);

實現控制消息的靜音和震動功能

邏輯:
在設置頁面放置:switch按鈕,控制靜音和震動按鈕,
- 靜音:播放一段沒有聲音的音頻文件
- 震動:播放一段震動的音頻文件
使用場合:

  • 聊天消息:
  • 本地通知和APNS:

// 播放接收到新消息時的聲音

- (SystemSoundID)playNewMessageSound
{
    // 要播放的音頻文件地址
    NSString *audioStr = [[NSBundle mainBundle] pathForResource:@"right_answer" ofType:@"mp3"];
    NSURL *audioPath = [[NSURL alloc] initFileURLWithPath:audioStr];
    //    NSURL *bundlePath = [[NSBundle mainBundle] URLForResource:@"EaseMob" withExtension:@"bundle"];
    //    NSURL *audioPath = [[NSBundle mainBundle] pathForResource:@"right_answer" ofType:@"mp3"];
    // 創建系統聲音,同時返回一個ID
    SystemSoundID soundID;
    AudioServicesCreateSystemSoundID((__bridge CFURLRef)(audioPath), &soundID);
    // Register the sound completion callback.
    AudioServicesAddSystemSoundCompletion(soundID,
                                          NULL, // uses the main run loop
                                          NULL, // uses kCFRunLoopDefaultMode
                                          EMSystemSoundFinishedPlayingCallback, // the name of our custom callback function
                                          NULL // for user data, but we don't need to do that in this case, so we just pass NULL
                                          );
    
    AudioServicesPlaySystemSound(soundID);
    
    return soundID;
}

// 震動

- (void)playVibration
{
    // Register the sound completion callback.
    AudioServicesAddSystemSoundCompletion(kSystemSoundID_Vibrate,
                                          NULL, // uses the main run loop
                                          NULL, // uses kCFRunLoopDefaultMode
                                          EMSystemSoundFinishedPlayingCallback, // the name of our custom callback function
                                          NULL // for user data, but we don't need to do that in this case, so we just pass NULL
                                          );
    
    AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
}

實現搖一搖功能

  • 設置app是否支持震動功能
[UIApplication sharedApplication].applicationSupportsShakeToEdit = YES;
  • 實現代理
-(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event ;
  • 代理實現方式
#pragma mark - Event Delegate
-(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
    if(motion == UIEventSubtypeMotionShake) {
        // 播放聲音
        AudioServicesPlaySystemSound(shakingSoundID);
//        AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
        
        // 真實一點的搖動動畫
        [self shaking];
    }
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容