对FreeStreamer的单例形式封装
来源:互联网 发布:软件开发毕业设计题目 编辑:程序博客网 时间:2024/05/20 22:40
直接代码吧,注释已经加好。
.h 文件如下
//// FreeStreamerPlayer.h// DownloadList//// Created by puslar on 16/9/18.// Copyright © 2016年 puslar. All rights reserved.//#import <Foundation/Foundation.h>#import "FSAudioStream.h"@protocol PlayerDelegate <NSObject>@optional/** * 更新播放进度 * * @param currentPosition 当前位置 * @param endPosition 结束位置(总时长) */- (void)updateProgressWithCurrentPosition:(FSStreamPosition)currentPosition andEndPosition:(FSStreamPosition)endPosition;@end@interface FreeStreamerPlayer : FSAudioStream/** * 是否为播放状态 */@property (assign, nonatomic) BOOL isPlay;/** * 是否循环播放 */@property (assign, nonatomic) BOOL isLoop;/** * 播放文件地址(队列)数组 */@property (strong, nonatomic) NSMutableArray * audioArray;/** * 播放速率 */@property (assign, nonatomic) float rate;/** * 代理 用于更新播放进度 */@property (weak, nonatomic) id<PlayerDelegate> playerDelegate;/** * 获得播放器单例 * * @return 获得播放器单例 */+ (instancetype)defaultPlayer;/** * 播放文件队列中的指定文件 * * @param itemIndex 指定的文件的索引 */- (void)playItemAtIndex:(NSUInteger)itemIndex;@end
.m文件如下
//// FreeStreamerPlayer.m// DownloadList//// Created by puslar on 16/9/18.// Copyright © 2016年 puslar. All rights reserved.//#import "FreeStreamerPlayer.h"@interface FreeStreamerPlayer ()@property (assign, nonatomic) NSUInteger currentItemIndex;@property (strong, nonatomic) NSTimer * progressTimer;@end@implementation FreeStreamerPlayer/** * 获得播放器单例 * * @return 播放器单例 */+ (instancetype)defaultPlayer{ static FreeStreamerPlayer * player = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ //创建FSAudioStream对象 FSStreamConfiguration *config = [[FSStreamConfiguration alloc] init]; config.httpConnectionBufferSize *= 2; config.enableTimeAndPitchConversion = YES; player = [[super alloc] initWithConfiguration:config]; player.onFailure=^(FSAudioStreamError error,NSString *description){ NSLog(@"发生错误:%@",description); }; player.onCompletion=^(){ NSLog(@"播放完成!"); [player stop]; }; player.onStateChange=^(FSAudioStreamState state){ if (state == kFsAudioStreamPlaying) { [player setPlayRate:player.rate]; } if (state == kFsAudioStreamStopped) { if (player.isSinglePlay && player.isLoop) { [player playItemAtIndex:player.currentItemIndex]; } if (!player.isSinglePlay && player.currentItemIndex+1 < player.audioArray.count) { [player playItemAtIndex:player.currentItemIndex+1]; } if (!player.isSinglePlay && player.isLoop && player.currentItemIndex+1 >= player.audioArray.count) { [player playItemAtIndex:0]; } } }; [player setVolume:0.5]; player.progressTimer = nil; player.isSinglePlay = YES; player.isLoop = NO; player.rate = 1; }); return player;}/** * 播放 */- (void)play{ [super play]; if (!_progressTimer) { _progressTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(updateProgress) userInfo:nil repeats:YES]; } _isPlay = YES;}/** * 播放指定地址的文件 * * @param url 文件地址 */- (void)playFromURL:(NSURL *)url{ [super playFromURL:url]; if (![self.audioArray containsObject:url]) { [self.audioArray addObject:url]; } _currentItemIndex = [self.audioArray indexOfObject:url]; if (!_progressTimer) { _progressTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(updateProgress) userInfo:nil repeats:YES]; } _isPlay = YES;}/** * 从指定位置开始播放文件 * * @param offset 起始偏移量 */- (void)playFromOffset:(FSSeekByteOffset)offset{ [super playFromOffset:offset]; if (!_progressTimer) { _progressTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(updateProgress) userInfo:nil repeats:YES]; } _isPlay = YES;}/** * 播放文件队列中的指定文件 * * @param itemIndex 指定的文件的索引 */- (void)playItemAtIndex:(NSUInteger)itemIndex{ if (itemIndex < self.audioArray.count) { _currentItemIndex = itemIndex; [self playFromURL:[self.audioArray objectAtIndex:itemIndex]]; } else { NSLog(@"超出列表长度"); }}/** * 暂停 */- (void)pause{ if (_isPlay) { [super pause]; if (!_progressTimer) { _progressTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(updateProgress) userInfo:nil repeats:YES]; } }}/** * 停止 */- (void)stop{ [super stop]; [_progressTimer invalidate]; _progressTimer = nil; _isPlay = NO;}/** * 设置播放速率 * * @param rate 播放速率(0.5~2) */- (void)setRate:(float)rate{ _rate = rate; [self setPlayRate:rate];}/** * 更新播放进度 */- (void)updateProgress{ if ([_playerDelegate respondsToSelector:@selector(updateProgressWithCurrentPosition:andEndPosition:)]) { [_playerDelegate updateProgressWithCurrentPosition:self.currentTimePlayed andEndPosition:self.duration]; }}/** * 播放的文件队列 * * @return 播放的文件队列 */- (NSMutableArray *)audioArray{ if (!_audioArray) { _audioArray = [[NSMutableArray alloc] init]; } return _audioArray;}@end
1 0
- 对FreeStreamer的单例形式封装
- 单例模式的三种形式
- Double Check形式的单例模式
- swift 单例的几种形式
- 单例模式的最高级形式
- 单例的几种形式
- 单例模式的几种形式
- 单例模式的其他实现形式
- 单例的一种封装
- 单例形式模式
- AS3单例形式
- 封装的几种形式
- 使用poi Java封装对Excel内容的读取,支持合并列形式
- 单例模式的三种常用的形式
- 单例的封装创建与使用
- OKHTTP的单例和再封装
- 牛人 封装的单例模板
- Okhttp的单例模式封装
- Oracle RAC 11g 通过SCAN连接遇到ORA-12170错误的解决办法
- 2017百度前端开发
- Map集合,Queue集合
- HDU1698 Just a Hook(线段树成段替换)
- 修改系统状态栏颜色
- 对FreeStreamer的单例形式封装
- Android eclipse转studio的流程及问题
- Spark与Hive的交互
- 设计模式学习之代理模式(一)
- java8改进的HashMap和Hashtable类
- 栈
- Codeforces 627A XOR Equation
- BZOJ2393: Cirno的完美算数教室 容斥原理
- [数位DP] HDU 3555 Bomb