import UIKit import AVFoundation//引入流媒体基础框架,它能收集各种多媒体数据,这些数据可以来自不同的输入设备,比如摄像机、录音机等 class ViewController: UIViewController { var audioPlayer:AVAudioPlayer = AVAudioPlayer()//初始化音频播放对象,并将音频播放对象作为视图控制器类的属性 override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. let path = Bundle.main.path(forResource: "MarryYou", ofType: "mp3")//获取沙箱目录中音频文件的所在路径 let soundUrl = URL(fileURLWithPath: path!)//将字符串路径转换成网址路径 do {//在音频播放前,首先创建一个异常捕捉语句 try audioPlayer = AVAudioPlayer(contentsOf: soundUrl)//对音频播放对象进行初始化,并加载指定的音频文件 audioPlayer.volume = 1.0//设置音频播放对象的音量大小 audioPlayer.numberOfLoops = -1//设置音频播放次数,-1为无线循环播放 audioPlayer.play() } catch { print(error) } } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }