iOS中音频

来源:互联网 发布:知乎a tender feeling 编辑:程序博客网 时间:2024/06/06 02:14

我们把音频播放分成两部分:

        一种播放时间短的

        一种播放时间长的

    短的常做于系统提示音 它需要导入的框架为AudioToolBox

 使用:

   步奏1、声明声音ID SystemSoundID系统播放声音文件时,就是靠声音ID来区分是哪一个音效 ID 是无符号整形

   步奏2、创建播放声音文件的服务  这个服务里有两个参数,第一个指的是声音文件的路劲 第二个指的是声音的ID  这个步奏只是告诉系统,你要准备播放的资源,并没有开始播放

   步奏3、播放声音

 下面用一个代码的例子来表演给大家看

    首先得导入这个框架

#import <AudioToolbox/AudioToolbox.h>

   SystemSoundID   soundtID =1; 声明ID

AudioServicesCreateSystemSoundID((__bridgeCFURLRef_Nonnull)([[NSBundlemainBundle]URLForResource:@"胜利.aiff"withExtension:nil]),&sountID); 创建系统声音的服务 这里我是以胜利的这个声音文件为例 

AudioServicesPlaySystemSound(sountID); 播放

播放的时候有两个情况:

  1.没有震动AudioServicesPlaySystemSound(sountID);

  2.有震动AudioServicesPlayAlertSound(sountID);



长的就是像我们的音乐播放器里面的那些资源

   它使用的框架为AVFoundation

需要这种播放的需要注意下几点:

  1、必须声明全局变量的音乐播放对象或者是声明为属性

  2、在退出音乐播放页面时,需要把播放对象给置空同时把delegate给置空

使用:

 步奏1、资源文件路劲

 步奏2、初始化播放器

 步奏3、设置播放器

 步奏4、预播放

 步奏5、播放

其实真使用时比较简单

我这的视图控制器叫 MusicPlayViewController

#import"MusicPlayViewController.h"
#import
<AVFoundation/AVFoundation.h>
@interfaceMusicPlayViewController ()<AVAudioPlayerDelegate>
{
   
AVAudioPlayer *audioPlayer;
}
@end

@implementationMusicPlayViewController

- (
void)viewDidLoad {
    [
superviewDidLoad];
用来控制音乐的播放和暂停的按钮
   
UIButton *playSound = [UIButtonbuttonWithType:UIButtonTypeCustom];
    [playSound
setTitle:@"播放"forState:UIControlStateNormal];
    [playSound
setTitle:@"暂停"forState:UIControlStateSelected];
    playSound.
tag= 10;
    playSound.
frame= CGRectMake(100,100,100,100);
    [playSound
addTarget:selfaction:@selector(playSound:)forControlEvents:UIControlEventTouchUpInside];
    [playSound
setBackgroundColor:[UIColorredColor]];
    [
self.viewaddSubview:playSound];
用来控制音乐的停止的按钮
   
UIButton *playSound1 = [UIButtonbuttonWithType:UIButtonTypeCustom];
    [playSound1
setTitle:@"停止"forState:UIControlStateNormal];
    playSound1.
frame= CGRectMake(50,210,100,100);
    playSound1.
tag= 11;
    [playSound1
addTarget:selfaction:@selector(playSound:)forControlEvents:UIControlEventTouchUpInside];
    [playSound1
setBackgroundColor:[UIColorredColor]];
    [self.viewaddSubview:playSound1];

[selfplayMusicWithName:@"鸟叔.mp3"Type:nil];

}
按钮的触发事件
-(void)playSound:(UIButton*)sender{
sender.selected= !sender.selected;
 if(sender.tag== 11) {
        [
audioPlayerstop];//停止
       
UIButton *button = [self.viewviewWithTag:10];
        button.selected= NO;
       [selfplayMusicWithName:@"鸟叔.mp3"Type:nil];
     
    }elseif (sender.selected== YES){
        [
audioPlayerplay];//播放
    }
else{
        [
audioPlayerpause];//暂停
    }
   
 
}
封装的音乐播放的方法
-(
void)playMusicWithName:(NSString*)name Type:(UIButton*)sender{
   
//获得文件的路劲并声明一个播放器对象有错误会把错误消息给error
   
NSError *error;
   
audioPlayer = [[AVAudioPlayeralloc]initWithContentsOfURL:[[NSBundlemainBundle]URLForResource:namewithExtension:nil]error:&error];
   
//判断是否有错误
   
if (error) {
       
NSLog(@"%@",error);
       
return;
    }
   
//开始预播放
    [
audioPlayerprepareToPlay];

    audioPlayer.pan= 0.0;//音乐的左右声道-1.00 中间 1.0
     audioPlayer.volume= 0.2;//音乐的音量0.0 - 1.0
   
//audioPlayer.numberOfChannels 获得音乐的声道
   
//设置速率必须设置enableRateyes
   
audioPlayer.enableRate= YES;
   
audioPlayer.rate= 2;//0.5一半1.0正常2.0双倍
   
//设置峰值必须设置meteringEnabledyes
   
audioPlayer.meteringEnabled=YES;
    [
audioPlayerupdateMeters];//更新峰值
   
//当前峰值[audioPlayer peakPowerForChannel:2];平均峰值[audioPlayer averagePowerForChannel:2];
   
//设置播放次数audioPlayer.numberOfLoops 0一次  1播放两次 负数是无限循环
   
audioPlayer.numberOfLoops= 0;
   
}
- (
void)audioPlayerDidFinishPlaying:(AVAudioPlayer*)player successfully:(BOOL)flag{
   NSLog(@"dddd");
}播放完后响应的事件
-(void)viewWillDisappear:(BOOL)animated{
    [
superviewWillDisappear:animated];
   
//如果不置空下次返回来时会没有声音 *
   
audioPlayer = nil;
   
audioPlayer.delegate= nil;
}

  


今天就给大家分享这么多,希望对大家有帮助,明天介绍下录音。谢谢!










0 0
原创粉丝点击