본문 바로가기
Language/Swift

SwiftUI에서 VideoPlayer 이용하기

by IFLA 2022. 12. 31.

 

iOS14 부터 SwiftUI 에 비디오를 추가해주는 VideoPlayer 생성자가 추가됐다. VideoPlayer 를 사용하게 되면 로컬 또는 원격 URL 에서 영화 및 동영상을 재생할 수 있다.

AVKit 프레임 워크에서 제공하기 때문에 AVKit 를 기본적으로 추가해야한다.

 

기본 코드

import SwiftUI
import AVKit

struct ContentView: View {
    var body: some View {
        VideoPlayer(player: AVPlayer(url: Bundle.main.url(forResource: "sample", withExtension: "mp4")!))
    }
}

 

실행 화면

 

  • 프로젝트에 동영상이 있으면 위의 예제와 같이 사용하면 된다.
  • URL 을 이용해서 원격으로 비디오를 재생하려면 URL 을 사용해야한다.

 

struct ContentView: View {
	var body: some View {
		VideoPlayer(player: AVPlayer(url: URL(string: "<http://동영상주소.com>")!))
	}
}

 


개발자 Document

Apple Developer Documentation

 

Apple Developer Documentation

 

developer.apple.com

 

댓글


\