因为涉及到项目的开发,所以这里只把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