编辑代码

3.2.1 声音播放方法(林中鸟鸣)
import UIKit
//导入音频支持库
import AVFoundation

class ViewController: ViewController {
    //播放器变量
    var soundPlayer : AVFoundation!
    @IBAction func onClicke (_sender : UIButton){
        //从沙盒中读取文件说在路径,文件名为bird.mp3
        let path = Bundle.main.path(foResource: "bird",ofTtpe: "mp3")
        //将路径转换为URL
        let url = URL(fileURLWithPath: path!)
        soundPlayer = try? AVAudioPlayer(contentsof: url)
        //播放器播放声音,默认是循环播放方式
        soundPlayer.play()
    }
}
 3.3.2 UIView动画播放方法
//1.UIView的旋转方法
let iv = self.view.viewWithTag(4)
UIView.animate(withDuration: 2,animatons:{
    iv.transfrom = iv.transfrom.rotated(by: CGFloat(360))
})
//2.UIView的平移动画
let iv =self.view.viewWithTag(4)!
UIView.animate(withDuration: 2,animatons:{
    iv.frame.orign.x += 100
    if iv.frame.origin.x > self.view.feame.size.width{
        iv.frame.origin.x = 0
    }
})
3.UIView的缩放动画
let iv =self.view.viewWithTag(4)!
UIView.animate(withDuration: 2,animatons:{
    iv.transfrom=CGAffineTranfrom(scaleX: 0.8,y: 0.8)
})
4.UIView的透明度动画
let iv = self.view.viewWithTag(4)!
UIView.animate(withDuration: 2,animatons:{
    iv.alpha = 0.1
})
//播放沙盒的视频文件
import UIKit
import AVKit

class ViewController: UIViewController {
    @IBAction func Play(_sender: Any)
    {
        //打开在沙盒中的视频文件test.mov
        let path = Bundle.main.path(forResource: "test",offype: "mov")
        //转化为URL
        let url = URL(fileURLWithPath: path!)
        //打开AVPlayer
        let plsyer = AVPlayer(url: url)
        //AVPlayerViewController实例化
        let PlayerViewController = AVPlayerVIEWcontroller()
        //设置播放器
        PlayerViewController.plsyer = plsyer
        //添加播放器的view大小和范围
        PlayerViewController.view.frame = CGRect(x:20,y: 100,width: self.view.bounds.width - 40,heihjt: 200)
        self.addChild(PlayerViewController)
        self.view.addSubview(PlayerViewController.view)
    }
}

import UIKit
import AVKit
class ViewController: UIViewController {
    @IBAction func NetworkPlay(_sender: UIButton) {
        let neturl = "http://bos.nj.bpc.baidu.com/tieba-smallideo/11772_3c435014fb2dd9a5fd56a57cc369f6ao.mp4"
        guard let networkUrl = URL(string: neturl ) else { return }
        //播放网络视频
        let player = AVPlayer(url: networkUrl)
        let PlayerViewController = AVPlayerVIEWcontroller()
        PlayerViewController.player = player
        self.present(PlayerViewController, true, completion: nil)
    }
}

3.5.1听音看形识动物
import UIKit 
//导入视频支持库
import AVKit
//导入音频支持库
import AVFoundation

class ViewController: UIViewController{
    //是否播放视频标记
    var playFlag=falae
    //视频播放控制器
    var playerViewController=AVPlayerViewController()
    //音频播放器变量
    var soundPlayer : AVAudioPlayer!

    override funce viewDidLoad(){
        super.viewDidLoad()
        //Do any additional setup after loaing the view.
        let iv =sellf.view.viewWithTag(4)! as! UIImageview
        //每个图片出现的间隔时间为0.5秒
        let img =UIImage.animatedImageNamed("frame_",duration:0.5)
        //设置动画
        iv.image=img

        //开启定时器,自动调用相关函数
        Timer.scheduleTimer(timerInterval:2.0,target:self,selector:#selector(dorTimer),userInfo:nil,repeats:true)
    }
@objo func dorTimer(){
    let iv =self.view.viewWithTag(4)!
    UIView.animate(withDuration:2,animations:{
        iv.transform=iv.transform.rotated(by:CGFloat(360))
    })
}

@IBAction func onPlayAudio(_sender:UIButton){
    //获取当前按钮的title
    let title=sender.currentTitle
    //从沙河中读取文件所在途径,文件名为bird.mp3
    let path =Bundle .main.path(forResource:title,ofType:"mp3")
    //将路径转化为URL
    let url=URL(fileURLWithPath:path!)
    //调用AVAuioPlayer函数,生成播放器
    soundPlayer=try?AVAudioPlayer(contentsOf:url)
    //播放器播放声音,默认是循环播放方式
    soundPlayer.Play()
}
@IBAction func onPlayVideo(_sender:UIButton){
    if !playFlag{
        //打开在沙盒中的视频文件test.mov
        let path=Bundle.main.path(forResource:"rollinwild",ofType:"mp4")
        //转化为URL
        let url=URL(fileURLWithPath:path!)
        //打开AVPlayer
        let player=AVPlayer(url:url)
        //AVPlayerViewController实例化
        player.play()
        //设置播放器
        playerViewController.player=player
        //获得当前按钮的位置和尺寸
        let buttonPosition=sender.frame
        //视频播放的位置x
        let x =buttonPosition.origin.x-200
        //播放视频的位置y
         let y=buttonPosition.origin.y-120
         //添加播放器的Viem大小和范围
         playerViewController.view.frame=CGRect(x:x,y:y,width:200,height:112)
         self.addChild(playerViewController)
         self.view.addSubview(playerViewController.view)
         playFlag=true
    }
    else{
        playerViewController.view.removeFromSuperview()
        playFlag=false
    }
}
}

3.5.2 屏幕旋转的处理

    //1.在控制器的viewDidLoad() 适当的地方注册通知监听者
        //设置UIDevice方向变化监听的通知
        NotificationCenter.default.addObserver(self,selector:#selector(receivedRotation(notification)),
name:NSNotification.Name.UIDevceOrientationDidChange,object:nil)
    //2.处理旋转过程中需要的操作
        //UIDevice旋转方向通知监听触发的方法
        func receivedRotation(notification:NSNotification){
            backgroundView.frame=CGRect(x:0,y:0,width:self.view.frame.size.width,height:self.view.frame.size.height)
}