美文网首页iOS
iOS中的音频

iOS中的音频

作者: 阿凡提说AI | 来源:发表于2017-06-21 14:49 被阅读0次

一、简介

1.音频可以分为2种
(1)音效
又称“短音频”,通常在程序中的播放时长为1~2秒
在应用程序中起到点缀效果,提升整体用户体验
(2)音乐
比如游戏中的“背景音乐”,一般播放时间较

2.播放音频可以使用框架
AVFoundation.framework

二、音效

1.音效的播放

// 1.获得音效文件的路径
NSURL *url = [[NSBundle mainBundle] URLForResource:@"m_03.wav" withExtension:nil];

// 2.加载音效文件,得到对应的音效ID
SystemSoundID soundID = 0;
AudioServicesCreateSystemSoundID((__bridge CFURLRef)(url), &soundID);

// 3.播放音效
AudioServicesPlaySystemSound(soundID);

音效文件只需要加载1次
2.音效播放常见函数总结

音效播放常见函数总结
加载音效文件
AudioServicesCreateSystemSoundID(CFURLRef inFileURL, SystemSoundID *outSystemSoundID)

释放音效资源
AudioServicesDisposeSystemSoundID(SystemSoundID inSystemSoundID)

播放音效
AudioServicesPlaySystemSound(SystemSoundID inSystemSoundID)

播放音效带点震动
AudioServicesPlayAlertSound(SystemSoundID inSystemSoundID)

3.音效格式

屏幕快照 2017-06-22 下午2.35.22.png

注意:硬件解码器一次只能对一个音频文件解码。在实际应用中通常使用非压缩的音频格式(AIFF)或者CAF音频格式,从而减低系统在音频解码上的消耗,达到省电的目的
4.声音和音效小结——音频转换工具

转换aiff格式
afconvert -f AIFF -d I8 filename

转换caf格式
afconvert -f caff -d aac -b 32000 filename

批量转换
find . -name '*.mp3' -exec afconvert -f caff -d aac -b 32000 {} \;

5.音效播放工具类

#import <Foundation/Foundation.h>

@interface AudioTool : NSObject

+ (void)playSoundWithSoundname:(NSString *)soundname;

@end


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

@implementation XMGAudioTool

static NSMutableDictionary *_soundIDs;

+ (void)initialize
{
    _soundIDs = [NSMutableDictionary dictionary];
}

//+ (NSMutableDictionary *)soundIDs
//{
//    if (_soundIDs == nil) {
//        _soundIDs = [NSMutableDictionary dictionary];
//    }
//    
//    return _soundIDs;
//}

+ (void)playSoundWithSoundname:(NSString *)soundname
{
    // 1.定义SystemSoundID
    SystemSoundID soundID = 0;
    
    // 2.从字典中取出对应soundID,如果取出是nil,表示之前没有存放在字典
    soundID = [_soundIDs[soundname] unsignedIntValue];
    if (soundID == 0) {
        CFURLRef url = (__bridge CFURLRef)[[NSBundle mainBundle] URLForResource:soundname withExtension:nil];
        AudioServicesCreateSystemSoundID(url, &soundID);
        
        // 将soundID存入字典
        [_soundIDs setObject:@(soundID) forKey:soundname];
    }
    
    // 3.播放音效
    AudioServicesPlaySystemSound(soundID);
}

@end


三、音乐

1>音乐播放用到一个叫做AVAudioPlayer的类,AVAudioPlayer只能播放本地的音频文件
AVAudioPlayer常用方法
(1)加载音乐文件

- (id)initWithContentsOfURL:(NSURL *)url error:(NSError **)outError;
- (id)initWithData:(NSData *)data error:(NSError **)outError;

(2)准备播放(缓冲,提高播放的流畅性)

- (BOOL)prepareToPlay;

(3)播放(异步播放)

- (BOOL)play;

(4)暂停

- (void)pause;

(5)停止

- (void)stop;

(6)是否正在播放

@property(readonly, getter=isPlaying) BOOL playing;

(7)时长

@property(readonly) NSTimeInterval duration;

(8)当前的播放位置

@property NSTimeInterval currentTime;

(9)播放次数(-1代表无限循环播放,其他代表播放numberOfLoops+1次)

@property NSInteger numberOfLoops;

(10)音量

@property float volume;

(11)是否允许更改速率

@property BOOL enableRate;

(12)播放速率(1是正常速率,0.5是一般速率,2是双倍速率)

@property float rate;

(13)有多少个声道

@property(readonly) NSUInteger numberOfChannels;

(14)声道(-1是左声道,1是右声道,0是中间)

@property float pan;

(15)是否允许测量音量

@property(getter=isMeteringEnabled) BOOL meteringEnabled;

(16)更新测量值

- (void)updateMeters;

(17)获得当前的平均音量

- (float)averagePowerForChannel:(NSUInteger)channelNumber;

音效、音乐工具类

#import <Foundation/Foundation.h>

@interface AudioTool : NSObject

#pragma mark - 播放音乐
// 播放音乐 musicName : 音乐的名称
+ (void)playMusicWithMusicName:(NSString *)musicName;
// 暂停音乐 musicName : 音乐的名称
+ (void)pauseMusicWithMusicName:(NSString *)musicName;
// 停止音乐 musicName : 音乐的名称
+ (void)stopMusicWithMusicName:(NSString *)musicName;

#pragma mark - 音效播放
// 播放声音文件soundName : 音效文件的名称
+ (void)playSoundWithSoundname:(NSString *)soundname;

@end


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

@implementation XMGAudioTool

static NSMutableDictionary *_soundIDs;
static NSMutableDictionary *_players;

+ (void)initialize
{
    _soundIDs = [NSMutableDictionary dictionary];
    _players = [NSMutableDictionary dictionary];
}

+ (void)playMusicWithMusicName:(NSString *)musicName
{
    assert(musicName);
    
    // 1.定义播放器
    AVAudioPlayer *player = nil;
    
    // 2.从字典中取player,如果取出出来是空,则对应创建对应的播放器
    player = _players[musicName];
    if (player == nil) {
        // 2.1.获取对应音乐资源
        NSURL *fileUrl = [[NSBundle mainBundle] URLForResource:musicName withExtension:nil];
        
        if (fileUrl == nil) return;
        
        // 2.2.创建对应的播放器
        player = [[AVAudioPlayer alloc] initWithContentsOfURL:fileUrl error:nil];
        
        // 2.3.将player存入字典中
        [_players setObject:player forKey:musicName];
        
        // 2.4.准备播放
        [player prepareToPlay];
    }
    
    // 3.播放音乐
    [player play];
}

+ (void)pauseMusicWithMusicName:(NSString *)musicName
{
    assert(musicName);
    
    // 1.取出对应的播放
    AVAudioPlayer *player = _players[musicName];
    
    // 2.判断player是否nil
    if (player) {
        [player pause];
    }
}

+ (void)stopMusicWithMusicName:(NSString *)musicName
{
    assert(musicName);
    
    // 1.取出对应的播放
    AVAudioPlayer *player = _players[musicName];
    
    // 2.判断player是否nil
    if (player) {
        [player stop];
        [_players removeObjectForKey:musicName];
        player = nil;
    }
}

#pragma mark - 音效的播放
+ (void)playSoundWithSoundname:(NSString *)soundname
{
    // 1.定义SystemSoundID
    SystemSoundID soundID = 0;
    
    // 2.从字典中取出对应soundID,如果取出是nil,表示之前没有存放在字典
    soundID = [_soundIDs[soundname] unsignedIntValue];
    if (soundID == 0) {
        CFURLRef url = (__bridge CFURLRef)[[NSBundle mainBundle] URLForResource:soundname withExtension:nil];
        
        if (url == NULL) return;
        
        AudioServicesCreateSystemSoundID(url, &soundID);
        
        // 将soundID存入字典
        [_soundIDs setObject:@(soundID) forKey:soundname];
    }
    
    // 3.播放音效
    AudioServicesPlaySystemSound(soundID);
}

@end

2> AVPlayer
能播放本地、远程的音频、视频文件
基于Layer显示,得自己去编写控制面板

3> MPMoviePlayerController
能播放本地、远程的音频、视频文件
自带播放控制面板(暂停、播放、播放进度、是否要全屏)

4> MPMoviePlayerViewController
能播放本地、远程的音频、视频文件
内部是封装了MPMoviePlayerController
播放界面默认就是全屏的
如果播放功能比较简单,仅仅是简单地播放远程、本地的视频文件,建议用这个

5> DOUAudioStreamer
能播放远程、本地的音频文件
监听缓冲进度、下载速度、下载进度

相关文章

网友评论

    本文标题:iOS中的音频

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