ios播放系统自带音效以及震动

简介: <div style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"> int systemSoundID;</div> <div style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:
int systemSoundID;
AudioServicesPlaySystemSound(systemSoundID);
//systemSoundID的取值范围在1000-2000
//播放自己的声音,但此种播放方法主要播放一些较短的声音文件
NSString *path = [NSString stringWithFormat:@"%@%@", [[NSBundle mainBundle] resourcePath], 

@"/jad0007a.wav"];

id SystemSoundID soundID;
NSURL *filePath = [NSURL fileURLWithPath:path isDirectory:NO];
AudioServicesCreateSystemSoundID((CFURLRef)filePath, &soundID);
AudioServicesPlaySystemSound(soundID);


需求大致分为三种:
1.震动
2.系统音效(无需提供音频文件)
3.自定义音效(需提供音频文件)
我的工具类的封装:
[cpp] 
// 
//  WQPlaySound.h 
//  WQSound 
// 
//  Created by 念茜 on 12-7-20. 
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved. 
// 
 
#import <UIKit/UIKit.h> 
#import <AudioToolbox/AudioToolbox.h> 
 
@interface WQPlaySound : NSObject 
    SystemSoundID soundID; 
 
 
-(id)initForPlayingVibrate; 
 
 
-(id)initForPlayingSystemSoundEffectWith:(NSString *)resourceName ofType:(NSString *)type; 
 
 
-(id)initForPlayingSoundEffectWith:(NSString *)filename; 
 
 
-(void)play; 
 
@end 

[cpp]
// 
//  WQPlaySound.m 
//  WQSound 
// 
//  Created by 念茜 on 12-7-20. 
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved. 
// 
 
#import "WQPlaySound.h" 
 
@implementation WQPlaySound 
 
-(id)initForPlayingVibrate 
    self = [super init]; 
    if (self) { 
        soundID = kSystemSoundID_Vibrate; 
    } 
    return self;     
 
-(id)initForPlayingSystemSoundEffectWith:(NSString *)resourceName ofType:(NSString *)type 
    self = [super init]; 
    if (self) { 
        NSString *path = [[NSBundle bundleWithIdentifier:@"com.apple.UIKit"] pathForResource:resourceName ofType:type]; 
        if (path) { 
            SystemSoundID theSoundID; 
            OSStatus error =  AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:path], &theSoundID); 
            if (error == kAudioServicesNoError) { 
                soundID = theSoundID; 
            }else { 
                NSLog(@"Failed to create sound "); 
            } 
        } 
         
    } 
    return self; 
 
-(id)initForPlayingSoundEffectWith:(NSString *)filename 
    self = [super init]; 
    if (self) { 
        NSURL *fileURL = [[NSBundle mainBundle] URLForResource:filename withExtension:nil]; 
        if (fileURL != nil) 
        { 
            SystemSoundID theSoundID; 
            OSStatus error = AudioServicesCreateSystemSoundID((__bridge CFURLRef)fileURL, &theSoundID); 
            if (error == kAudioServicesNoError){ 
                soundID = theSoundID; 
            }else { 
                NSLog(@"Failed to create sound "); 
            } 
        } 
    } 
    return self; 
 
-(void)play 
    AudioServicesPlaySystemSound(soundID); 
 
-(void)dealloc 
{  
    AudioServicesDisposeSystemSoundID(soundID); 
@end 

调用方法步骤:
1.加入AudioToolbox.framework到工程中
2.调用WQPlaySound工具类
2.1震动
[cpp] 
WQPlaySound *sound = [[WQPlaySound alloc]initForPlayingVibrate]; 
[sound play]; 
2.2系统音效,以Tock为例
[cpp] 
WQPlaySound *sound = [[WQPlaySound alloc]initForPlayingSystemSoundEffectWith:@"Tock" ofType:@"aiff"]; 
[sound play]; 
2.3自定义音效,将tap.aif音频文件加入到工程
[cpp] 
WQPlaySound *sound = [[WQPlaySound alloc]initForPlayingSoundEffectWith:@"tap.aif"]; 
[sound play]; 
目录
相关文章
|
1月前
|
搜索推荐 Android开发 iOS开发
安卓与iOS系统的用户界面设计对比分析
本文通过对安卓和iOS两大操作系统的用户界面设计进行对比分析,探讨它们在设计理念、交互方式、视觉风格等方面的差异及各自特点,旨在帮助读者更好地理解和评估不同系统的用户体验。
21 1
|
2月前
|
搜索推荐 Android开发 iOS开发
探析安卓与iOS系统的优劣
【2月更文挑战第7天】安卓与iOS是当今手机市场上最主流的两款操作系统,各有优劣。本文将从用户体验、开放程度、生态系统等方面对两者进行深入探析,以期帮助读者更好地了解它们的特点。
|
1月前
|
机器学习/深度学习 测试技术 API
iOS系统下轻松构建自动化数据收集流程
iOS系统下轻松构建自动化数据收集流程
26 0
|
1月前
|
搜索推荐 安全 Android开发
安卓与iOS系统的用户体验比较
【2月更文挑战第11天】 在当今移动设备市场上,安卓和iOS系统一直是两大主流操作系统。本文将从用户界面设计、应用生态、系统定制性等方面对安卓和iOS系统进行比较分析,旨在探讨两者的优势和劣势,为用户选择合适的操作系统提供参考。
|
2月前
|
人工智能 vr&ar Android开发
探索安卓与iOS系统的发展趋势
【2月更文挑战第9天】 过去,人们对于安卓和iOS系统的争论主要集中在性能、用户体验和生态系统的比较上。然而,随着移动互联网的快速发展,两大操作系统在人工智能、物联网、安全性等方面的发展趋势也备受关注。本文将探讨安卓与iOS系统在技术发展方面的差异以及未来的发展趋势。
|
2月前
|
搜索推荐 Android开发 iOS开发
探讨安卓与iOS系统的用户体验差异
【2月更文挑战第7天】 在当今移动互联网时代,安卓和iOS系统是最受欢迎的手机操作系统。本文将从用户界面设计、应用生态系统、定制性能等方面探讨安卓与iOS系统的用户体验差异,为读者提供更深入的了解。
|
2月前
|
安全 开发工具 Android开发
安卓与iOS系统的优缺点比较
【2月更文挑战第6天】 安卓和iOS是目前市场上最流行的两种操作系统。虽然它们都拥有自己的独特之处,但它们也有一些共同之处。本文将探讨这两种操作系统的优缺点,并对它们进行比较。
|
iOS开发 内存技术 Swift
iOS - AudioServicesPlay 短频音效播放
前言 extern void AudioServicesPlayAlertSound(SystemSoundID inSystemSoundID) __OSX_AVAILABLE_STARTING(__MAC_10_5,__IPHONE_2_0); @available(iOS 2.0, *) public func AudioServicesPlayAlertSound(inSystemSoundID: SystemSoundID) 系统声音服务(System Sound Services)提供了一个接口,用于播放不超过 30 秒的声音。
922 0
|
1月前
|
API 数据安全/隐私保护 iOS开发
利用uni-app 开发的iOS app 发布到App Store全流程
利用uni-app 开发的iOS app 发布到App Store全流程
88 3
|
3月前
|
存储 iOS开发
iOS 开发,如何进行应用的本地化(Localization)?
iOS 开发,如何进行应用的本地化(Localization)?
122 2