IOS开发基础之音频工具类封装AVAudioPlayer

IOS开发基础之音频工具类封装AVAudioPlayer

源码在我的主页下面 ,项目名称是AVAudioPlayer
IOS开发基础之音频工具类封装AVAudioPlayer_第1张图片
关键性代码
工具类的封装

//
//  LJAudioTool.h
//  AVAudioPlayer
//
//  Created by 鲁军 on 2021/4/23.
//

#import 

NS_ASSUME_NONNULL_BEGIN

@interface LJAudioTool : NSObject
+(BOOL)playMusic:(NSString*)filename;
+(void)pauseMusic:(NSString*)filename;
+(void)stopMusic:(NSString*)filename;
+(void)playSound:(NSString*)filename;
+(void)disposeSound:(NSString*)filename;
@end

NS_ASSUME_NONNULL_END
//
//  LJAudioTool.m
//  AVAudioPlayer
//
//  Created by 鲁军 on 2021/4/23.
//
#import "LJAudioTool.h"
#import 
@implementation LJAudioTool
static NSMutableDictionary *_soundIDs;
+(NSMutableDictionary *)soundIDs{
     
    if(!_soundIDs){
     
        _soundIDs = [NSMutableDictionary dictionary];
    }
    return _soundIDs;
}
+(void)playSound:(NSString*)filename{
     
    if(!filename) return;
    SystemSoundID soundID = [[self soundIDs][filename] unsignedIntValue];
    if(!soundID){
     
        NSURL *url = [[NSBundle mainBundle] URLForResource:filename withExtension:nil];
        if(!url) return;
        OSStatus status=AudioServicesCreateSystemSoundID((__bridge CFURLRef _Nonnull)(url), &soundID);
        NSLog(@"%d",status);  //0 代表成功
        [self soundIDs][filename] = @(soundID);
    }
    //播放
    AudioServicesPlaySystemSound(soundID);
}

+(void)disposeSound:(NSString*)filename{
     
    if(!filename) return;
    SystemSoundID soundID = [[self soundIDs][filename] unsignedIntValue];
    if(soundID){
     
        AudioServicesDisposeSystemSoundID(soundID);
        [[self soundIDs] removeObjectForKey:filename];
    }
}
static NSMutableDictionary *_musicPlayers;
+(NSMutableDictionary *)musicPlayers{
     
    if(!_musicPlayers){
     
        _musicPlayers = [NSMutableDictionary dictionary];
    }
    return _musicPlayers;
}
+ (BOOL)playMusic:(NSString *)filename{
     
    if(!filename) return NO;
    AVAudioPlayer *player = [self musicPlayers][filename];
    if(!player){
     
        NSURL *url = [[NSBundle mainBundle] URLForResource:filename withExtension:nil];
        if(!url) return NO;
        player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
        if(![player prepareToPlay]) return NO;
        [self musicPlayers][filename]=player;
    }
    if(!player.isPlaying){
     
        return [player play];
    }
    return YES;
}
+ (void)pauseMusic:(NSString *)filename{
     
    if(!filename) return;
    AVAudioPlayer * player = [self musicPlayers][filename];
    if(player.isPlaying){
     
        [player pause];
    }
}
+ (void)stopMusic:(NSString *)filename{
     
    if(!filename) return;
    AVAudioPlayer * player = [self musicPlayers][filename];
    [player stop];
    [[self musicPlayers] removeObjectForKey:filename];
}
@end

//
//  ViewController.m
//  AVAudioPlayer
//
//  Created by 鲁军 on 2021/4/23.
//
#import "ViewController.h"
#import "LJAudioTool.h"
@interface ViewController ()
@property(nonatomic,strong)NSArray *songs;
@property(nonatomic,assign)int currentIndex;
@end
@implementation ViewController
- (NSArray *)songs{
     
    if(!_songs){
     
        self.songs = @[@"309769.mp3",@"235319.mp3",@"120125029.mp3"];
    }
    return _songs;
}
- (IBAction)play:(id)sender {
     
//    [LJAudioTool playMusic:self.songs[self.currentIndex]];
    [LJAudioTool playSound:@"buyao.wav"];
}
- (IBAction)pause:(id)sender {
     
   [LJAudioTool pauseMusic:self.songs[self.currentIndex]];
}
- (IBAction)stop:(id)sender {
     
   [LJAudioTool stopMusic:self.songs[self.currentIndex]];
}
- (IBAction)next:(id)sender {
     
//    [LJAudioTool stopMusic:self.songs[self.currentIndex]];
    [self stop:nil];
    self.currentIndex++;
    if(self.currentIndex>=self.songs.count){
     
        self.currentIndex = 0;
    }
    [self play:nil];
//    [LJAudioTool playMusic:self.songs[self.currentIndex]];
}
- (void)viewDidLoad {
     
    [super viewDidLoad];
}
@end

你可能感兴趣的:(IOS)