网站建设资讯

NEWS

网站建设资讯

swift开发ios视频的简单介绍

iOS 音视频开发 - 系统中断音频(Swift语言)

注册 AVAudioSession.interruptionNotification 的通知,可以收到播放打断通知。 系统将此通知发布到主线程。

创新互联主要从事网站建设、成都网站建设、网页设计、企业做网站、公司建网站等业务。立足成都服务武江,10余年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:18980820575

其中userinfo有如下字段:

分began与end,用来表示打断开始与打断结束。

只在打断结束时返回。选项用 shouldResume 来指示:另一个音频会话的中断已结束,应用程序可以恢复其音频会话。

该属性只在打断开始时返回。存在于版本iOS 10.3-14.5。

用于确定这次中断是否由于系统挂起App所致。

获取到的是一个NSNumber表示的Bool。为true表示中断是由于系统挂起,false是被另一音频打断。

该属性只在打断开始时返回。存在于版本iOS 14.5以后,用来代替 AVAudioSessionInterruptionWasSuspendedKey 。

default :因为另一个音频会话被激活,音频中断。(例如后台播着音频,此时播放微信语音)

appWasSuspended :由于APP被系统挂起,音频中断

builtInMicMuted :音频因内置麦克风静音而中断(例如iPad智能关闭套【iPad's Smart Folio】合上)

iOS swift 实现摄影视频 保存到本地

实现摄影视频  保存到本地

import UIKit

import AVFoundation

import Photos

class ViewController: UIViewController, AVCaptureFileOutputRecordingDelegate {

// 视频输出

let fileOutput = AVCaptureMovieFileOutput()

// 录像按钮

var recordButton: UIButton!

// 正在录音

var isRecording = false

override func viewDidLoad() {

    super .viewDidLoad()

// 设置预览画面

    setUpPreview()

}

func setUpPreview() {

    let videoDevice =AVCaptureDevice.default(for:AVMediaType.video)

    let audioDevice =AVCaptureDevice.default(for:AVMediaType.audio)

    do {

        if videoDevice== nil ||audioDevice== nil {

            throw NSError(domain:"device error", code:-1, userInfo: nil )

        }

        let captureSession =AVCaptureSession()

        // video inputを capture sessionに追加

        let videoInput = try AVCaptureDeviceInput(device: videoDevice!)

        captureSession.addInput(videoInput)

        // audio inputを capture sessionに追加

        let audioInput = try AVCaptureDeviceInput(device: audioDevice!)

        captureSession.addInput(audioInput)

        // max 30sec

        self .fileOutput.maxRecordedDuration=CMTimeMake(value:30, timescale:1)

        captureSession.addOutput(fileOutput)

        // プレビュー

        let videoLayer :AVCaptureVideoPreviewLayer=AVCaptureVideoPreviewLayer(session: captureSession)

        videoLayer.frame= self .view.bounds

        videoLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill

        self .view.layer.addSublayer(videoLayer)

        captureSession.startRunning()

        setUpButton()

    } catch {

        // エラー処理

    }

}

func setUpButton() {

    recordButton=UIButton(frame:CGRect(x:0,y:0,width:120,height:50))

    recordButton.backgroundColor = UIColor.gray

    recordButton.layer.masksToBounds = true

    recordButton.setTitle("録画开始", for:UIControl.State.normal)

    recordButton.layer.cornerRadius = 20.0

    recordButton.layer.position = CGPoint(x: self .view.bounds.width/2, y: self .view.bounds.height-50)

    recordButton.addTarget( self , action: #selector (ViewController.onClickRecordButton(sender:)), for: .touchUpInside)

    self .view.addSubview(recordButton)

}

@objc func onClickRecordButton(sender: UIButton) {

    if !isRecording{

        // 録画开始

        let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true )

        let documentsDirectory = paths[0] as String

        let filePath :String? ="\(documentsDirectory)/temp.mp4"

        let fileURL :NSURL=NSURL(fileURLWithPath: filePath!)

        fileOutput.startRecording(to: fileURL as URL, recordingDelegate: self )

        isRecording= true

        changeButtonColor(target:recordButton, color:UIColor.red)

        recordButton.setTitle("録画中", for: .normal)

    } else {

        // 録画终了

        fileOutput.stopRecording()

        isRecording= false

        changeButtonColor(target:recordButton, color:UIColor.gray)

        recordButton.setTitle("録画开始", for: .normal)

    }

}

func  changeButtonColor(target:UIButton, color:UIColor) {

    target.backgroundColor= color

}

func fileOutput( _ output:AVCaptureFileOutput, didFinishRecordingTo outputFileURL:URL, from connections: [AVCaptureConnection], error:Error?) {

    // ライブラリへ保存

    PHPhotoLibrary.shared().performChanges({

        PHAssetChangeRequest.creationRequestForAssetFromVideo(atFileURL: outputFileURL)

    }) { completed, error in

        if completed {

            print("Video is saved!")

        }

    }

}

}

如何用swift开发ios应用

苹果公司发布了一则视频教程,向开发者介绍如何使用 Swift 来开发应用。苹果通过公司的官方开发者博客公布了该视频,苹果 Swift 团队表示: 迄今为止 Swift 博客都主要讨论关于高级编程的话题,包括 Swift 语言的设计原则等。

iOS视频播放器(Swift)

iOS上除了一些第三方的播放器之外,我们一般常用的播放方式有使用:

一般简单的播放url可以使用网页播放的模式,有很多主流app的视频打开都是用的这种。不需要UI自定义的时候我们选择AVPlayerViewController,比较方便快捷。自定义需求较多时最好选用AVPlayer。

功能最全、自定义最高的播放器,也是使用最多得。使用起来较为复杂些。需导入AVKit控件

AVPlayer功能比较多,另外写了一篇文章:

AVPlayer播放器

只是让视频播放起来,没有暂停、停止、快进等等功能。

适用于简单的播放,不需要过多自定义的东西,使用比较简单,有两种方式,需要引入AVKit框架

(1)添加view

可以设置播放器的大小

(2)作为视图控制器弹窗

使用起来类似AVPlayerViewController的第一种方式。需要引入MediaPlayer框架(iOS9后被抛弃,使用AVPlayerViewController即可)

通知来进行一些操作的监听

使用起来类似AVPlayerViewController的第二种方式。需要引入MediaPlayer框架(iOS9后被抛弃,使用AVPlayerViewController即可)

swift视频播放器使用


标题名称:swift开发ios视频的简单介绍
浏览路径:http://cdweb.net/article/dssgepj.html