//
// ViewController.swift
// media
//
// Created by cyan on 15/7/18.
// Copyright (c) 2015年 cyan. All rights reserved.
//
import UIKit
import AVFoundation //播放长音
import AudioToolbox //播放短音
import MediaPlayer //播放本地音乐库的音乐
class ViewController: UIViewController ,AVAudioPlayerDelegate {
var audioPlayer:AVAudioPlayer!
var moviePlayer:MPMoviePlayerController!
override func viewDidLoad() {
super.viewDidLoad()
/*
//1.AVFoundation类封装类播放单个声音的能力。播放器用NSURL或者NSData来初始化。需要注意的是NSURL在此处AVFoundation下面,必须是本地的文件的url。因为这个 AVFoundation 封装的AVAudioPlayer不具备播放网络音频的能力。
//获取资源路径
let audioPath = NSBundle.mainBundle().pathForResource("Music", ofType: ".mp3")
//将这个获取到的路径封装成url
var url = NSURL(fileURLWithPath: audioPath!)
//建立播放器
audioPlayer = AVAudioPlayer(contentsOfURL: url, error: nil)
if (audioPlayer == nil){
println("nil")
}
audioPlayer.numberOfLoops = -1 //设置音乐的播放次数,-1为循环播放
audioPlayer.volume = 0.8 //设置音量范围,范围是0.0-1.0
audioPlayer.prepareToPlay()//准备播放音乐
//开始播放
audioPlayer.play()
//停止播放
//audioPlayer.stop()
//暂停播放
// audioPlayer.pause()
*/
/*
//2.短音,需要加载AudioToolbox框架
//首先必须通过AudioServicesCreateSystemSoundID注册这个声音文件
var soundPath = NSBundle.mainBundle().pathForResource("Sound12", ofType: ".aif")
var soundURL = NSURL(fileURLWithPath:soundPath!)
var shortSound : SystemSoundID = SystemSoundID()
var err = AudioServicesCreateSystemSoundID(soundURL as! CFURL, &shortSound)
println(err)
if(err != 0){//kAudioServicesNoError(没有错误时的代码)
println("不能加载文件\(soundURL),错误代码:\(err)")
}else{
AudioServicesPlaySystemSound(shortSound)
}
}
*/
/**/
//3.视频,使用MPMoviePlayerController来播放电影文件,但只能播放本地的MPEG-4,和H.264
//需要加载MediaPlayer框架
var moviePath = NSBundle.mainBundle().pathForResource("movie", ofType: ".m4v")
if(moviePath != nil){
var movieURL = NSURL(fileURLWithPath: moviePath!)
moviePlayer = MPMoviePlayerController(contentURL: movieURL)
//MPMoviePlayerController有一个视图来展示播放器的控件
self.view.addSubview(moviePlayer.view)
//设置播放器的尺寸
moviePlayer.view.frame = CGRectMake(50, 50, 300, 200)
moviePlayer.allowsAirPlay = true
moviePlayer.fullscreen = true //全屏
//需要在应用程序中实现播放功能,还需要注册MPMoviePlayerPlaybackDidFinishNotification到NSNotification
NSNotificationCenter.defaultCenter().addObserver(self, selector: "playMovieFinished:", name: MPMoviePlayerPlaybackDidFinishNotification, object: moviePlayer)
moviePlayer.prepareToPlay()
moviePlayer.play()
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func playMovieFinished(theNotification:NSNotification){
NSNotificationCenter.defaultCenter().removeObserver(self,name:MPMoviePlayerPlaybackDidFinishNotification,object:moviePlayer)
moviePlayer.view.removeFromSuperview()
}
func audioPlayerDidFinishPlaying(player: AVAudioPlayer!, successfully flag: Bool) {
//播放结束时执行的动作
}
func audioPlayerDecodeErrorDidOccur(player: AVAudioPlayer!, error: NSError!) {
//解码错误执行的动作
}
func audioPlayerBeginInterruption(player: AVAudioPlayer!) {
//处理中断的代码
}
func audioPlayerEndInterruption(player: AVAudioPlayer!) {
//处理中断结束的代码
}
}