iOS开发之创建音频播放的单例类

因为涉及到项目的开发,所以这里只把PlayerCenter单例类中的代码进行展示,仅做参考,其中包含上一曲,暂停/播放,下一曲等功能,项目中也涉及到了后台播放和操作的功能,具体的使用大家可以继续研究,或者给我留言,等有空写一个Demo给大家分享

PlayCenter.h

#import <Foundation/Foundation.h>
#import <AVFoundation/AVFoundation.h>
#import <MediaPlayer/MediaPlayer.h>
#import <AudioToolbox/AudioToolbox.h>
#import "define.h"

@interface PlayCenter : NSObject
@property (nonatomic,strong) AVAudioPlayer *player;//音频对象
@property (nonatomic,strong) NSDictionary *musicInfo;//音频信息
@property(nonatomic,strong)NSArray *musicArray;//音频数组
@property(nonatomic,assign)NSInteger allNum;//音乐数量
@property(nonatomic,assign)NSInteger currentInt;//当前第几个音乐
@property(nonatomic,copy)NSString *currentUrlPathStr;//当前音频地址

//创建播放单例类
+ (PlayCenter *)shareCenter;
//记录音频播放的数组,并且根据点击的音频找到本地url地址,进行音乐播放
//- (NSDictionary *)play:(NSURL *)url;
- (NSDictionary *)play:(NSArray *)musicArray clickMusicPath:(NSDictionary *)urlDic;
//播放音频
- (void)play;
//暂停音频
- (void)pause;
//上一首
- (void)forwardItem;
//下一首
- (void)nextItem;
@end

PlayCenter.m

#import "PlayCenter.h"

@implementation PlayCenter
static PlayCenter *center = nil;
+ (PlayCenter *)shareCenter
{
    if (!center) {
        center = [PlayCenter new];
    }
    return center;
    
}
- (id)init{
    if (self = [super init]) {
        //后台播放音频设置
        AVAudioSession *session = [AVAudioSession sharedInstance];
        [session setCategory:AVAudioSessionCategoryPlayback error:nil];
        [session setActive:YES error:nil];
        
        //让app支持接受远程控制事件
        [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
        
        self.currentUrlPathStr = @"1";
        
    }
    return self;
}


- (NSDictionary *)play:(NSArray *)musicArray clickMusicPath:(NSDictionary *)urlDic{
    
    self.musicArray = [NSArray arrayWithArray:musicArray];
    self.allNum = musicArray.count;
    
    self.musicInfo = urlDic;
    
    for (int i = 0; i < self.allNum; i++) {
        NSDictionary *dic = self.musicArray[i];
        if ([dic[@"filename"] isEqualToString:urlDic[@"filename"]]) {
            self.currentInt = i;
        }
    }
    
    NSString *path1 = [NSString stringWithFormat:@"%@/%@",[[NSUserDefaults standardUserDefaults]objectForKey:MP3URLPATH],urlDic[@"filename"]];
    //判断当前的当前音频是否一致
    if (![path1 isEqualToString:self.currentUrlPathStr]) {
        //如果不一致,音频对象滞空
        //播放之前先滞空
        [self.player stop];
        self.player = nil;
        
        self.currentUrlPathStr = path1;
        
        //NSLog(@"。。。。。。/.....地址%@",path1);
    }
    
    NSURL *url = [NSURL fileURLWithPath:path1];
    
    //如果音频已经播放,不再重新播放
    if (self.player.playing) {
        return urlDic;
    }
    
    UIBackgroundTaskIdentifier bgTask = 0;
    //判断是否有音频对象,没有创建
    if (_player == nil) {
        _player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
    }
    
    if([UIApplication sharedApplication].applicationState== UIApplicationStateBackground) {
        
        //NSLog(@"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx后台播放");
        
        [_player play];
        
        UIApplication *app = [UIApplication sharedApplication];
        UIBackgroundTaskIdentifier newTask = [app beginBackgroundTaskWithExpirationHandler:nil];
        
        if(bgTask!= UIBackgroundTaskInvalid) {
            
            [app endBackgroundTask: bgTask];
            
        }
        bgTask = newTask;
        
    }else{
        
        //NSLog(@"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx前台播放");
        
        [_player prepareToPlay];
        [_player setVolume:1.0];
        _player.numberOfLoops = 1; //设置音乐播放次数  -1为一直循环
        [_player play]; //播放
        
        //NSLog(@"音频正在播放。。。。。。。");
        
    }
    return urlDic;
    
}

- (void)forwardItem
{
    //根据当前播放的是第几个,进行下一个播放
    _currentInt--;
    if (_currentInt < 0) {
        _currentInt = self.allNum-1;
    }
    
    //这个地方获取到了本地音频的地址,所以可以直接播放上一首
    NSDictionary *dic = self.musicArray[_currentInt];
    
    //播放上一首之前先停止,然后注销
    //[self.player stop];
    //self.player = nil;
    [self play:_musicArray clickMusicPath:dic];
    
}
- (void)nextItem
{
    //根据当前播放的是第几个,进行下一个播放
    _currentInt++;
    if (_currentInt > self.allNum-1) {
        _currentInt = 0;
    }
    
    //这个地方获取到了本地音频的地址,所以可以直接播放上一首
    NSDictionary *dic = self.musicArray[_currentInt];
    
    //播放上一首之前先停止,然后注销
    //[self.player stop];
    //self.player = nil;
    [self play:_musicArray clickMusicPath:dic];
}
- (void)play
{
    if (self.player.playing) {
        return;
    }
    [self.player play];
}
- (void)pause
{
    [self.player pause];
}

单例类源码下载:https://github.com/hbblzjy/PlayCenterModel







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hbblzjy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值