美文网首页
iOS开发播放音效和音乐类封装

iOS开发播放音效和音乐类封装

作者: Flynn_Lee | 来源:发表于2021-03-09 09:24 被阅读0次

    1、创建一个类继承于NSObject,如:soundPlay。
    2、.h文件代码

    #import <Foundation/Foundation.h>
    @interface soundPlay : NSObject
    ///播放本地音效
    +(void)playWithLocalSoundName:(NSString *)soundName;
    ///销毁音效id
    +(void)disposeWithLocalSoundName:(NSString *)soundName;
    
    ///播放音乐
    +(void)playWithMusicName:(NSString *)musicName;
    ///暂停音乐
    +(void)pauseWithMusicName:(NSString *)musicName;
    ///停止音乐
    +(void)stopWithMusicName:(NSString *)musicName;
    @end
    

    3、.m文件代码

    #import "soundPlay.h"
    #import <AVFoundation/AVFoundation.h>
    @interface soundPlay()
    @end
    @implementation soundPlay
    static NSMutableDictionary *_soundDict;
    
    static NSMutableDictionary *_playerDict;
    
    +(NSMutableDictionary *)soundDict
    {
        if (!_soundDict) {
            _soundDict = [NSMutableDictionary dictionary];
        }
        return _soundDict;
    }
    
    +(NSMutableDictionary *)playerDict
    {
        if (!_playerDict) {
            _playerDict = [NSMutableDictionary dictionary];
        }
        return _playerDict;
    }
    
    +(void)playWithLocalSoundName:(NSString *)soundName
    {
        if (soundName == nil) return;
        
        //从数组中取得soundId
        SystemSoundID soundId = [[self soundDict][soundName] unsignedIntValue];
        
        //如果数组中soundId不存在,就要从新创建然后添加到数组里
        if (!soundId) {
            
            //获取工程中播放音效的路径
            NSURL *url = [[NSBundle mainBundle]URLForResource:soundName withExtension:nil];
            
            //如果该路径不存在,返回
            if (!url) return;
            
            //创建soundId
            AudioServicesCreateSystemSoundID((__bridge CFURLRef _Nonnull)(url), &soundId);//创建soundId
            
            //将soundId存储到数组中
            [self soundDict][soundName] = @(soundId);
        }
        
        //播放音效
        AudioServicesPlaySystemSound(soundId);
    //    AudioServicesPlayAlertSound(soundId)//带振动效果
    }
    
    +(void)disposeWithLocalSoundName:(NSString *)soundName
    {
        if (soundName == nil) return;
        
        //从数组中取得soundId
        SystemSoundID soundId = [[self soundDict][soundName] unsignedIntValue];
        
        //如果soundId存在就销毁
        if (soundId) {
            
            //销毁音效id
            AudioServicesDisposeSystemSoundID(soundId);
            
            //从数组中移除该文件
            [[self soundDict]removeObjectForKey:soundName];
        }
    }
    
    ///播放音乐
    +(void)playWithMusicName:(NSString *)musicName
    {
        AVAudioPlayer *player = nil;
        
        if (musicName == nil) return;
        
        player = [self playerDict][musicName];
        
        if (!player) {
            NSURL *url = [[NSBundle mainBundle]URLForResource:musicName withExtension:nil];
            if (!url) return;
            player = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:nil];
            [player prepareToPlay];
            [[self playerDict] setValue:player forKey:musicName];
        }
        [player play];
    }
    
    ///暂停音乐
    +(void)pauseWithMusicName:(NSString *)musicName
    {
        AVAudioPlayer *player = [self playerDict][musicName];
        if (player) {
            
            [player pause];
        }
    }
    
    ///停止音乐
    +(void)stopWithMusicName:(NSString *)musicName
    {
        AVAudioPlayer *player = [self playerDict][musicName];
        if (player) {
            [player stop];
            [[self playerDict]removeObjectForKey:musicName];
            player = nil;
        }
    }
    @end
    

    4、调用,在需要使用的控制器导入头文件,#import "soundPlay.h"

    ///m_16.wav该文件需要事先导入工程,如下图
    //播放音效
    [soundPlay playWithLocalSoundName:@"m_16.wav"];
    //内存警告
    -(void)didReceiveMemoryWarning
    {//移除音效
        [soundPlay disposeWithLocalSoundName:@"buyao.wav"];
    }
    
    - (IBAction)play:(id)sender {
        
        [soundPlay playWithMusicName:@"music.mp3"];
        
    }
    - (IBAction)pause:(id)sender {
        
        [soundPlay pauseWithMusicName:@"music.mp3"];
    }
    - (IBAction)stop:(id)sender {
        
        [soundPlay stopWithMusicName:@"music.mp3"];
    }
    
    image.png

    相关文章

      网友评论

          本文标题:iOS开发播放音效和音乐类封装

          本文链接:https://www.haomeiwen.com/subject/dryvqltx.html