4
例如,在iOS SDK下載頁面上,有示例代碼;我正在使用計算器應用程序(iPhoneUnitTests)。我想知道是否可以輕鬆地將聲音添加到已經構建的應用程序的按鈕上。如何在沒有Cocos2D的情況下將聲音實現到現有應用程序中
例如,在iOS SDK下載頁面上,有示例代碼;我正在使用計算器應用程序(iPhoneUnitTests)。我想知道是否可以輕鬆地將聲音添加到已經構建的應用程序的按鈕上。如何在沒有Cocos2D的情況下將聲音實現到現有應用程序中
播放簡短的聲音,如按鈕聲音,其實很簡單。這是一個簡單的例子。
必須鏈接AudioToolbox.framework二進制
SoundExampleViewController.h;
#import <UIKit/UIKit.h>
#import <AudioToolbox/AudioToolbox.h>
@interface SoundExampleViewController : UIViewController{
SystemSoundID mySoundID;
}
@end
SoundExampleViewController.m:
#import "SoundExampleViewController.h"
@implementation SoundExampleViewController
- (void)viewDidLoad{
[super viewDidLoad];
NSString *path = [[NSBundle mainBundle] pathForResource:@"SoundFileName" ofType:@"wav"];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:path],&mySoundID);
}
- (void)viewDidUnload{
[super viewDidUnload];
AudioServicesDisposeSystemSoundID(mySoundID);
}
@end
然後發揮你只需撥打:AudioServicesPlaySystemSound(mySoundID);
甜,非常感謝你。非常簡單。 – 2012-01-15 04:40:15
宏偉的答案 – Fattie 2013-12-10 12:52:56
解決了我與AVAudioPlayer的內存泄漏問題,謝謝! – 2015-12-11 13:04:39