OC-本地音频播放

#import 

@interface LilyLocalPlayer : NSObject
@property(nonatomic,copy) void(^completePlayingBlock)(void);    //播放完毕

+ (LilyLocalPlayer*)sharedAudioPlayer;//实列化

//初始化播放器
- (void)createAudioPlayerWith:(NSString *)path error:(NSError **)error;
//开始播放
- (void)playAudio;
//暂停播放
- (void)pauseAudio;
//停止播放
- (void)stopAudio;

//播放时长
- (NSTimeInterval)currentTime;
//音频时长
- (NSTimeInterval)duration;
//播放进度
- (float)percent;

//修改播放进度
- (void)playWithPercent:(float)percent;

@end
#import "LilyLocalPlayer.h"
#import 

@interface LilyLocalPlayer ()
@property(nonatomic,strong) AVAudioPlayer * audioPlayer;//播放器
@end

static LilyLocalPlayer * manager = nil;
@implementation LilyLocalPlayer
+(id)sharedAudioPlayer
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[LilyLocalPlayer alloc] init];
    });
    return manager;
}

#pragma mark - 初始化播放器
- (void)createAudioPlayerWith:(NSString *)path error:(NSError **)error
{
    //启动音频会话管理,此时会阻断后台音乐播放
    [[AVAudioSession sharedInstance] setActive:YES error:nil];
    //设置音频播放类别,表示该应用仅支持音频播放
    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
    
    NSURL * url = [NSURL fileURLWithPath:path];
    _audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:error];
    //为音频播放做好准备
    [_audioPlayer prepareToPlay];
    //设置音量
    [_audioPlayer setVolume:1.0];
    //是否循环播放
    //_audioPlayer.numberOfLoops = -1
    //设置代理
    _audioPlayer.delegate = self;
}

#pragma mark - 播放完毕
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
    if (self.completePlayingBlock) {
        self.completePlayingBlock();
    }
}

#pragma mark - 开始播放
- (void)playAudio{
    if (_audioPlayer) {
        [_audioPlayer play];
    }
}

#pragma mark - 暂停播放
- (void)pauseAudio{
    if (_audioPlayer) {
        [_audioPlayer pause];
    }
}

#pragma mark - 停止播放
- (void)stopAudio{
    if (_audioPlayer) {
        [_audioPlayer stop];
    }
}

#pragma mark - 播放时长
- (NSTimeInterval)currentTime {
    return _audioPlayer?_audioPlayer.currentTime:0;
}

#pragma mark - 音频时长
- (NSTimeInterval)duration {
    return _audioPlayer?_audioPlayer.duration:0;
}

#pragma mark - 播放进度
- (float)percent{
    float perc = 0.0;
    if (_audioPlayer && _audioPlayer.duration > 0) {
        perc = self.currentTime/self.duration;
    }
    return perc;
}

#pragma mark - 修改播放进度
- (void)playWithPercent:(float)percent{
    if (_audioPlayer && _audioPlayer.duration > 0) {
        [_audioPlayer setCurrentTime:self.duration*percent];
    }
}
@end
//初始化播放器
[[LilyLocalPlayer sharedAudioPlayer] createAudioPlayerWith:@"本地音频路径" error:nil];

//开始播放
[[LilyLocalPlayer sharedAudioPlayer] playAudio];
//暂停播放
[[LilyLocalPlayer sharedAudioPlayer] pauseAudio];
//停止播放
[[LilyLocalPlayer sharedAudioPlayer] stopAudio];

//播放时长
[[LilyLocalPlayer sharedAudioPlayer] currentTime];
//音频时长
[[LilyLocalPlayer sharedAudioPlayer] duration];
//播放进度
[[LilyLocalPlayer sharedAudioPlayer] percent];

//修改播放进度
[[LilyLocalPlayer sharedAudioPlayer] playWithPercent:0.2];

你可能感兴趣的:(OC-本地音频播放)