zoukankan      html  css  js  c++  java
  • [Xcode 实际操作]六、媒体与动画-(14)使用SystemSoundId播放简短声音

    目录:[Swift]Xcode实际操作

    本文将演示如何播放音频素材。

    在项目名称上点击鼠标右键,弹出右键菜单,

    选择【Add Files to "DemoApp"】,往项目中导入文件命令。

    点击选择一个音频文件->【Add】

    在项目导航区,打开视图控制器的代码文件【ViewController.swift】

     1 import UIKit
     2 //首先导入音频工具箱框架,这样才可以使用系统声音服务
     3 import AudioToolbox
     4 
     5 class ViewController: UIViewController {
     6 
     7     override func viewDidLoad() {
     8         super.viewDidLoad()
     9         // Do any additional setup after loading the view, typically from a nib.
    10         
    11         //声明一个系统声音标识类型的声音变量
    12         var _soundId:SystemSoundID = 0
    13         //获取位于主程序的目录中,声音文件的所在路径
    14         let path = Bundle.main.path(forResource: "camera", ofType: "wav")
    15         //将字符串路径,转换为网址路径
    16         let soundUrl = URL(fileURLWithPath: path!)
    17         //对于按键音、下拉菜单音等较短暂的声音,以及震动效果,
    18         //可以使用系统音频服务来播放
    19         AudioServicesCreateSystemSoundID(soundUrl as CFURL, &_soundId)
    20         
    21         //播放指定的音频文件
    22         AudioServicesPlaySystemSound(_soundId)
    23     }
    24 
    25     override func didReceiveMemoryWarning() {
    26         super.didReceiveMemoryWarning()
    27         // Dispose of any resources that can be recreated.
    28     }
    29 }
  • 相关阅读:
    java小知识点5
    java小知识点4
    java小知识点3
    编程之法:面试和算法心得(寻找最小的k个数)
    389. Find the Difference
    104. Maximum Depth of Binary Tree
    485. Max Consecutive Ones
    693. Binary Number with Alternating Bits
    463. Island Perimeter
    566. Reshape the Matrix
  • 原文地址:https://www.cnblogs.com/strengthen/p/10040541.html
Copyright © 2011-2022 走看看