TVOS的简单应用 - 但对我来说并不简单。循环我的电影

发布于 2025-01-24 17:02:54 字数 1140 浏览 6 评论 0原文

我在编码方面完全是新手,并且正在尝试为我自己制作一个小应用程序。 这是我想要的,但我不知道如何制作电影循环?试图到处看,但对我来说很难。

//
//  ContentView.swift
//  VideoWallPaperV5
//
//  Created by Me on 13/04/2022.
//


import SwiftUI
import AVKit


struct ContentView: View {
    
    let player = AVPlayer(url: Bundle.main.url(forResource: "small_movie", withExtension: "mp4")!)

    
    var body: some View {
        ZStack {
                VideoPlayer(player: player)
                .scaledToFill()
                    .edgesIgnoringSafeArea(.all)
            
                    .onAppear {
                        player.play()
//                        player.isMuted = true
                    }

            
            VStack{
                Text("Airplay to kitchen")
                    .multilineTextAlignment(.leading)
                    .padding()
                    .background(Color.black.cornerRadius(10).opacity(0.6))
                    .position(x: 200, y: 100)
                    .font(Font.custom("Hill", size: 33))
                    .lineSpacing(15)
                    .foregroundColor(Color.white)
    Spacer()
}

            }
    }
}

I'm total newbie in coding and are trying to make a small app for my own.
This is working as I want but I've no idea how to make the movie loop? Tried to look everywhere but to difficult for me.

//
//  ContentView.swift
//  VideoWallPaperV5
//
//  Created by Me on 13/04/2022.
//


import SwiftUI
import AVKit


struct ContentView: View {
    
    let player = AVPlayer(url: Bundle.main.url(forResource: "small_movie", withExtension: "mp4")!)

    
    var body: some View {
        ZStack {
                VideoPlayer(player: player)
                .scaledToFill()
                    .edgesIgnoringSafeArea(.all)
            
                    .onAppear {
                        player.play()
//                        player.isMuted = true
                    }

            
            VStack{
                Text("Airplay to kitchen")
                    .multilineTextAlignment(.leading)
                    .padding()
                    .background(Color.black.cornerRadius(10).opacity(0.6))
                    .position(x: 200, y: 100)
                    .font(Font.custom("Hill", size: 33))
                    .lineSpacing(15)
                    .foregroundColor(Color.white)
    Spacer()
}

            }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吻风 2025-01-31 17:02:54

Avplayerlooper提供了一个简单的接口来循环单个AvplayerItem。您可以通过将其传递给您的AvqueEplayer来创建一个播放器循环器,并通过传递对Avplayer的引用来创建该播放器。

查看Apples文档:

avplayerlooper

avqueueplayer

AVPlayerLooper provides a simple interface to loop a single AVPlayerItem. You create a player looper by passing it a reference to your AVQueuePlayer which you create by passing a reference to your AVPlayer.

Check out Apples documentation:

AvPlayerLooper

AVQueuePlayer

眼眸印温柔 2025-01-31 17:02:54

如果要循环视频ITITEM,则可能应该使用Avqueueplayer和Avplayerlooper,因为它是完成任务的最合适方法。

但是,如果您坚持使用Avplayer及其界面,则需要订阅nsnotification.name.name.avplayeritemdidplaytoendtime

并通过寻求定位0.0并使用Play()

Kinda pseudocode看起来像这样:

NotificationCenter.default.addObserver(self,
                                       selector: #selector(itemPlayToEndTime),
                                       name: NSNotification.Name.AVPlayerItemDidPlayToEndTime)


private func itemPlayToEndTime() {
    let time = CMTime(seconds: 0.0, preferredTimescale: 1)
    self.player.seek(to: time) { [weak self] completed in 
        guard completed else { return }
        self?.player.play() 
}

但是我强烈建议您了解使用AvqueEplayer。

If you want to loop your videoItem you should probably use AVQueuePlayer and AVPlayerLooper as it is the most proper way for your task.

But if you insist on using only AVPlayer and its interface you need to subscribe to NSNotification.Name.AVPlayerItemDidPlayToEndTime

And handle this notification by seeking to position 0.0 and triggering playback with play()

kinda pseudocode would look like this:

NotificationCenter.default.addObserver(self,
                                       selector: #selector(itemPlayToEndTime),
                                       name: NSNotification.Name.AVPlayerItemDidPlayToEndTime)


private func itemPlayToEndTime() {
    let time = CMTime(seconds: 0.0, preferredTimescale: 1)
    self.player.seek(to: time) { [weak self] completed in 
        guard completed else { return }
        self?.player.play() 
}

but I strongly recommend you learn about using AVQueuePlayer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文