IOS 音乐播放器 (附源码)

柳胜
2023-12-01

头文件h

#import <Foundation/Foundation.h>

/*
 *   关注路线语音播放类,支持MP3文件的播放
 */

@interface LJVoiceManager : NSObject

- (instancetype)initLJVoiceManager;

//播放
- (void)playAudio;

//暂停
- (void)pauseAudio;

//停止
- (void)stopAudio;

@end

源文件M

#import "LJVoiceManager.h"

@interface LJVoiceManager ()<AVAudioPlayerDelegate>

//必须全局声明,否则没声音
@property (nonatomic, strong)AVAudioPlayer *avAudioPlayer;

@end

@implementation LJVoiceManager

#pragma mark -- dealloc
- (void)dealloc
{
    
}

- (instancetype)init
{
    self = [super init];
    if (self)
    {
        NSAssert(NO, @"请调用initLJVoiceManager方法");
    }
    return self;
}

#pragma mark -- 初始化
- (instancetype)initLJVoiceManager
{
    self = [super init];
    if (self)
    {
        [self configPlayAudio];
    }
    return self;
}

- (void)configPlayAudio
{
    //开启后台播放
    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
    
    //从budle路径下读取音频文件 lj_attention_voice.aac
    NSString *voicePathString = [[NSBundle mainBundle] pathForResource:@"lj_attention_voice" ofType:@"mp3"];
    
    if (voicePathString.length > 0)
    {
        //把音频文件转换成url格式
        NSURL *voiceUrl = [NSURL fileURLWithPath:voicePathString];
        //初始化音频类 并且添加播放文件
        self.avAudioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:voiceUrl error:nil];
        //设置代理
        self.avAudioPlayer.delegate = self;
        
        //设置初始音量大小
        self.avAudioPlayer.volume = 1;//1:声音最大
        
        //设置音乐播放次数  -1为一直循环
        //self.avAudioPlayer.numberOfLoops = -1;
        
        //预播放
        //[self.avAudioPlayer prepareToPlay];
    }
}

#pragma mark -- 播放设置
//播放
- (void)playAudio
{
    //1. 有时候会出现一直没声音的情况,这里可以打开下configPlayAudio,重新加载一遍资源文件
//    [self configPlayAudio];
    
    //2. 一般情况下,都是有声音的,就按照如下的步骤走既可以
    //如果在播放,先停止,再播放
    if ([self.avAudioPlayer isPlaying])
    {
        [self.avAudioPlayer stop];
    }
    
    if ([self.avAudioPlayer prepareToPlay])
    {
         [self.avAudioPlayer play];
    }
}

//暂停
- (void)pauseAudio
{
    [self.avAudioPlayer pause];
}

//停止
- (void)stopAudio
{
    self.avAudioPlayer.currentTime = 0;  //当前播放时间设置为0
    [self.avAudioPlayer stop];
}

#pragma mark -- AVAudioPlayerDelegate
//播放完成时调用的方法
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
    CHDebugLog(@"audioPlayerDidFinishPlaying---%hhd",flag);
    
    //如果播放完毕,则停止播放器
    if (flag)
    {
        if ([self.avAudioPlayer isPlaying])
        {
            [self.avAudioPlayer stop];
        }
    }
}

- (void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError * __nullable)error
{
    CHDebugLog(@"audioPlayerDidFinishPlaying---%@",error);
    
    if ([self.avAudioPlayer isPlaying])
    {   //如果发生错误,也停止播放
        [self.avAudioPlayer stop];
    }
}

//当程序被应用外部打断之后,重新回到应用程序的时候触发。在这里当回到此应用程序的时候,继续播放音乐。
- (void)audioPlayerEndInterruption:(AVAudioPlayer *)player
{
    [self.avAudioPlayer play];
}

@end


 类似资料: