![](/style/images/good.png)
![](/style/images/bad.png)
SwiftUI Video Player Tutorial
source link: https://www.ioscreator.com/tutorials/swiftui-video-player-tutorial
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
The VideoPlayer is a view which embeds a AVPlayer including all controls for playblack. In this tutorial a video is loaded from a URL and played in a a VideoPlayer view. This tutorial is built for iOS 14 with Xcode 12 which can be download at the Apple developer portal.
Open Xcode and either click Create a new Xcode project in Xcode’s startup window, or choose File > New > Project. In the template selector, select iOS as the platform, select App template in the Application section and then click Next.
Enter SwiftUIVideoPlayerTutorial as the Product Name, select SwiftUI as Interface, SwiftUI App as Life Cycle and Swift as Language. Deselect the Include Tests checkbox and click Next. Choose a location to save the project on your Mac.
In the canvas, click Resume to display the preview. If the canvas isn’t visible, select Editor > Editor and Canvas to show it.
In the Project navigator, click to select ContentView.swift. Change the code inside the ContentView struct to
import SwiftUI import AVKit struct ContentView: View { private let player = AVPlayer(url: URL(string: "https://www.learningcontainer.com/wp-content/uploads/2020/05/sample-mp4-file.mp4")!) var body: some View { VideoPlayer(player: player) .onAppear() { player.play() } } }
The player property creates a new player to play a video resource referenced by the given URL. Inside The body View the VideoPlayer is displayed. The onappear modifier is used to auto-play the video using the play() method of AVPlayer.
Go to the Preview pane and select the Live Preview button, the video will start playing.
The source code of the SwiftUIVideoPlayerTutorial can be downloaded at the ioscreator repository on Github.
Recommend
-
62
At WWDC19 Apple introduces SwiftUI, which is a native declarative UI framework. SwiftUI let developers declare what they want to put in the UI and SwiftUI makes sure all rules are enforced. SwiftUI is also cross-platform...
-
99
The text view is a view that displays one ore more lines of read-only text. In this tutorial the text views will be modified with different type of modifiers.SwiftUI requires Xcode 11 and MacOS Catalina, for which the Be...
-
50
State is a value, or a set of values, that can change and that affects a view’s behavior, content, or layout. In this tutorial a toggle is created which will determine if a user is logged in or not. .SwiftUI requires Xco...
-
48
An Alert displays an alert message to the user. The alert will be displayed using the presentation method which can added to a view. In this tutorial we will display an alert when the user clicks a button.SwiftUI require...
-
43
At the current state of SwiftUI not all views are implemented yet. UIKit views can be added in the Swift hierarchy. In this tutorial a url will be displayed in a web view.SwiftUI requires Xcode 11 and MacOS Catalina, for...
-
29
A segmented control is a set of two or more segments, each of which functions as a mutually exclusive button. In this tutorial a segmented control will be displayed and a text view will be updated with the contents of th...
-
29
A picker includes one or more scrollable lists of values, each of which has a single selected value. In this tutorial a picker is displayed containing colors.SwiftUI requires Xcode 11 and MacOS Catalina, for which the Be...
-
34
A Slider object is a visual control used to select a single value from a continuous range of values. Sliders are always displayed as horizontal bars. An indicator, or thumb indicates the current value of the slider and c...
-
35
An action sheet is a specific style of alert that appears in response to a control or action, and presents a set of two or more choices related to the current context. In this tutorial an action sheet is presented contai...
-
2
Introduction Last week I posted my calculator made in the style of Neomorphism. Today I decided to continue posting my similar works. Therefore, I want to present to you my music player, also made in the style of neomorphism.
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK