#import <Foundation/Foundation.h> /// Apple宣稱其保留使用全部「兩字母前綴」的權利,本身選用的前綴應該是三個字母 @class EocSoundPlayer; @protocol EOCSoundPlayerDelegate <NSObject> //播放完畢回調 -(void) soundPlayerDidFinish:(EocSoundPlayer *)player; @end @interface EocSoundPlayer : NSObject @property (nonatomic , weak) id <EOCSoundPlayerDelegate> delegate; -(id) initWithURL:(NSURL *)url; -(void) playSound; @end #import "EocSoundPlayer.h" #import <AudioToolbox/AudioToolbox.h> void completion (SystemSoundID ssID,void *clientData) { EocSoundPlayer *player = (__bridge EocSoundPlayer *)clientData; if ([player.delegate respondsToSelector:@selector(soundPlayerDidFinish:)]) { [player.delegate soundPlayerDidFinish:player]; } } @implementation EocSoundPlayer { SystemSoundID _systemSoundID; } -(id)initWithURL:(NSURL *)url { if ((self = [super init])) { AudioServicesCreateSystemSoundID((__bridge CFURLRef)url, &_systemSoundID); } return self; } -(void) dealloc { AudioServicesDisposeSystemSoundID(_systemSoundID); } -(void)playSound { AudioServicesAddSystemSoundCompletion(_systemSoundID, NULL, NULL,completion,(__bridge void *)self), AudioServicesPlaySystemSound(_systemSoundID); } @end