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];
  • 相关阅读:
    docker exit status 255解决
    postgresql安装,java简单使用postgresql
    jQuery学习笔记
    2017.11.06
    UML类图讲解
    设计模式:可复用面向对象软件的基础
    conflicting types for xx错误
    QString的拼接
    source In sight 中修改自动补全快捷键方式
    设置Qt应用程序图标及应用程序名 【转载】
  • 原文地址:https://www.cnblogs.com/liyajie/p/4947948.html
Copyright © 2011-2022 走看看