AVAudioPlayer简易封装

AVAudioPlayer简易封装

 

[说明]

AVAudioPlayer简易封装,仅仅支持播放,暂停,停止,暂停时候带有渐隐效果,自己用,没有参考价值.

 

[源码]

https://github.com/YouXianMing/AVAudioPlayer-

 

一个定时器的封装类源码(该定时器可以指定运行的次数)

//
//  SpecialTimer.h
//  Music
//
//  Created by XianMingYou on 15/4/13.
//  Copyright (c) 2015年 XianMingYou. All rights reserved.
//

#import <Foundation/Foundation.h>

@class SpecialTimer;

@protocol SpecialTimerDelegate <NSObject>
@optional
- (void)specialTimer:(SpecialTimer *)specialTimer CurrentCount:(NSInteger)count;

@end

@interface SpecialTimer : NSObject

/**
 *  定时器代理
 */
@property (nonatomic, weak) id<SpecialTimerDelegate>   delegate;

/**
 *  重复执行的次数
 */
@property (nonatomic) NSInteger                        repeatTimes;

/**
 *  定时器执行的总时间
 */
@property (nonatomic) NSTimeInterval                   totalDuration;

/**
 *  激活定时器
 */
- (void)fire;

/**
 *  让定时器无效
 */
- (void)invalid;

@end


//
//  SpecialTimer.m
//  Music
//
//  Created by XianMingYou on 15/4/13.
//  Copyright (c) 2015年 XianMingYou. All rights reserved.
//

#import "SpecialTimer.h"

@interface SpecialTimer ()

@property (nonatomic)         NSInteger   count;
@property (nonatomic, strong) NSTimer    *timer;

@end

@implementation SpecialTimer

- (void)fire {
    // 参数没有配置就返回
    if (self.repeatTimes <= 0 || self.totalDuration <= 0) {
        return;
    }
    
    // 计数时间间隔
    NSTimeInterval timeInterval = self.totalDuration / self.repeatTimes;
    
    // 开启定时器
    self.timer = [NSTimer scheduledTimerWithTimeInterval:timeInterval
                                                  target:self
                                                selector:@selector(timerEvent)
                                                userInfo:nil
                                                 repeats:YES];
}

- (void)timerEvent {
    // 运行代理
    if (_delegate || [_delegate respondsToSelector:@selector(specialTimer:CurrentCount:)]) {
        [_delegate specialTimer:self CurrentCount:_count];
    }
    
    _count++;
    if (_count >= _repeatTimes) {
        _count = 0;
        [self.timer invalidate];
    }
}

- (void)invalid {
    [self.timer invalidate];
}

@end