如何使自定义AVPlayer支持SharePlay调整坐标或播放暂停控制



这个应用程序是用SwiftUI编写的,我也查阅了苹果的官方文档和一些第三方网站。所以我写了如下代码

这是调用播放器和shareplay相关逻辑的Swift代码

import SwiftUI
import AVFoundation
import AVKit
import GroupActivities
import Combine
struct EpisodeDetail: View {
@State var episode: CommonResponse<Episode>
@State var videoPlayer: CustomVideoPlayer
@State private var groupSession: GroupSession<EpisodeActivity>?
@State private var subscriptions = Set<AnyCancellable>()

var body: some View {
VStack {
videoPlayer
.transition(.move(edge: .bottom))
.edgesIgnoringSafeArea(.all)

ScrollView {
VStack(alignment: .center) {
Button {
prepareToPlay(episode.attributes)
} label: {
Label("同播共享", systemImage: "shareplay")
}
.buttonStyle(.bordered)
.cornerRadius(20)
}

VStack(alignment: .leading, spacing: 20) {
Text(episode.attributes.title)
.font(.title2)
.fontWeight(.bold)

Text(episode.attributes.description)
.font(.body)
.foregroundColor(.gray)
}
}
.padding(12)
}
.task {
for await session in EpisodeActivity.sessions() {
configureGroupSession(session)
}
}
}
private func configureGroupSession(_ session: GroupSession<EpisodeActivity>) {
groupSession = session
videoPlayer.player.playbackCoordinator.coordinateWithSession(session)
session.$state
.sink { state in
if case .invalidated = state {
groupSession = nil
subscriptions.removeAll()
}
}
.store(in: &subscriptions)
session.$activity
.sink { activity in
print("Activity Changed: (activity.metadata.title ?? "No title for this shitty video LOL")")
}
.store(in: &subscriptions)
session.join()
}
private func prepareToPlay(_ playerEpisodeData: Episode) {
let activity = EpisodeActivity(episode: playerEpisodeData)
Task {
switch await activity.prepareForActivation() {
case .activationDisabled:
videoPlayer.player.replaceCurrentItem(with: AVPlayerItem(url: playerEpisodeData.videoUrl))
break
case .activationPreferred:
videoPlayer.player.replaceCurrentItem(with: AVPlayerItem(url: playerEpisodeData.videoUrl))
_ = try await activity.activate()
case .cancelled:
break
@unknown default:
break
}
}
}
}
下面的代码是一个自定义的AVPlayer
import SwiftUI
import AVFoundation
import AVKit
struct CustomVideoPlayer: UIViewControllerRepresentable {
@State var videoUrl: URL

var player: AVPlayer {
return AVPlayer(url: videoUrl)
}

func updateUIViewController(_ playerController: AVPlayerViewController, context: Context) {
playerController.modalPresentationStyle = .fullScreen
playerController.allowsPictureInPicturePlayback = true
playerController.canStartPictureInPictureAutomaticallyFromInline = true
playerController.player = player
}

func makeUIViewController(context: Context) -> AVPlayerViewController {
return AVPlayerViewController()
}
}

正确弹出SharePlay提示符并正确显示当前活动

这是一个SharePlay弹出框的截图,我不能插入图像,所以我必须插入一个链接

https://i.stack.imgur.com/QNqBE.jpg

但是当我对播放器做一些事情时,比如暂停,或者调整播放进度,另一个iPhone不同步

那我该怎么办呢?

感激不尽:-)

解决了,通过在AVPlayer中添加@State注释,谢谢大家,这个问题已经结束了

struct CustomVideoPlayer: UIViewControllerRepresentable {
@State var player: AVPlayer? = nil

func updateUIViewController(_ playerController: AVPlayerViewController, context: Context) {
playerController.modalPresentationStyle = .fullScreen
playerController.allowsPictureInPicturePlayback = true
playerController.canStartPictureInPictureAutomaticallyFromInline = true
playerController.player = player
}

func makeUIViewController(context: Context) -> AVPlayerViewController {
return AVPlayerViewController()
}
}

最新更新