#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
转载于:https://my.oschina.net/u/2319073/blog/615312