关闭

多媒体的应用

174人阅读 评论(0) 收藏 举报
分类:

首先要提一下沙盒,因为在音频的录制过程当中,ios把他放在了沙盒中的document文件目录下,所谓沙盒,也就是应用程序只能访问自己的文件,而不能翻过墙去访问其他的应用程序的文件,恰恰提现了苹果的闭源

http://blog.csdn.net/totogo2010/article/details/7669837

主要学习一下多媒体的播放声音 和播放视频等的应用

首先先下载一段音乐 随后直接拖入到文件 进行拷贝 随后再sb中添加三个按钮 为play pause 和stop为三种状态 随后添加头文件  为3个按钮指定动作

代码如下


import UIKit
//导入播放媒体的类
import AVFoundation
class ViewController: UIViewController {
//初始化播放的对象
    var player = AVAudioPlayer()
    override func viewDidLoad() {
        //加载Player
        do { try player = AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath:NSBundle.mainBundle().pathForResource("泡沫", ofType: "mp3")!))
        }
        catch{
            print("加载失败")
        }
      super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        }
         override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
            }
    @IBAction func play(sender: UIButton) {
        //播放
        player.play()
    }
    @IBAction func pause(sender: UIButton) {
    //暂停
        player.pause()
    }
    
    @IBAction func stop(sender: UIButton) {
        //停止 
        player.stop()
        //返回最开始 
        player.currentTime = 0
    }
}
随后便可以播放声音了

以上主要输用来呈现短音乐的,较长的音乐可以使用音乐播放器

添加一个playSound的按钮

为其添加事件

//导入音乐播放器的库
import MediaPlayer

@IBAction func playSong(sender: UIButton) {
        //加载音乐播放器
        var mediaPlayer = MPMoviePlayerViewController(contentURL: NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("泡沫", ofType: "mp3")!))
        //加载音乐播放器的视图 是一个viewcontroller
        presentViewController(mediaPlayer, animated: true, completion: nil)
    }

随后点击按钮会出现一个音乐播放器的界面

@IBAction func playSong(sender: UIButton) {
        //加载音乐播放器
        var mediaPlayer = MPMoviePlayerViewController(contentURL: NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("泡沫", ofType: "mp3")!))
        //加载音乐播放器的视图 是一个viewcontroller
        presentViewController(mediaPlayer, animated: true, completion: nil)
    }


播放库音乐播放器,由于该操作是要在手机上模拟的,因此,只给出代码吧

import UIKit
//导入播放媒体的库
import AVFoundation
//导入音乐播放器的库
import MediaPlayer
class ViewController: UIViewController,MPMediaPickerControllerDelegate {
//获取音乐
    var picker = MPMediaPickerController()
//播放音乐
    var player = MPMusicPlayerController()
    
        override func viewDidLoad() {
                    //进行初始化
            picker = MPMediaPickerController(mediaTypes:MPMediaType.Music)
            //指定处理器
            picker.delegate = self
            //可以选择多个
            picker.allowsPickingMultipleItems = true
            //在选择音乐的程序退出后还能播放音乐
            player = MPMusicPlayerController.iPodMusicPlayer()
      super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        }
         override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
            }
    //有选择到一些内容
    func mediaPicker(mediaPicker: MPMediaPickerController, didPickMediaItems mediaItemCollection: MPMediaItemCollection) {
        //将选择到后的选择歌曲的界面取消掉
        mediaPicker.dismissMoviePlayerViewControllerAnimated()
        //选择到后 可以进行播放的操作
        player.setQueueWithItemCollection(mediaItemCollection)
        player.play()
        print("\(mediaItemCollection)")
        //获取音乐相关的信息
        //获取音乐的名字
       var firstName =  mediaItemCollection.items[0].valueForProperty(MPMediaItemPropertyTitle)
        print("\(firstName)")
        
    }
    //取消掉一些值
    func mediaPickerDidCancel(mediaPicker: MPMediaPickerController) {
        print("cancel")
        mediaPicker.dismissMoviePlayerViewControllerAnimated()
    }
    }
import UIKit
//导入播放媒体的库
import AVFoundation
//导入音乐播放器的库
import MediaPlayer
class ViewController: UIViewController{
//录音的初始化
    var avRec = AVAudioRecorder()
//录音的存取文件路径的初始化 为nsurl
    var audioFileUrl:NSURL!
        override func viewDidLoad() {
          //文件路径的初始化
            audioFileUrl = (NSFileManager.defaultManager().URLsForDirectory(NSSearchPathDirectory.DocumentDirectory, inDomains: NSSearchPathDomainMask.AllDomainsMask)[0] as NSURL).URLByAppendingPathComponent("rec")
            do {try avRec = AVAudioRecorder(URL: audioFileUrl, settings:avRec.settings )
            }
            catch{
            print("出错")
            }
            
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        }
         override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
            }
    @IBAction func start(sender: UIButton) {
        avRec.record()
    }
    
    @IBAction func stop(sender: UIButton) {
        avRec.stop()
    }
    
    @IBAction func pause(sender: UIButton) {
        avRec.pause()
    }
        }

录制音频的较好的两个网站

http://www.tuicool.com/articles/6V3YFv

http://ningandjiao.iteye.com/blog/1904117

还有音乐播放器的代理,表示音乐播放器当停止播放 或者在解码的过程中出现失误的回调操作

import UIKit
import AVFoundation
class ViewController: UIViewController,AVAudioPlayerDelegate {
var player = AVAudioPlayer()
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        player.delegate = self
    //表明播放的路径
    let path = NSBundle.mainBundle().pathForResource("小幸运", ofType: "mp3")
        do { player = try AVAudioPlayer(contentsOfURL: NSURL(string: path!)!)
        }
        catch{
            print("error")
        }
    //播放音频
        player.play()
    //在指定什么时间开始播放
        player.playAtTime(NSTimeInterval(29))
        //暂停
        player.pause()
        //预处理播放设备
        player.prepareToPlay()
        //播放的音量
        print(player.volume)
        //循环的次数 -1表示不循环
        player.numberOfLoops = -1
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    func audioPlayerBeginInterruption(player: AVAudioPlayer) {
        print("当播放器中断时回调,比如打进电话")
    }
    func audioPlayerEndInterruption(player: AVAudioPlayer, withFlags flags: Int) {
        print("中断结束")
    }
    func audioPlayerDidFinishPlaying(player: AVAudioPlayer, successfully flag: Bool) {
        print("播放结束")
    }
    func audioPlayerDecodeErrorDidOccur(player: AVAudioPlayer, error: NSError?) {
        print("解码失误")
    }
    
}




0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:66239次
    • 积分:2551
    • 等级:
    • 排名:第14432名
    • 原创:197篇
    • 转载:0篇
    • 译文:0篇
    • 评论:5条
    最新评论