zoukankan      html  css  js  c++  java
  • iOS开发拓展篇—音效的播放

    iOS开发拓展篇—音效的播放

    一、简单介绍

    简单来说,音频可以分为2种

    (1)音效

    又称“短音频”,通常在程序中的播放时长为1~2秒

    在应用程序中起到点缀效果,提升整体用户体验

    (2)音乐

      比如游戏中的“背景音乐”,一般播放时间较长

    框架:播放音频需要用到AVFoundation.framework框架

    二、音效的播放

    1.获得音效文件的路径

      NSURL *url = [[NSBundle mainBundle] URLForResource:@"m_03.wav" withExtension:nil];

    2.加载音效文件,得到对应的音效ID

      SystemSoundID soundID = 0;

      AudioServicesCreateSystemSoundID((__bridge CFURLRef)(url), &soundID);

    3.播放音效

      AudioServicesPlaySystemSound(soundID);

    注意:音效文件只需要加载1次

    4.音效播放常见函数总结

    加载音效文件

      AudioServicesCreateSystemSoundID(CFURLRef inFileURL, SystemSoundID *outSystemSoundID)

    释放音效资源

      AudioServicesDisposeSystemSoundID(SystemSoundID inSystemSoundID)

    播放音效

      AudioServicesPlaySystemSound(SystemSoundID inSystemSoundID)

    播放音效带点震动

      AudioServicesPlayAlertSound(SystemSoundID inSystemSoundID)

     

    三、程序示例

    先导入需要依赖的框架

    导入需要播放的音效文件素材

      

    说明:AVFoundation.framework框架中的东西转换为CF需要使用桥接。

    代码示例:

    YYViewController.m文件

     1 //
     2 //  YYViewController.m
     3 //  14-音效播放
     4 //
     5 //  Created by apple on 14-8-8.
     6 //  Copyright (c) 2014年 yangyong. All rights reserved.
     7 //
     8 
     9 #import "YYViewController.h"
    10 #import <AVFoundation/AVFoundation.h>
    11 
    12 @interface YYViewController ()
    13 
    14 @end
    15 
    16 @implementation YYViewController
    17 
    18 - (void)viewDidLoad
    19 {
    20     [super viewDidLoad];
    21 }
    22 
    23 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    24 {
    25     //1.获得音效文件的全路径
    26     
    27     NSURL *url=[[NSBundle mainBundle]URLForResource:@"buyao.wav" withExtension:nil];
    28     
    29     //2.加载音效文件,创建音效ID(SoundID,一个ID对应一个音效文件)
    30     SystemSoundID soundID=0;
    31     AudioServicesCreateSystemSoundID((__bridge CFURLRef)url, &soundID);
    32     
    33     //把需要销毁的音效文件的ID传递给它既可销毁
    34     //AudioServicesDisposeSystemSoundID(soundID);
    35     
    36     //3.播放音效文件
    37     //下面的两个函数都可以用来播放音效文件,第一个函数伴随有震动效果
    38     AudioServicesPlayAlertSound(soundID);
    39     //AudioServicesPlaySystemSound(<#SystemSoundID inSystemSoundID#>)
    40 }
    41 
    42 @end

    说明:点击屏幕可以播放音效文件。

  • 相关阅读:
    环境是如何建立的 启动文件有什么
    环境中存储的是什么
    串行 并行 异步 同步
    TPC-H is a Decision Support Benchmark
    进程通信类型 管道是Linux支持的最初Unix IPC形式之一 命名管道 匿名管道
    删除环境变量
    14.3.2.2 autocommit, Commit, and Rollback 自动提交 提交和回滚
    14.3.2.2 autocommit, Commit, and Rollback 自动提交 提交和回滚
    14.3.2.1 Transaction Isolation Levels 事务隔离级别
    14.3.2.1 Transaction Isolation Levels 事务隔离级别
  • 原文地址:https://www.cnblogs.com/wendingding/p/3900112.html
Copyright © 2011-2022 走看看