static AudioPlayerView *_audioPlayerView = nil;
@implementation AudioPlayerView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
}
returnself;
}
- (void)playerWithStringUrl:(NSString *)url
{
//如果正在播放同一种广播就继续播放
if (self.player.playing && [url isEqualToString:self.palyerUrl]) {
return;
}
//如果没有正在播放同一种广播就重新开始播放
if (self.player && !self.player.playing && [url isEqualToString:self.palyerUrl]) {
return;
}
self.palyerUrl = url;
NSError * error;
//音频绝对地址
//生成音乐播放器,AVAudioPlayer
NSURL *audioURL = [NSURL fileURLWithPath:url];
AVAudioPlayer *tempPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioURL error:&error];
self.player = tempPlayer;
self.player.delegate = self;//播放器代理
self.player.meteringEnabled = YES;//计量打开
[tempPlayer release];
}
//单例
+ (AudioPlayerView *)shareAudioPlayerView
{
UIWindow *window = [UIApplicationsharedApplication].keyWindow;
if (!window){
window = [[UIApplicationsharedApplication].windowsobjectAtIndex:0];
}
@synchronized(self){//单元的写法
if (_audioPlayerView == nil) {
_audioPlayerView = [[AudioPlayerViewalloc] initWithFrame:CGRectZero];
[window addSubview:_audioPlayerView];
}
}
return_audioPlayerView;//已经生成就反应原来的
}
- (void)gotoPalying:(BOOL)isPlay
{
if (isPlay) {
[self.player prepareToPlay];
[self.player play];
}
else
{
[self.player pause];
}
}
//播放完成
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
_playerBt.selected = NO;
}