iOS简单的音效播放单例 (您可照搬代码)

.h

#import <Foundation/Foundation.h>


@interface MusicPlayManager : NSObject

+ (BOOL)playMusic:(NSString *)filename;//播放音乐

+ (void)pauseMusic:(NSString *)filename;//暂停播放

+ (void)stopMusic:(NSString *)filename;//停止音乐

@end


.m

#import "MusicPlayManager.h"
#import <AVFoundation/AVFoundation.h>
@interface MusicPlayManager()

@end

@implementation MusicPlayManager
/**
 *存放所有的音乐播放器
 */
static NSMutableDictionary *_musicPlayers;
+ (NSMutableDictionary *)musicPlayers{
     if (_musicPlayers==nil) {
        _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 ([filename isEqualToString:@"BGM.mp3"]) {
            [player setNumberOfLoops:-1];//设置循环播放
         }
//       player.volume = 0.1;//声音大小
     }
     //播放
     if (![player isPlaying]) {
        return [player play];//如果当前没处于播放状态,那么就播放
     }
     return YES;//正在播放
}


+ (void)pauseMusic:(NSString *)filename{
    if (!filename) return;
    AVAudioPlayer *player=[self musicPlayers][filename];
    [player pause];
}

+ (void)stopMusic:(NSString *)filename{
     if (!filename) return;
     AVAudioPlayer *player=[self musicPlayers][filename];
     [player stop];
    [[self musicPlayers] removeObjectForKey:filename];
 }


@end


注意:使用此单例不可加全局断点。


你可能感兴趣的:(iOS简单的音效播放单例 (您可照搬代码))