Skip to content

Commit

Permalink
iOS Fixes and changes:
Browse files Browse the repository at this point in the history
- Issue #122
- Changed jeepCapVideoPlayerExit, it returns currentTime now
- Added a func to convert SRT subtitles to VTT in case there's any
  • Loading branch information
PhantomPainX committed Apr 12, 2023
1 parent f455ada commit 04b150b
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 19 deletions.
4 changes: 4 additions & 0 deletions dist/esm/definitions.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,10 @@ export interface capExitListener {
* Dismiss value true or false
*/
dismiss?: boolean;
/**
* Video current time when listener trigerred
*/
currentTime?: number;
}
export interface capVideoPlayerResult {
/**
Expand Down
7 changes: 4 additions & 3 deletions docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,7 @@ The listeners are attached to the plugin not anymore to the DOM document element

#### capExitListener

| Prop | Type | Description |
| ----------- | ------- | --------------------------- |
| **dismiss** | boolean | Dismiss value true or false |
| Prop | Type | Description |
| --------------- | ------- | ------------------------------------------ |
| **dismiss** | boolean | Dismiss value true or false |
| **currentTime** | number | Video current time when listener trigerred |
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,12 @@ extension CapacitorVideoPlayerPlugin {
// MARK: - playerFullscreenDismiss

@objc func playerFullscreenDismiss(notification: Notification) {
let info: [String: Any] = ["dismiss": true]

var currentTime: Double = 0.0
if let playerView = self.videoPlayerFullScreenView {
currentTime = playerView.getRealCurrentTime()
}
let info: [String: Any] = ["dismiss": true, "currentTime": currentTime]
DispatchQueue.main.async {
if self.mode == "fullscreen" {
if let vPFSV = self.videoPlayerFullScreenView {
Expand Down
20 changes: 6 additions & 14 deletions ios/Plugin/Extensions/CapacitorVideoPlayerPlugin.Observers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,6 @@ extension CapacitorVideoPlayerPlugin {
.addObserver(forName: .videoPathInternalReady, object: nil,
queue: nil, using: videoPathInternalReady)

willResignObserver =
NotificationCenter.default.addObserver(
forName: UIApplication.willResignActiveNotification,
object: nil, queue: nil) { (_) in
if !isInPIPMode &&
self.videoPlayerFullScreenView != nil {
// release video tracks
if let playerItem = self.videoPlayerFullScreenView?
.playerItem {
self.videoTrackEnable(playerItem: playerItem,
enable: false)
}
}
}
backgroundObserver =
NotificationCenter.default.addObserver(
forName: UIApplication.didEnterBackgroundNotification,
Expand All @@ -61,6 +47,12 @@ extension CapacitorVideoPlayerPlugin {
self.videoPlayerFullScreenView != nil {
self.videoPlayerFullScreenView?
.videoPlayer.player = nil

if let playerItem = self.videoPlayerFullScreenView?
.playerItem {
self.videoTrackEnable(playerItem: playerItem,
enable: false)
}
}
} else {
if self.videoPlayerFullScreenView != nil {
Expand Down
58 changes: 57 additions & 1 deletion ios/Plugin/VideoPlayer/FullScreenVideoPlayerView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import UIKit
import AVKit
import MediaPlayer

// swiftlint:disable file_length
// swiftlint:disable type_body_length
Expand Down Expand Up @@ -71,7 +72,13 @@ open class FullScreenVideoPlayerView: UIView {
// swiftlint:disable function_body_length
private func initialize() {
// Set SubTitles if any
if let subTitleUrl = self._stUrl {
if var subTitleUrl = self._stUrl {
//check if subtitle is .srt
if subTitleUrl.pathExtension == "srt" {
let vttUrl: URL = srtSubtitleToVtt(srtURL: self._stUrl!)
self._stUrl = vttUrl
subTitleUrl = vttUrl
}
var textStyle: [AVTextStyleRule] = []
if let opt = self._stOptions {
textStyle.append(contentsOf: self.setSubTitleStyle(options: opt))
Expand Down Expand Up @@ -350,6 +357,17 @@ open class FullScreenVideoPlayerView: UIView {
@objc func getCurrentTime() -> Double {
return self._currentTime
}
// This func will return the updated currentTime of player item
// getCurrentTime() is only updated when player plays, pauses, seek, etc
// the function is only used in playerFullscreenDismiss() Notification
public func getRealCurrentTime() -> Double {
if let item = self.playerItem {
let currentTime = CMTimeGetSeconds(item.currentTime())
return currentTime
} else {
return 0
}
}
@objc func setCurrentTime(time: Double) {
let seekTime: CMTime = CMTimeMake(value: Int64(time*1000), timescale: 1000)
self.player?.seek(to: seekTime)
Expand Down Expand Up @@ -409,6 +427,44 @@ open class FullScreenVideoPlayerView: UIView {
return []
}
}

private func srtSubtitleToVtt(srtURL: URL) -> URL {
guard let cachesURL = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first else {
fatalError("Couldn't get caches directory")
}
let vttFileName = UUID().uuidString + ".vtt"
let vttURL = cachesURL.appendingPathComponent(vttFileName)
let session = URLSession(configuration: .default)
let vttFolderURL = vttURL.deletingLastPathComponent()
do {
try FileManager.default.createDirectory(at: vttFolderURL, withIntermediateDirectories: true, attributes: nil)
} catch let error {
print("Creating folder error: ", error)
}
let task = session.dataTask(with: srtURL) { (data, response, error) in
guard let data = data, error == nil else {
print("Download failed: \(error?.localizedDescription ?? "ukn")")
return
}
do {
let tempSRTURL = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("subtitulos.srt")
try data.write(to: tempSRTURL)
let srtContent = try String(contentsOf: tempSRTURL, encoding: .utf8)
let vttContent = srtContent.replacingOccurrences(of: ",", with: ".")
let vttString = "WEBVTT\n\n" + vttContent
try vttString.write(toFile: vttURL.path, atomically: true, encoding: .utf8)
try FileManager.default.removeItem(at: tempSRTURL)

} catch let error {
print("Processing subs error: \(error)")
exit(1)
}

}

task.resume()
return vttURL
}

}

Expand Down
4 changes: 4 additions & 0 deletions src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,10 @@ export interface capExitListener {
* Dismiss value true or false
*/
dismiss?: boolean;
/**
* Video current time when listener trigerred
*/
currentTime?: number;
}
export interface capVideoPlayerResult {
/**
Expand Down

0 comments on commit 04b150b

Please sign in to comment.