| | |
| | | |
| | | AVAudioPlayer *player = self.musicPlayers[playID]; //先查询对象是否缓存了 |
| | | player.delegate = self; |
| | | player.numberOfLoops = -1;//设置音乐播放次数 -1为一直循环 |
| | | |
| | | if (!player) { |
| | | |
| | | NSError *error = nil; |
| | | player = [[AVAudioPlayer alloc] initWithData:musicData error:&error]; |
| | | player.delegate = self; |
| | | player.numberOfLoops = -1;//设置音乐播放次数 -1为一直循环 |
| | | |
| | | if (![player prepareToPlay]){ |
| | | NSLog(@"播放器缓冲失败"); |
| | |
| | | |
| | | AVAudioPlayer *player=self.musicPlayers[playID]; |
| | | [player stop]; |
| | | [self.musicPlayers removeObjectForKey:playID]; |
| | | if (_blockPlayerStopPlay) { |
| | | _blockPlayerStopPlay(); |
| | | } |
| | |
| | | NSLog(@"请传入playID"); |
| | | return ; |
| | | } |
| | | |
| | | AVAudioPlayer *player = self.musicPlayers[playID]; |
| | | |
| | | [player stop]; |
| | |
| | | self.soundIDs[playID] = @(soundID); |
| | | } |
| | | |
| | | // 设置播放完成回调 |
| | | AudioServicesAddSystemSoundCompletion(soundID, NULL, NULL, soundCompleteCallback, NULL); |
| | | AudioServicesPlaySystemSound(kSystemSoundID_Vibrate); |
| | | // 播放 |
| | | AudioServicesPlaySystemSound(soundID); |
| | | } |
| | | |
| | | |
| | | //当音频播放完毕会调用这个函数 |
| | | void soundCompleteCallback(SystemSoundID soundID,void* sample) |
| | | { |
| | | AudioServicesPlaySystemSound(kSystemSoundID_Vibrate); //震动 |
| | | AudioServicesPlaySystemSound(soundID); |
| | | |
| | | /*播放全部结束,因此释放所有资源 */ |
| | | // AudioServicesDisposeSystemSoundID(sample); |
| | | // CFRelease(sample); |
| | | // CFRunLoopStop(CFRunLoopGetCurrent()); |
| | | } |
| | | |
| | | //摧毁音效 |
| | | - (void)disposeSoundWithPlayID:(NSString *)playID |