2012-01-15 37 views

回答

5

播放簡短的聲音,如按鈕聲音,其實很簡單。這是一個簡單的例子。

必須鏈接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);

+0

甜,非常感謝你。非常簡單。 – 2012-01-15 04:40:15

+0

宏偉的答案 – Fattie 2013-12-10 12:52:56

+0

解決了我與AVAudioPlayer的內存泄漏問題,謝謝! – 2015-12-11 13:04:39

相關問題