ios之音频系列

1.AVPlayer简单使用

//url自己随便百度整一个可以播放音乐就行

//注意打开网络协议 不然不能上网  

//player是AVPlayer类的对象 必须设置为全局变量


NSString *str =@"http://127.0.0.1/%E7%AC%AC%E4%B8%80%E5%A4%AB%E4%BA%BA.mp3";

    NSURL *url = [NSURLURLWithString:str];

    player = [[AVPlayeralloc]initWithURL:url];

    [playerplay];

    //暂停[playerpause]; 


2.将本地声音转为系统声音 并播放  注意声音文件格式

NSString *path = [[NSBundlemainBundle]pathForResource:@"44th Street Medium.caf"ofType:nil];//这个文件自己拖进工程 

    NSURL *url =[NSURLfileURLWithPath:path];

    //注册为系统声音

    UInt32 soundID;

    AudioServicesCreateSystemSoundID((__bridgeCFURLRef _Nonnull)(url), &soundID);

    AudioServicesPlaySystemSound(soundID);

 //震动播放真机尝试才行

    AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);

    //将制定的系统声音给销毁

    //AudioServicesDisposeSystemSoundID(soundID);


3.录音以及播放


#import "ViewController.h"

#import <AVFoundation/AVFoundation.h>

@interface ViewController ()

{

    

    NSString *recorderPath;

    AVAudioRecorder *audioRecoder;

    AVAudioPlayer *player;

}

@end


@implementation ViewController


- (void)viewDidLoad {

    [superviewDidLoad];

    

    UIButton *startRecoderbtn = [UIButtonbuttonWithType:UIButtonTypeCustom];

    startRecoderbtn.frame =CGRectMake(100,100, 100,50);

    [startRecoderbtn setTitle:@"开始录音"forState:UIControlStateNormal];

    startRecoderbtn.backgroundColor = [UIColororangeColor];

    [startRecoderbtn addTarget:selfaction:@selector(starRecoder)forControlEvents:UIControlEventTouchUpInside];

    [self.viewaddSubview:startRecoderbtn];

    

    

    UIButton *stopRecoderbtn = [UIButtonbuttonWithType:UIButtonTypeCustom];

    stopRecoderbtn.frame =CGRectMake(100,200, 100,50);

    [stopRecoderbtn setTitle:@"停止录音"forState:UIControlStateNormal];

    stopRecoderbtn.backgroundColor = [UIColororangeColor];

    [stopRecoderbtn addTarget:selfaction:@selector(stopRecoder)forControlEvents:UIControlEventTouchUpInside];

    [self.viewaddSubview:stopRecoderbtn];

    

    

    UIButton *playRecoderbtn = [UIButtonbuttonWithType:UIButtonTypeCustom];

    playRecoderbtn.frame =CGRectMake(100,300, 100,50);

    [playRecoderbtn addTarget:selfaction:@selector(playRecoder)forControlEvents:UIControlEventTouchUpInside];

    [playRecoderbtn setTitle:@"播放录音"forState:UIControlStateNormal];

    playRecoderbtn.backgroundColor = [UIColororangeColor];

    [self.viewaddSubview:playRecoderbtn];

    

    recorderPath = [NSHomeDirectory()stringByAppendingPathComponent:@"av.caf"];

    

    NSLog(@"%@",NSHomeDirectory());

    

}


- (void)starRecoder{

    NSURL *url = [NSURLfileURLWithPath:recorderPath];

    NSMutableDictionary *settring = [NSMutableDictionarydictionary];

    //音频格式

    settring[AVFormatIDKey] =@(kAudioFormatAppleIMA4);

    audioRecoder = [[AVAudioRecorderalloc]initWithURL:urlsettings:settring error:nil];

    [audioRecoderprepareToRecord];

    [audioRecoderrecord];

    

    

}



- (void)stopRecoder{

    [audioRecoder pause];

    

}


- (void)playRecoder{

    NSURL *url = [NSURL fileURLWithPath:recorderPath];

    player = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:nil];

    [player play];

    

}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值