-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathAVPlayer+Android.swift
89 lines (72 loc) · 2.5 KB
/
AVPlayer+Android.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#if os(Android)
//
// JNIVideo.swift
// UIKit
//
// Created by Chris on 13.09.17.
// Copyright © 2017 flowkey. All rights reserved.
//
import JNI
public class AVPlayer: JNIObject {
public override static var className: String { "org.uikit.AVPlayer" }
public var onError: ((ExoPlaybackError) -> Void)?
public var onVideoReady: (() -> Void)?
public var onVideoEnded: (() -> Void)?
public var onVideoBuffering: (() -> Void)?
public convenience init(playerItem: AVPlayerItem) {
let parentView = JavaSDLView(getSDLView())
try! self.init(arguments: parentView, playerItem.asset)
globalAVPlayer = self
}
public func play() {
try! call("play")
}
public func pause() {
try! call("pause")
}
public func getCurrentTimeInMS() -> Int64 {
return try! call("getCurrentTimeInMilliseconds")
}
public func seek(to timeInMilliseconds: Int64) {
try! call("seekToTimeInMilliseconds", arguments: [timeInMilliseconds])
}
public var rate: Float {
get { return try! call("getPlaybackRate") }
set { try! call("setPlaybackRate", arguments: [newValue]) }
}
public var isMuted: Bool = false {
didSet {
let newVolume = isMuted ? 0.0 : 1.0
try! call("setVolume", arguments: [newVolume])
}
}
deinit {
try? call("cleanup")
}
public struct ExoPlaybackError: Error {
let type: Int // https://exoplayer.dev/doc/reference/com/google/android/exoplayer2/ExoPlaybackException.Type.html
let message: String
}
}
private weak var globalAVPlayer: AVPlayer?
@_cdecl("Java_org_uikit_AVPlayer_nativeOnVideoReady")
public func nativeOnVideoReady(env: UnsafeMutablePointer<JNIEnv>, cls: JavaObject) {
globalAVPlayer?.onVideoReady?()
}
@_cdecl("Java_org_uikit_AVPlayer_nativeOnVideoEnded")
public func nativeOnVideoEnded(env: UnsafeMutablePointer<JNIEnv>, cls: JavaObject) {
globalAVPlayer?.onVideoEnded?()
}
@_cdecl("Java_org_uikit_AVPlayer_nativeOnVideoBuffering")
public func nativeOnVideoBuffering(env: UnsafeMutablePointer<JNIEnv>, cls: JavaObject) {
globalAVPlayer?.onVideoBuffering?()
}
@_cdecl("Java_org_uikit_AVPlayer_nativeOnVideoError")
public func nativeOnVideoError(env: UnsafeMutablePointer<JNIEnv>, cls: JavaObject, type: JavaInt, message: JavaString) {
let error = AVPlayer.ExoPlaybackError(
type: Int(type),
message: (try? String(javaString: message)) ?? "N/A"
)
globalAVPlayer?.onError?(error)
}
#endif