zoukankan      html  css  js  c++  java
  • IOS播放音乐和音效

    1.播放音效

      1.1 首先获取到音效文件路径  

    1 NSString *path = [[NSBundle mainBundle] pathForResource:soundFileName ofType:nil];

      1.2 将音效文件路径转换为NSURL

    NSURL *url = [NSURL fileURLWithPath:path];

      1.3 加载音效文件,并返回SystemSoundID(用于播放此音效)

    SystemSoundID soundId ;
        //如下方式是C语言的框架,是将音频文件处理之后返回systemSoundId赋值给&soundId
        AudioServicesCreateSystemSoundID((__bridge CFURLRef _Nonnull)(url), &soundId);

      1.4 在使用的地方播放音效

    AudioServicesPlaySystemSound(soundId);

    2.播放音乐

      2.1 获取包中音乐文件的路径

    NSString *path = [[NSBundle mainBundle] pathForResource:@"背景音乐" ofType:@"caf"];
                          NSLog(@"%@",path);

      2.2 把路径转换成url

    NSURL *url = [NSURL fileURLWithPath:path];

      2.3 通过url初始化音频播放器

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

      2.4 设置音乐循环播放次数,-1:无限循环播放,0:一次,1:2次...

    [player setNumberOfLoops:-1];

      2.5 设置播放的音量,可以不设置

    [player setVolume:0.5f];

      2.6 设置准备播放

    [player prepareToPlay];

      2.7 播放

    [player play];
  • 相关阅读:
    Golang教程:并发介绍
    Go在windows10 64位上安装
    ElasticSearch 聚合
    ElasticSearch深入搜索
    ElasticSearch基础入门
    php安装扩展
    报错提示优化
    NK3C程序配置
    NK3C 业务权限控制
    关于测试方法
  • 原文地址:https://www.cnblogs.com/liyajie/p/4947948.html
Copyright © 2011-2022 走看看