iOS音频播放基础

#import "ViewController.h"

#import <AudioToolbox/AudioToolbox.h>


//如果想使用音乐播放功能  就必须导入AVFoundation

#import <AVFoundation/AVFoundation.h>

@interface ViewController ()<AVAudioPlayerDelegate>

{

    

    AVAudioPlayer *player;

}

@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    /*

//  AudioToolbox  通过系统声音ID   播放音效

    

    NSString *path = [[NSBundle mainBundle]pathForResource:@"背景音乐.caf" ofType:nil];

    

//   注册系统声音的ID

    SystemSoundID ID = 1;

    AudioServicesCreateSystemSoundID((__bridge CFURLRef)([NSURL fileURLWithPath:path]), &ID);

    

//    播放

    AudioServicesPlayAlertSound(ID);

//    播放带有震动的

//    AudioServicesPlayAlertSound(ID);

    */

    

    [self creatPlayerWithAudioName:@""];

}

- (void)creatPlayerWithAudioName:(NSString *)name

{

    

//    初始化音乐播放器

     NSString *path = [[NSBundle mainBundle]pathForResource:@"背景音乐.caf" ofType:nil];

    if (player) {

        player = nil;

    }

    NSError *error;

    player = [[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath:path] error:&error];

//    音量

    player.volume = 0.3;

    

//    速率  如果想设置播放速率  必须 得先开启允许设置

    player.enableRate = YES;

    player.rate = 1;

    

//    声道(-1.0左声道  0.0中间  1.右声道

    player.pan = -1.0;

    

//    循环次数 0播放一次  负数循环播放   正数设置为+1

    player.numberOfLoops = -1;

//    预播放 (把音乐文件加载到内存)

    [player prepareToPlay];

    

    player.delegate = self;

    //    设置音乐播放器 当前的播放时间

    player.currentTime = 150;

    //    duration 总时长  只读

    NSLog(@"音乐的总时长%f",player.duration);

    NSLog(@"通道的数量%ld",player.numberOfChannels);

    [player play];

    

//    player.isPlaying

//    暂停

//    [player pause];

//    停止

//    [player stop];

    NSLog(@"设备当前时间%f",player.deviceCurrentTime);


}

#pragma mark --------代理方法

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag

{

    NSLog(@"....");

}

@end














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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值