iOS开始之制作音乐播放器

一、音频播放与音效播放的基础知识

音效播放

1、音效播放
《1》功能介绍
用于播放比较短小的音效 常用作系统的提示音
使用到的框架 AudioToolBox
《2》框架介绍
1、SystemSoundID
2、AudioServicesCreateSystemSoundID
3、AudioServicesPlaySystemSound 播放系统声音
4、AudioServicesPlayAlertSound

一个实例
导入框架 #import
1、声明一个soundID
根据这个数字来区分是哪一个系统声音

        SystemSoundIDsoundID = 1;

2、创建一个系统声音的服务 注意修复错误

AudioServicesCreateSystemSoundID((__bridge CFURLRef _Nonnull)([[NSBundle
    mainBundle]URLForResource:@"胜利.aiff"withExtension:nil]), &soundID) ;

3、播放系统声音 带震动

AudioServicesPlayAlertSound(soundID);

不带震动
或者AudioServicesPlaySystemSound(soundID) ;

音频播放

1、介绍

  • 功能介绍
    用于播放比较长的音频、说明、音乐 ,使用到的是AVFoundation
  • 框架介绍
    AVAudioPlayer
  • 初始化:
    注意 :
    (3)必须声明全局变量的音乐播放对象、或者是属性的音乐播放对象 才可以播放
    (4)在退出播放页面的时候 一定要把播放对象置空 同时把delegate置空

导入框架:#import
声明全局变量

    @interface ViewController ()
    {
        AVAudioPlayer*audioPlayer;
    }
    @end

音频的基本属性:
预播放 [audioPlayer prepareToPlay];
获取 当前音乐的声道 audioPlayer.numberOfChannels
audioPlayer.currentTime //当前播放的时间
audioPlayer.playing//判断是否正在播放
audioPlayer.numberOfLoops ;//设置循环播放的此次
audioPlayer.duration 获得播放音频的时间
audioPlayer.pan 设置左右声道效果
audioPlayer.volume 设置音量 0.0- 1.0 是一个百分比
设置速率 必须设置enableRate 为YES; audioPlayer.enableRate =YES; audioPlayer.rate =3.0;
音量 audioPlayer.volume = 0.1;
设置播放次数 负数是无限循环的 0 是一次 1是两次 依次类推 audioPlayer.numberOfLoops = 0;
获得当前峰值 audioPlayer peakPowerForChannel:2
平均峰值
audioPlayer averagePowerForChannel:2
音频播放的几个代理方法:

    //播放完成的时候调用
   - (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{
       NSLog(@"播放完成");
   }
   //解码出现错误的时候调用
   - (void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError * __nullable)error{

   }
   //开始被打扰中断的时候调用
   - (void)audioPlayerBeginInterruption:(AVAudioPlayer *)player{

   }
   //中断结束后调用
   - (void)audioPlayerEndInterruption:(AVAudioPlayer *)player withOptions:(NSUInteger)flags{

   }

代码示例:(此链接下是一个完整的音乐播放器Demo,封装了播放的类)
在ViewDidLoad中

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(100, 100, 100, 100);
        button.backgroundColor = [UIColor brownColor];
        [button setTitle:@"Play" forState:UIControlStateNormal];
        [button setTitle:@"Pause" forState:UIControlStateSelected];
        [button addTarget:self action:@selector(play:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:button];

         [self playMusicWithName:@"TFBOYS-青春修炼手册.mp3"];
    按钮的触发方法:
    - (void)play:(UIButton *)sender{
        sender.selected = !sender.selected;
       sender.selected != YES ? [audioPlayer pause]:[audioPlayer play];
    }

    - (void)playMusicWithName:(NSString *)name{
        NSError *error;

    //    创建一个音乐播放对象
        audioPlayer  = [[AVAudioPlayer alloc]initWithContentsOfURL:[[NSBundle mainBundle]URLForResource:name withExtension:nil] error:&error];
        if (error) {
            NSLog(@"%@",error);
        }

        预播放
        [audioPlayer prepareToPlay];

        播放  在这里不写在这 但它是必须的步骤
        [audioPlayer play];
        获取  当前音乐的声道
        NSLog(@"%ld",audioPlayer.numberOfChannels);
        durtion:获得播放音频的时间
        设置声道 -1.0左 0.0中间 1.0右
        audioPlayer.pan = 0.0;
        音量
        audioPlayer.volume = 0.1;

        设置速率  必须设置enableRate为YES
        audioPlayer.enableRate = YES;
        设置速率 0.5是一半的速度 1.0普通 2.0双倍速率
        audioPlayer.rate = 1.0;

        currentTime 获得时间
       获得峰值  必须设置meteringEnabled为YES
        audioPlayer.meteringEnabled = YES;
        更新峰值
        [audioPlayer updateMeters];   
        获得当前峰值
        NSLog(@"当前峰值:%f",[audioPlayer peakPowerForChannel:2]);
        NSLog(@"平均峰值%f",[audioPlayer averagePowerForChannel:2]);

       设置播放次数 负数是无限循环的 0 是一次 1是两次 依次类推
        audioPlayer.numberOfLoops = 0;
        挂上代理
        audioPlayer.delegate = self;

    }

你可能感兴趣的:(iOS开始之制作音乐播放器)