您的位置:首页 > 移动开发 > IOS开发

音频播放

2016-07-08 15:08 351 查看
导入框架AVFoundation.framework与头文件#import<AVFoundation/AVFoundation.h>

简单使用终端转换音频格式: afconvert 提示 afconvert -hf提示可转格式 

afconvert  -f  目标格式  -d  目标格式后的数据格式  文件名

短音频播放方法封装

接口文件中定义方法

/**
 *  播放音效
 *  @param filename音效文件名
 */
+ (void)playSound:(NSString *)filename;

/**
 *  销毁音效
 *  @param filename音效文件名
 */
+ (void)disposeSound:(NSString *)filename;

实现文件实现方法

// 字典: filename作为key, soundID作为value
// 存放所有的音频ID
static NSMutableDictionary *_soundIDDict;
+ (void)initialize
{
    _soundIDDict = [NSMutableDictionarydictionary];
}

+ (void)playSound:(NSString *)filename
{
//判断不可以为nil
    if (!filename)return;
        //从字典中取出soundID
    SystemSoundID soundID = [_soundIDDict[filename]unsignedLongValue];
    if (!soundID) {//
创建
        // 加载音效文件
   
4000
    NSURL *url = [[NSBundlemainBundle]
URLForResource:filenamewithExtension:nil];
           if (!url)return;

        // 创建音效ID
        AudioServicesCreateSystemSoundID((__bridgeCFURLRef)url, &soundID);

        // 放入字典
        _soundIDDict[filename] =@(soundID);
    }
    
    // 播放
    AudioServicesPlaySystemSound(soundID);
}

+ (void)disposeSound:(NSString *)filename
{
    if (!filename)return;
    
    SystemSoundID soundID = [_soundIDDict[filename]unsignedLongValue];
    if (soundID) {
        // 销毁音效ID
        AudioServicesDisposeSystemSoundID(soundID);
        
        // 从字典中移除
        [_soundIDDictremoveObjectForKey:filename];
    }
}

长音频播放

/**

 *  播放音乐
 *
 *  @param filename
音乐文件名
 */
+ (AVAudioPlayer *)playMusic:(NSString *)filename;

/**
 *  暂停音乐
 *
 *  @param filename
音乐文件名
 */
+ (void)pauseMusic:(NSString *)filename;

/**
 *  停止音乐
 *
 *  @param filename
音乐文件名
 */
+ (void)stopMusic:(NSString *)filename;

/**
 *  返回当前正在播放的音乐播放器
 */
+ (AVAudioPlayer *)currentPlayingAudioPlayer;

实现文件

/**

 *  存放所有的音乐播放器
 *  字典: filename作为key, audioPlayer作为value
 */
static NSMutableDictionary *_audioPlayerDict;

/**
 *  初始化
 */
+ (void)initialize
{
       _audioPlayerDict = [NSMutableDictionary
dictionary];    
    // 设置音频会话类型
    AVAudioSession *session = [AVAudioSession
sharedInstance];
    [session setCategory:AVAudioSessionCategorySoloAmbient
error:nil];
    [session setActive:YES
error:nil];
}
/**
 *  播放音乐
 *
 *  @param filename
音乐文件名
 */
+ (AVAudioPlayer *)playMusic:(NSString *)filename
{
    if (!filename)
return nil;
    
    // 从字典中取出audioPlayer
    AVAudioPlayer *audioPlayer =
_audioPlayerDict[filename];
    if (!audioPlayer) {
// 创建
        // 加载音乐文件
        NSURL *url = [[NSBundle
mainBundle] URLForResource:filename
withExtension:nil];
        
        if (!url)
return nil;
        
        // 创建audioPlayer
        audioPlayer = [[AVAudioPlayer
alloc] initWithContentsOfURL:url
error:nil];
        
        // 缓冲
        [audioPlayer prepareToPlay];
        
//        audioPlayer.enableRate = YES;
//        audioPlayer.rate = 10.0;
        
        // 放入字典
        _audioPlayerDict[filename] = audioPlayer;
    }
    
    // 播放
    if (!audioPlayer.isPlaying) {
        [audioPlayer play];
    }
    
    return audioPlayer;
}

/**
 *  暂停音乐
 *
 *  @param filename
音乐文件名
 */
+ (void)pauseMusic:(NSString *)filename
{
    if (!filename)
return;
    
    // 从字典中取出audioPlayer
    AVAudioPlayer *audioPlayer =
_audioPlayerDict[filename];
    
    // 暂停
    if (audioPlayer.isPlaying) {
        [audioPlayer pause];
    }
}

/**
 *  停止音乐
 *
 *  @param filename
音乐文件名
 */
+ (void)stopMusic:(NSString *)filename
{
    if (!filename)
return;
    
    // 从字典中取出audioPlayer
    AVAudioPlayer *audioPlayer =
_audioPlayerDict[filename];
    
    // 暂停
    if (audioPlayer.isPlaying) {
        [audioPlayer stop];
        
        // 直接销毁
        [_audioPlayerDict
removeObjectForKey:filename];
    }
}

/**
 *  返回当前正在播放的音乐播放器
 */
+ (AVAudioPlayer *)currentPlayingAudioPlayer
{
    for (NSString *filename
in _audioPlayerDict) {
        AVAudioPlayer *audioPlayer =
_audioPlayerDict[filename];
        
        if (audioPlayer.isPlaying) {
            return audioPlayer;
        }
    }
    
    return
nil;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ios 音频