播放本地短音频和本地歌曲

播放音频,首先要导入框架   #import <AVFoundation/AVFoundation.h>

1,播放短音频:(如简短提示音)

//    步骤:1,注册 2,播放
//    /*
    NSString *path = [[NSBundle mainBundle]pathForResource:@"message_prompt" ofType:@"mp3"];
//
    SystemSoundID ID = 1;
//    1,注册系统声音的ID
    AudioServicesCreateSystemSoundID((__bridge CFURLRef)([NSURL fileURLWithPath:path]), &ID);
//    播放
    AudioServicesPlaySystemSound(ID);
//
//    震动
    AudioServicesPlayAlertSound(ID);
//     音效播放 (系统声音服务)是不允许咱们去控制音频的


2,播放长音频、歌曲等

    播放声音文件
    使用 AVFoundation(audio 和 video)处理音频 视频的操作
    1,音频播放
    如果想使用 音乐播放功能 就必须导入AVFoundation
    使用 AVAudioPlayer 播放音频
    设置音频的音量(volume),播放速率(rate),音频播放的位置,得到总时长,监测音频是否播放完毕、音乐播放的循坏次数。。。

NSString *path = [[NSBundle mainBundle]pathForResource:@"58" ofType:@"mp3"];
    NSError *error;
    
    if (player) {
        player = nil;
    }

#pragma mark  ------初始化音频播放器-------------
    player = [[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath:path] error:&error];
//    音量 0-1
    player.volume = 1;
    
//    速率
//    如果想设置播放速率 必须得先开启允许设置
    player.enableRate = YES;
    player.rate = 1;
    
//    声道(-1.0 左声道  0.0中间  1.0 右声道)
//    player.pan = -1.0;
    
//    循坏次数 0 是一次,负数是循坏,正数是设置次数加1
    player.numberOfLoops = 0;
    
    player.delegate = self;
//    预播放(把音乐文件的数据加载到内存)
    [player prepareToPlay];
//    播放
    [player play];
    
//    设置音乐播放器 当前播放的时间
    player.currentTime = 0;
    
//    duration总时长 只读
    NSLog(@"总时长%f",player.duration);
    NSLog(@"通道的数量%lu",(unsigned long)player.numberOfChannels);
    
//    player.isPlaying 是否正在播放

//    暂停
//    [player pause];
    
//    停止
//    [player stop];
    
//
    NSLog(@"设备当前的时间%f",player.deviceCurrentTime);


// 音乐播放的代理方法
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{
    NSLog(@"^_^ 成功播放 ~~~");
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值