音视频

 1.录音

  NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
   
   
    path = [path
stringByAppendingPathComponent:@"123.MP3"];
   
   
NSURL *url = [NSURL URLWithString:path];

   
   
//1.1创建录音对象
   
// settings  录音文件质量的
   
NSDictionary *setting = [NSDictionary dictionary];
   
AVAudioRecorder *recorder = [[AVAudioRecorder alloc]initWithURL:url settings:setting error:nil];
   
   
//2.开始
    [recorder record];

   2.播放音效
   //1.获取文件
   
   
NSString *path = @"/Users/apple/Desktop/123.MP3";
   
   
NSURL *url = [NSURL fileURLWithPath:path];
   
   
SystemSoundID soundID = 0;
   
   
//1.返回值  2.&
   
//桥接
   
AudioServicesCreateSystemSoundID((__bridge CFURLRef _Nonnull)(url), &soundID);
   
   
   
//2.播放
   
   
//没有震动效果
   
AudioServicesPlaySystemSound(soundID);
   
   
//有震动效果
//    AudioServicesPlayAlertSound(soundID);

3.播放多个文件,工具类
#import <Foundation/Foundation.h>

@interface CZPlayMusicTool : NSObject
//播放音乐
+ (
void)playAudioWithName:(NSString *)name;
//暂停音乐
+ (
void)pauseAudioWithName:(NSString *)name;
//停止音乐
+ (
void)stopAudioWithNmae:(NSString *)name;
@end


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

static NSMutableDictionary *_players;
@implementation CZPlayMusicTool

+ (
void)initialize{
   
   
_players = [NSMutableDictionary dictionary];
}
//播放音乐
+ (
void)playAudioWithName:(NSString *)name{
   
   
AVAudioPlayer *player = _players[name];
   
   
if (player == nil) {
   
   
NSString *path = [[NSBundle mainBundle]pathForResource:name ofType:nil];
   
   
NSURL *url = [NSURL fileURLWithPath:path];
   
    player = [[
AVAudioPlayer alloc]initWithContentsOfURL:url error:nil];
        [
_players setObject:player forKey:name];
    }
   
//开始播放
    [player
play];
   
}
//暂停音乐
+ (
void)pauseAudioWithName:(NSString *)name{
   
AVAudioPlayer *player = _players[name];
   
if (player) {
        [player
pause];
    }
}
//停止音乐
+ (
void)stopAudioWithNmae:(NSString *)name{
   
AVAudioPlayer *player = _players[name];
   
if (player) {
       [player
stop];
    }
 
   
}
- (IBAction)play:(id)sender {
    [
CZPlayMusicTool  playAudioWithName:@"咖啡.mp3"];
}
- (
IBAction)stop:(id)sender {
    [
CZPlayMusicTool playAudioWithName:@"煎饼侠.mp3"];
}
- (
IBAction)anting:(id)sender {
    [
CZPlayMusicTool pauseAudioWithName:@"煎饼侠.mp3"];
}
- (
IBAction)djkfs:(id)sender {
    [
CZPlayMusicTool stopAudioWithNmae:@"咖啡.mp3"];
}
- (
IBAction)stop2:(id)sender {
    [
CZPlayMusicTool stopAudioWithNmae:@"煎饼侠.mp3"];
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值