使用SystemSoundld播放简短声音
//导入音频工具箱框架。
import AudioToolbox
//使用SystemSoundld播放简短声音
func test1() {
//声明一个系统声音标识类型的声音变量
var _soundId : SystemSoundID = 0
//获取沙箱目录中,声音文件的所在路径
let path = Bundle.main.path(forResource: "camera", ofType: "wav")
let soundUrl = URL(fileURLWithPath: path!)
AudioServicesCreateSystemSoundID(soundUrl as CFURL, &_soundId)
AudioServicesPlayAlertSound(_soundId)
}
使用AudioPlayer播放音乐
导入流媒体基础框架
import AVFoundation
需要对音乐的播放进行监听 AVAudioPlayerDelegate
//初始化音频播放对象。并将音频播放对象作为视图控制器类的属性
var audioPlayer:AVAudioPlayer = AVAudioPlayer()
//获取沙箱目录中 音频文件的所在路径
let path = Bundle.main.path(forResource: "music", ofType: "mp3")
//将字符串路径转换层网址路径
let soundUrl = URL(fileURLWithPath: path!)
//在音频播放前 创建一个异常捕捉语句
do {
//初始化音频播放对象。并加载指定文件
try audioPlayer = AVAudioPlayer(contentsOf: soundUrl)
audioPlayer.volume = 1.0 //音量大小
audioPlayer.numberOfLoops = -1//设置播放次数 -1为轮回播放
//将音频的代理设置为视图控制器
audioPlayer.delegate = self
audioPlayer.play()
} catch {
print(error)
}
实现音乐的背景播放功能
添加一条新的信息
添加一条新的信息
import UIKit
//导入流媒体基础框架
import AVFoundation
//需要对音乐的播放进行监听
class ViewController: UIViewController{
//初始化音频播放对象
var audioPlayer:AVAudioPlayer = AVAudioPlayer()
override func viewDidLoad() {
super.viewDidLoad();
test1()
}
//使用AudioPlayer播放音乐
func test1() {
//获得音频会话对象 (该对象为单例模式)
let session = AVAudioSession.sharedInstance()
do {
//启动音频会话管理 ,此时会阻断后台音乐的播放
try session.setActive(true)
//设置音频操作类别。表示该应用仅支持音频的播放
try session.setCategory(AVAudioSessionCategoryPlayback)
//设置应用程序支持远程控制事件∂
UIApplication.shared.beginReceivingRemoteControlEvents()
//定义一个字符串常量。描声音文件的路径
let path = Bundle.main.path(forResource: "music", ofType: "mp3")
let soundUrl = URL(fileURLWithPath: path!)
//初始化音频对象。加载文件
try audioPlayer = AVAudioPlayer(contentsOf: soundUrl)
audioPlayer.prepareToPlay()
audioPlayer.volume = 1.0
audioPlayer.numberOfLoops = -1
audioPlayer.play()
} catch {
print(error)
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
使用MPMoviePlayerController实现视频播放
import UIKit
import MediaPlayer
class ViewController: UIViewController{
var mediaPlayer:MPMoviePlayerController = MPMoviePlayerController()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
do{
let path = Bundle.main.path(forResource: "movie", ofType: "mp4")
let movieUrl = URL(fileURLWithPath: path!)
mediaPlayer = MPMoviePlayerController(contentURL: movieUrl)
mediaPlayer.controlStyle = MPMovieControlStyle.fullscreen
mediaPlayer.view.frame = self.view.bounds
mediaPlayer.initialPlaybackTime = -1
self.view.addSubview(mediaPlayer.view)
NotificationCenter.default.addObserver(self, selector: #selector(ViewController.movieFinished(_:)), name: .MPMoviePlayerPlaybackDidFinish, object: mediaPlayer)
mediaPlayer.play()
} catch{
print(error)
}
}
@objc func movieFinished(_ notify:Notification)
{
print("Movie ends.")
let player = notify.object
NotificationCenter.default.removeObserver(self, name: NSNotification.Name.MPMoviePlayerPlaybackDidFinish, object: player)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}