美文网首页
AVAudioPlayer播放单音效、组合音效

AVAudioPlayer播放单音效、组合音效

作者: biny_ios | 来源:发表于2018-08-27 10:45 被阅读21次

    因为音效均为几秒的音频,首先考虑了AudioToolbox,但是发现戴耳机会出现音量太小的问题。每次都需要重新再改变音量控制键才可以。因此,舍弃了AudioToolbox的音效播放方式。
    以下为AVAudioPlayer播放音效:
    代码取自https://github.com/owlcoding/SimpleAudioPlayer
    在多组合音效做了简易修改:
    下面为代码.h 部分

    #import <Foundation/Foundation.h>
    #import <AudioToolbox/AudioToolbox.h>
    #import <AVFoundation/AVFoundation.h>
    
    typedef void(^CompletionBlock)(BOOL);
    
    @interface AVAudioPlayerWithCompletionBlock : AVAudioPlayer
    
    @property (nonatomic, copy) CompletionBlock completionBlock;
    @end
    
    @interface HBAudioService : NSObject<AVAudioPlayerDelegate>{
        SystemSoundID soundID;
        int audioCount;
        NSURL *url;
        NSMutableSet *players;
    }
    
    @property (nonatomic, strong) AVAudioPlayer *player;
    @property (nonatomic, strong) NSArray *audioFile;
    @property (nonatomic,assign) NSInteger currentIndex;
    @property(nonatomic,assign)SystemSoundID audioSoundID;//播放文件标识
    
    + (HBAudioService *)shared;
    
    /*
     The method
     + (AVAudioPlayer *) playFile:(NSString *)name volume:(CGFloat)vol loops:(NSInteger)loops withCompletionBlock:(CompletionBlock)completion;
     
     the loops parameter works like this:
     any negative number - sound keeps playing in a loop over and over
     0, 1 - sound is played once
     2, 3, etc - sound is played twice, 3 times, etc-times :)
     */
    + (AVAudioPlayer *) playFile:(NSString *)name volume:(CGFloat)vol loops:(NSInteger)loops withCompletionBlock:(CompletionBlock)completion;
    
    /*
     The methods below just call the
     playFile: volume: loops: withCompletionBlock:
     */
    + (AVAudioPlayer *)playFile:(NSString *)name;
    + (AVAudioPlayer *)playFile:(NSString *)name volume:(CGFloat)vol loops:(NSInteger)loops;
    + (AVAudioPlayer *) playFile:(NSString *)name withCompletionBlock:(CompletionBlock)completion ;
    
    + (AVAudioPlayer *) playLoopedFile:(NSString *) name;
    
    + (void)stopPlayer:(AVAudioPlayer *)player;
    + (void)stopAllPlayers;
    + (void) playFiles:(NSArray *) filesList withCompletionBlock:(CompletionBlock) completion;
    
    @end
    

    下面为.m部分

    #import "HBAudioService.h"
    @implementation AVAudioPlayerWithCompletionBlock
    
    @end
    
    static HBAudioService *_manger = nil;
    @implementation HBAudioService
    
    static HBAudioService *sharedInstance = nil;
    
    + (void)initialize
    {
        if (sharedInstance == nil)
            sharedInstance = [[self alloc] init];
    }
    
    + (HBAudioService *)shared
    {
        //Already set by +initialize.
        return sharedInstance;
    }
    
    + (id)allocWithZone:(NSZone*)zone
    {
        //Usually already set by +initialize.
        @synchronized(self) {
            if (sharedInstance) {
                //The caller expects to receive a new object, so implicitly retain it
                //to balance out the eventual release message.
                return sharedInstance;
            } else {
                //When not already set, +initialize is our caller.
                //It's creating the shared instance, let this go through.
                return [super allocWithZone:zone];
            }
        }
    }
    
    - (id)init
    {
        //If sharedInstance is nil, +initialize is our caller, so initialize the instance.
        //If it is not nil, simply return the instance without re-initializing it.
        if (sharedInstance == nil) {
            self = [super init];
            if (self) {
                //Initialize the instance here.
                players = [NSMutableSet setWithCapacity:1];
                self.currentIndex = 0;
            }
        }
        return self;
    }
    
    
    - (AVAudioPlayer *) playFile:(NSString *)name volume:(CGFloat)vol loops:(NSInteger)loops withCompletionBlock:(CompletionBlock)completion
    {
        NSString *filePath = [[NSBundle mainBundle] pathForResource:name ofType:nil];
        if(!filePath) {
            return nil;
        }
       
        NSError *error = nil;
        NSURL *fileURL = [NSURL fileURLWithPath:filePath isDirectory:NO];
        AVAudioPlayerWithCompletionBlock *player = [[AVAudioPlayerWithCompletionBlock alloc] initWithContentsOfURL:fileURL error:&error];
        player.volume = vol;
        player.numberOfLoops = loops;
        // Retain and play
        if(player) {
            [players addObject:player];
            player.delegate = self;
            player.completionBlock = completion;
            [player play];
            return player;
        }
        return nil;
        
    }
    
    
    - (void) playFiles:(NSArray*) filesList withCompletionBlock:(CompletionBlock) completion
    {
        if (self.currentIndex < filesList.count) {
            [self playFile:filesList[self.currentIndex] withCompletionBlock:^(BOOL completed) {
                if (self.currentIndex < filesList.count) {
                    self.currentIndex++;
                    [self playFiles:filesList withCompletionBlock:completion];
                }else {
                    if (completion) {
                        completion(YES);
                    }
                }
            }];
        }
    }
    
    
    - (AVAudioPlayer *)playFile:(NSString *)name {
        
        return [self playFile:name volume:1.0f loops:0 withCompletionBlock:nil];
    }
    - (AVAudioPlayer *) playLoopedFile:(NSString *) name {
        return [self playFile:name volume:1.0f loops:-1];
    }
    - (AVAudioPlayer *) playFile:(NSString *)name withCompletionBlock:(CompletionBlock)completion
    {
        return [self playFile:name volume:1.0f loops:0 withCompletionBlock:completion];
    }
    
    - (AVAudioPlayer *)playFile:(NSString *)name volume:(CGFloat)vol loops:(NSInteger)loops {
        
        return [self playFile:name volume:vol loops:loops withCompletionBlock:nil];
    }
    
    
    - (void)stopPlayer:(AVAudioPlayer *)player {
        if([players containsObject:player]) {
            player.delegate = nil;
            [players removeObject:player];
            [player stop];
        }
    }
    
    - (void)stopAllPlayers {
        NSSet *pls = [NSSet setWithSet:players];
        for (AVAudioPlayer *p in pls) {
            [self stopPlayer:p];
        }
    }
    
    - (void)audioPlayerDidFinishPlaying:(AVAudioPlayerWithCompletionBlock *)player successfully:(BOOL)completed {
        
        if (player.completionBlock) {
            player.completionBlock ( completed );
        }
        player.delegate = nil;
        [players removeObject:player];
    }
    
    - (void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError *)error {
        //TRC_DBG(@"audioPlayerDecodeErrorDidOccur %@", error);
        player.delegate = nil;
        [players removeObject:player];
    }
    
    + (AVAudioPlayer *)playFile:(NSString *)name {
        return [[HBAudioService shared] playFile:name];
    }
    
    + (AVAudioPlayer *)playFile:(NSString *)name volume:(CGFloat)vol loops:(NSInteger)loops {
        return [[HBAudioService shared] playFile:name volume:vol loops:loops];
    }
    + (AVAudioPlayer *) playFile:(NSString *)name withCompletionBlock:(CompletionBlock)completion
    {
        return [[HBAudioService shared] playFile:name withCompletionBlock:completion];
    }
    + (AVAudioPlayer *) playFile:(NSString *)name volume:(CGFloat)vol loops:(NSInteger)loops withCompletionBlock:(CompletionBlock)completion
    {
        return [[HBAudioService shared] playFile:name volume:vol loops:loops withCompletionBlock:completion];
    }
    + (AVAudioPlayer *) playLoopedFile:(NSString *) name
    {
        return [[HBAudioService shared] playLoopedFile:name];
    }
    + (void)stopPlayer:(AVAudioPlayer *)player {
        return [[HBAudioService shared] stopPlayer:player];
    }
    + (void)stopAllPlayers {
        return [[HBAudioService shared] stopAllPlayers];
    }
    + (void) playFiles:(NSArray *)filesList withCompletionBlock:(CompletionBlock)completion
    {
        [[HBAudioService shared] playFiles:filesList withCompletionBlock:completion];
    }
    

    相关文章

      网友评论

          本文标题:AVAudioPlayer播放单音效、组合音效

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