征服iOS音频AVPlayer

iOS开发-音频播放

2015-08-20  本文已影响7011人  superWX

iOS--音频的简单使用

1:AVAudioRecorder的简单使用

录音

// 创建录音配置信息的字典
NSMutableDictionary *setting = [NSMutableDictionary dictionary];

// 音频格式
setting[AVFormatIDKey] = @(kAudioFormatAppleIMA4);

// 录音采样率(Hz) 如:AVSampleRateKey==8000/44100/96000(影响音频的质量)
setting[AVSampleRateKey] = @(8000.0);

// 音频通道数 1 或 2
setting[AVNumberOfChannelsKey] = @(1);

// 线性音频的位深度 8、16、24、32
setting[AVLinearPCMBitDepthKey] = @(8);

//录音的质量
setting[AVEncoderAudioQualityKey] = [NSNumber numberWithInt:AVAudioQualityHigh];

 // 1.创建存放录音文件的地址
        NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
        NSString *filePath = [path stringByAppendingPathComponent:@"321.caf"];
        NSURL *url = [NSURL URLWithString:filePath];
        
        // 2.创建录音对象
        self.recoder = [[AVAudioRecorder alloc] initWithURL:url settings:nil error:nil];
        
        // 3.准备录音
        [self.recoder prepareToRecord];    
         
// 开始录音
    [self.recoder record];
// 暂停录音
    [self.recoder stop];
    

播放音效

    // 1.创建SystemSoundID,根据音效文件来生成
    SystemSoundID soundID = 0;
    
    // 2.根据音效文件,来生成SystemSoundID
    NSURL *url = [[NSBundle mainBundle] URLForResource:@"win.aac" withExtension:nil];
    CFURLRef urlRef = (__bridge CFURLRef)(url);
    AudioServicesCreateSystemSoundID(urlRef, &soundID);
    
    // 播放音效
    // AudioServicesPlaySystemSound(soundID); 
    
    //有震动效果
     AudioServicesPlayAlertSound(self.soundID);  

优化

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

播放音乐

    // 取出资源的URL
    NSURL *url = [[NSBundle mainBundle] URLForResource:@"1201111234.mp3" withExtension:nil];
    
    // 创建播放器
    NSError *error = nil;
    AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
    
    // 准备播放
    [player prepareToPlay];
     
    // 播放歌曲
    // [self.player play];
    
     //注意:停止播放效果和暂停播放一样,并不会重新开始播放
     //暂停播放
     [player pause];

     //停止播放
     [player stop];
        
注意:一个play对象对应一个url。因此切换歌曲的时候不能用kvc去修改url的值
优化:

+ (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;
    }
}


2:AVPlayer的简单使用


 //延时加载
 - (AVPlayer *)player
{
    if (_player == nil) {
        // 1.获取URL(远程/本地)
        // NSURL *url = [[NSBundle mainBundle] URLForResource:@"01-知识回顾.mp4" withExtension:nil];
        NSURL *url = [NSURL URLWithString:@"http://v1.mukewang.com/a45016f4-08d6-4277-abe6-bcfd5244c201/L.mp4"];
        
        // 2.创建AVPlayerItem
        AVPlayerItem *item = [AVPlayerItem playerItemWithURL:url];
        
        // 3.创建AVPlayer
        _player = [AVPlayer playerWithPlayerItem:item];
        
        // 4.添加AVPlayerLayer
        AVPlayerLayer *layer = [AVPlayerLayer playerLayerWithPlayer:self.player];
        layer.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.width * 9 / 16);
        [self.view.layer addSublayer:layer];
    }
    return _player;
}


上一篇下一篇

猜你喜欢

热点阅读