音频播放

FCAudioTool.h

//
//  FCAudioTool.h
//  AVAudioPlayer//

#import <Foundation/Foundation.h>

@interface FCAudioTool : NSObject
/**
 *  播放音乐
 *  filename:音乐名
 */
+ (BOOL)playMusic:(NSString *)filename;
/**
 *  暂停音乐
 *  filename:音乐名
 */
+ (void)pauseMusic:(NSString *)filename;
/**
 *  关闭播放器
 */
+ (void)stopMusic:(NSString *)filename;

/**
 *  播放音效
 */
+ (void)playSound:(NSString *)filename;

/**
 *  关闭音效
 */
+ (void)pauseSound:(NSString *)filename;

@end

FCAudioTool.m

//
//  FCAudioTool.m
//  AVAudioPlayer
//
//  Created by Apple on 14-8-9.
//  Copyright (c) 2014年 it.heima. All rights reserved.
//

#import "FCAudioTool.h"
#import <AVFoundation/AVFoundation.h>

@implementation FCAudioTool
/**
 *  存放所有音乐播放器
 */
static NSMutableDictionary *musicDict;
/**
 *  存放所有音效的ID
 */
static NSMutableDictionary *soundDict;

+ (NSMutableDictionary *)musicDict
{
    if (!musicDict) {
        musicDict = [NSMutableDictionary dictionary];
    }
    return musicDict;
}

+ (NSMutableDictionary *)soundDict
{
    if (!soundDict) {
        soundDict = [NSMutableDictionary dictionary];
    }
    return soundDict;
}
/**
 *  播放音乐
 *
 *  @param filename filename
 */
+ (BOOL)playMusic:(NSString *)filename
{
    // 如果没有此音乐名
    if(!filename) return NO;
    
   // 1.取出相应的音乐播放器
    AVAudioPlayer *player = [self musicDict][filename];
    // 如果没有相对应的播放器,则创建一个
    if (!player) {
        // 加载音乐路径
        NSURL *url = [[NSBundle mainBundle] URLForResource:filename withExtension:nil];
        // 创建播放器,(一个播放器只能播放一个音乐文件)
        player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
        // 缓冲音乐文件,准备播放
        BOOL isSuccess = [player prepareToPlay];
        // 如果缓冲成功,则播放,反之不播放
        if(!isSuccess) return NO;
        
        // 添加播放器
        [self musicDict][filename] = player;
    }
    
    // 如果有相应的音乐播放器,则直接播放
    // 判断音乐是否正在播放,如果正在播放,则不需要再次播放
    if (!player.isPlaying)
        [player play];
    
    return YES;  // 正在播放
}

/**
 *  暂停音乐
 */
+ (void)pauseMusic:(NSString *)filename
{
    // 如果没有此音乐名,直接退出函数
    if (!filename)  return ;
    
    // 取出相应的音乐播放器
    AVAudioPlayer *player = [self musicDict][filename];
    
    // 音乐是否正在播放
    if (player.isPlaying)
    // 暂停音乐
    [player pause];
    
}

/**
 *  关闭播放器
 */
+ (void)stopMusic:(NSString *)filename
{
    // 如果没有此音乐名,直接退出函数
    if(!filename) return ;
    // 取出相应的音乐播放器
    AVAudioPlayer *player = [self musicDict][filename];
    // 关闭音乐播放器
    [player stop];
    // 将此播放器从字典中移除
    [[self musicDict] removeObjectForKey:filename];
  
}

/**
 *  播放音效
 */
+ (void)playSound:(NSString *)filename
{
    // 如果没有此音乐名,直接退出函数
    if(!filename) return ;
    // 取出对应的音效
    SystemSoundID soundID = [[self soundDict][filename] unsignedLongValue];
    // 初始化
    if (!soundID) {
        // 音频文件的url
        NSURL *url = [[NSBundle mainBundle] URLForResource:filename withExtension:nil];
        if (!url) return ;
        AudioServicesCreateSystemSoundID((__bridge CFURLRef)(url), &soundID);
        // 存入字典
        [self soundDict][filename] = @(soundID);
        }
   
    // 播放音效
    AudioServicesPlaySystemSound(soundID);
}

/**
 *  销毁音效
 */
+ (void)disposeSound:(NSString *)filename
{
    if (!filename) return;
    // 1.取出对应的音效ID
    SystemSoundID soundID = [[self soundDict][filename] unsignedLongValue];
    // 2.销毁
    if (soundID) {
        AudioServicesDisposeSystemSoundID(soundID);
        [[self soundDict] removeObjectForKey:filename];
    }
}

@end
原文地址:https://www.cnblogs.com/Fc-ios/p/3902583.html