iOS——音频封装

.h文件

#import <Foundation/Foundation.h>
#import <AVFoundation/AVFoundation.h>
#import "RadioDetailListList.h"//对应数据源的model(存放音频资源)
@interface RadioPlayManager : NSObject

@property(nonatomic,assign) BOOL isPlay;//播放状态
@property(nonatomic,assign) NSInteger playIndex;//播放歌曲索引
@property(nonatomic,copy) NSMutableArray *musicArray;//播放列表
@property(nonatomic,assign) float currentTime;//当前时间
@property(nonatomic,assign) float totalTime;//总时间
@property(nonatomic,strong) AVPlayer *avPlayer;
+ (instancetype)defaultManager;
-(void)play;  //播放
-(void)pause; //暂停
-(void)seekTotime:(float)time; //跳到指定位置播放
-(void)lastMusic;//上一首
-(void)nextMusic;//下一首
-(void)changeMusicWithIndex:(NSInteger)index;//切换歌曲
-(void)playerDidFinish;//播放完成
@end

.m文件

#import "RadioPlayManager.h"
@implementation RadioPlayManager
//单列的声明
+ (instancetype)defaultManager{
    static RadioPlayManager *manager;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[RadioPlayManager alloc]init];    
    });
    return manager;
}
//重写init方法
-(instancetype)init{
    self = [super init];
    if (self) {
        _musicArray = [NSMutableArray array];
    }
    return  self;
}

//通过播放列表数组  初始化_avPlayer(通过AVPlayerItem来初始化)
-(void)setMusicArray:(NSMutableArray *)musicArray{
    [_musicArray removeAllObjects];
    _musicArray = musicArray;
    //通过索引拿到model
    RadioDetailListList *listmodel = _musicArray[_playIndex];
    AVPlayerItem *avPlayerItem = [[AVPlayerItem alloc]initWithURL:[NSURL URLWithString:listmodel.musicUrl]];
    if (_avPlayer) {
        //切换的时候 AVPlayer要用到AVPlayerItem来切换
        [_avPlayer replaceCurrentItemWithPlayerItem:avPlayerItem];
    }
    else{
        //初始化的时候 AVPlayer要用到AVPlayerItem来初始化
        _avPlayer = [[AVPlayer alloc]initWithPlayerItem:avPlayerItem];
    }   
}
//当前时间
-(float)currentTime
{
    //  当前基数为零
    if (_avPlayer.currentItem.timebase == 0) {
        return 0;
    }
    //value/timescale = seconds.
    //当前时间的秒
    return _avPlayer.currentTime.value / _avPlayer.currentTime.timescale;   
}
//总时间
-(float)totalTime
{
    if (_avPlayer.currentItem.duration.timescale == 0) {
        return 0;
    }
    //当前item的duration 持续的时间
    return _avPlayer.currentItem.duration.value / _avPlayer
    .currentItem.duration.timescale; 
}

#pragma mark - 播放控制
//播放
-(void)play{
    [_avPlayer play];
    _isPlay = YES;
}
//关闭
-(void)pause
{
    [_avPlayer pause];
    _isPlay = NO;
}
//秒
-(void)seekTotime:(float)time
{
    [self pause];
    CMTime newtime = _avPlayer.currentTime;
    newtime.value = newtime.timescale * time;
    [_avPlayer seekToTime:newtime];
    [self play];
}


//切歌
-(void)changeMusicWithIndex:(NSInteger)index
{
    _playIndex = index;
    RadioDetailListList *listmodel = _musicArray[_playIndex];
    //AVPlayerItem 初始化 要用到一个URL
    AVPlayerItem *avPlayerItem = [[AVPlayerItem alloc]initWithURL:[NSURL URLWithString:listmodel.musicUrl]];
    [_avPlayer replaceCurrentItemWithPlayerItem:avPlayerItem];
    [self play];
}

//上一首
-(void)lastMusic
{
    _playIndex --;
    if (_playIndex<0) {
        _playIndex = _musicArray.count -1;
    }
    [self changeMusicWithIndex:_playIndex];

}

//下一首
-(void)nextMusic{
    _playIndex ++;
    _playIndex = _playIndex % _musicArray.count;
    [self changeMusicWithIndex:_playIndex];
}

//播放完毕
-(void)playerDidFinish
{
    [self nextMusic];  
}
@end
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值