代码笔记调用系统震动和声音
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了代码笔记调用系统震动和声音相关的知识,希望对你有一定的参考价值。
代码:
RootViewController.m
#import "RootViewController.h"
#import <AudioToolbox/AudioToolbox.h>
@interface RootViewController ()
@end
@implementation RootViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[email protected]"RootViewController";
//这个震动不震动,有没有声音是和手机的设置一样的。
//调用系统震动
[self getChatMessageGoToShake];
//调用系统声音
[self getChatMessageGoToSound];
}
#pragma -mark -调用系统震动
- (void)getChatMessageGoToShake
{
//调用系统震动
AudioservicesPlaySystemSound(kSystemSoundID_Vibrate);
}
#pragma -mark -调用系统声音
- (void)getChatMessageGoToSound
{
//调用系统声音
NSString *path = [NSString stringWithFormat:@"/System/Library/Audio/UISounds/%@.%@",@"sms-received3",@"caf"];
if (path) {
SystemSoundID sd;
OSStatus error = AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:path],&sd);
//获取声音的时候出现错误
if (error != kAudioServicesNoError) {
NSLog(@"----调用系统声音出错----");
sd = 0;
}
AudioServicesPlaySystemSound(sd);
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
以上是关于代码笔记调用系统震动和声音的主要内容,如果未能解决你的问题,请参考以下文章
android 设置整个app的通知notification 的声音和震动用啥方法