AVPlayer duration changes when passed to differrent controller
I have a controller with a AVPlayer in collection view cell. When orientation changes to Landscape, the player should get FullScreen.
For this I am presenting an AVPlayerController with same instance of player in Collection View Cell. The video works fine when it is rotated in playing mode.
However, when video is paused and I change orientation to Landscape, the frame at current moment changes i.e video moves forward.
I have tested, even when the orientation is kept same, when player is passed, the duration skips few seconds.
Here is the code:
In ViewController where cell is present.
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)
guard let videoCell = contentGalleryController.curatorVideoCell else {return}
if UIDevice.current.orientation == .landscapeLeft || UIDevice.current.orientation == .landscapeRight {
let player = videoCell.getVideoPlayer
playerViewController = (storyboard!.instantiateViewController(withIdentifier: "FullScreenPlayerController") as! FullScreenPlayerController)
playerViewController?.player = player
playerViewController?.didPlayedToEnd = videoCell.isVideoFinishedPlaying ?? false
playerViewController?.isMuteTurnedOn = player.isMuted
let wasVideoPlaying: Bool = player.isPlaying
present(playerViewController!, animated: false){
if wasVideoPlaying {
player.play()
}
}
}
else {
videoCell._setMuteIcon()
videoCell._setPlayPauseIcon()
playerViewController?.dismiss(animated: false, completion: nil)
}
}
In FullscreenPlayer View Controller
override func viewDidLoad() {
super.viewDidLoad()
setPlayPauseIcon()
setMuteIcon()
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(customControlViewTapped))
customView.addGestureRecognizer(tapGesture)
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
if !view.subviews.contains(customView) {
customView.frame = view.bounds
view.addSubview(customView)
}
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
NotificationCenter.default.addObserver(self, selector: #selector(playerDidPlayToEnd), name: Notification.Name.AVPlayerItemDidPlayToEndTime, object: player?.currentItem)
}
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
NotificationCenter.default.removeObserver(self)
}
I am doing nothing else in controller.
Screenshots:
Portrait

LANDSCAPE

When orientation changes, video moves forward even on pause state.
Thanks for help in advance.
ios swift video avplayer avplayerviewcontroller
add a comment |
I have a controller with a AVPlayer in collection view cell. When orientation changes to Landscape, the player should get FullScreen.
For this I am presenting an AVPlayerController with same instance of player in Collection View Cell. The video works fine when it is rotated in playing mode.
However, when video is paused and I change orientation to Landscape, the frame at current moment changes i.e video moves forward.
I have tested, even when the orientation is kept same, when player is passed, the duration skips few seconds.
Here is the code:
In ViewController where cell is present.
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)
guard let videoCell = contentGalleryController.curatorVideoCell else {return}
if UIDevice.current.orientation == .landscapeLeft || UIDevice.current.orientation == .landscapeRight {
let player = videoCell.getVideoPlayer
playerViewController = (storyboard!.instantiateViewController(withIdentifier: "FullScreenPlayerController") as! FullScreenPlayerController)
playerViewController?.player = player
playerViewController?.didPlayedToEnd = videoCell.isVideoFinishedPlaying ?? false
playerViewController?.isMuteTurnedOn = player.isMuted
let wasVideoPlaying: Bool = player.isPlaying
present(playerViewController!, animated: false){
if wasVideoPlaying {
player.play()
}
}
}
else {
videoCell._setMuteIcon()
videoCell._setPlayPauseIcon()
playerViewController?.dismiss(animated: false, completion: nil)
}
}
In FullscreenPlayer View Controller
override func viewDidLoad() {
super.viewDidLoad()
setPlayPauseIcon()
setMuteIcon()
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(customControlViewTapped))
customView.addGestureRecognizer(tapGesture)
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
if !view.subviews.contains(customView) {
customView.frame = view.bounds
view.addSubview(customView)
}
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
NotificationCenter.default.addObserver(self, selector: #selector(playerDidPlayToEnd), name: Notification.Name.AVPlayerItemDidPlayToEndTime, object: player?.currentItem)
}
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
NotificationCenter.default.removeObserver(self)
}
I am doing nothing else in controller.
Screenshots:
Portrait

LANDSCAPE

When orientation changes, video moves forward even on pause state.
Thanks for help in advance.
ios swift video avplayer avplayerviewcontroller
add a comment |
I have a controller with a AVPlayer in collection view cell. When orientation changes to Landscape, the player should get FullScreen.
For this I am presenting an AVPlayerController with same instance of player in Collection View Cell. The video works fine when it is rotated in playing mode.
However, when video is paused and I change orientation to Landscape, the frame at current moment changes i.e video moves forward.
I have tested, even when the orientation is kept same, when player is passed, the duration skips few seconds.
Here is the code:
In ViewController where cell is present.
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)
guard let videoCell = contentGalleryController.curatorVideoCell else {return}
if UIDevice.current.orientation == .landscapeLeft || UIDevice.current.orientation == .landscapeRight {
let player = videoCell.getVideoPlayer
playerViewController = (storyboard!.instantiateViewController(withIdentifier: "FullScreenPlayerController") as! FullScreenPlayerController)
playerViewController?.player = player
playerViewController?.didPlayedToEnd = videoCell.isVideoFinishedPlaying ?? false
playerViewController?.isMuteTurnedOn = player.isMuted
let wasVideoPlaying: Bool = player.isPlaying
present(playerViewController!, animated: false){
if wasVideoPlaying {
player.play()
}
}
}
else {
videoCell._setMuteIcon()
videoCell._setPlayPauseIcon()
playerViewController?.dismiss(animated: false, completion: nil)
}
}
In FullscreenPlayer View Controller
override func viewDidLoad() {
super.viewDidLoad()
setPlayPauseIcon()
setMuteIcon()
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(customControlViewTapped))
customView.addGestureRecognizer(tapGesture)
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
if !view.subviews.contains(customView) {
customView.frame = view.bounds
view.addSubview(customView)
}
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
NotificationCenter.default.addObserver(self, selector: #selector(playerDidPlayToEnd), name: Notification.Name.AVPlayerItemDidPlayToEndTime, object: player?.currentItem)
}
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
NotificationCenter.default.removeObserver(self)
}
I am doing nothing else in controller.
Screenshots:
Portrait

LANDSCAPE

When orientation changes, video moves forward even on pause state.
Thanks for help in advance.
ios swift video avplayer avplayerviewcontroller
I have a controller with a AVPlayer in collection view cell. When orientation changes to Landscape, the player should get FullScreen.
For this I am presenting an AVPlayerController with same instance of player in Collection View Cell. The video works fine when it is rotated in playing mode.
However, when video is paused and I change orientation to Landscape, the frame at current moment changes i.e video moves forward.
I have tested, even when the orientation is kept same, when player is passed, the duration skips few seconds.
Here is the code:
In ViewController where cell is present.
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)
guard let videoCell = contentGalleryController.curatorVideoCell else {return}
if UIDevice.current.orientation == .landscapeLeft || UIDevice.current.orientation == .landscapeRight {
let player = videoCell.getVideoPlayer
playerViewController = (storyboard!.instantiateViewController(withIdentifier: "FullScreenPlayerController") as! FullScreenPlayerController)
playerViewController?.player = player
playerViewController?.didPlayedToEnd = videoCell.isVideoFinishedPlaying ?? false
playerViewController?.isMuteTurnedOn = player.isMuted
let wasVideoPlaying: Bool = player.isPlaying
present(playerViewController!, animated: false){
if wasVideoPlaying {
player.play()
}
}
}
else {
videoCell._setMuteIcon()
videoCell._setPlayPauseIcon()
playerViewController?.dismiss(animated: false, completion: nil)
}
}
In FullscreenPlayer View Controller
override func viewDidLoad() {
super.viewDidLoad()
setPlayPauseIcon()
setMuteIcon()
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(customControlViewTapped))
customView.addGestureRecognizer(tapGesture)
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
if !view.subviews.contains(customView) {
customView.frame = view.bounds
view.addSubview(customView)
}
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
NotificationCenter.default.addObserver(self, selector: #selector(playerDidPlayToEnd), name: Notification.Name.AVPlayerItemDidPlayToEndTime, object: player?.currentItem)
}
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
NotificationCenter.default.removeObserver(self)
}
I am doing nothing else in controller.
Screenshots:
Portrait

LANDSCAPE

When orientation changes, video moves forward even on pause state.
Thanks for help in advance.
ios swift video avplayer avplayerviewcontroller
ios swift video avplayer avplayerviewcontroller
edited Nov 22 '18 at 8:14
bhatejaud
asked Nov 20 '18 at 13:53
bhatejaudbhatejaud
6112
6112
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
You should try capturing the currentTime and then use the seek(to time: CMTime) method on the player to start at that exact time. I am not sure exactly why this is happening to you, but I think this would get you the result you are looking for.
I've tried seeking but I am unable to seek to exact pos. because the difference is in milliseconds.
– bhatejaud
Nov 28 '18 at 13:11
are you familiar with how CMTime works? You can provide an exact time with it. developer.apple.com/documentation/coremedia/cmtime-u58
– jacob bullock
Nov 29 '18 at 1:43
1
if u get player.currentTime when orientation changes, you can seek that time. for precise seek use seek(to:toleranceBefore:toleranceAfter:) with kCMTimeZero for both tolerances instead.
– ugur
Dec 2 '18 at 23:50
I am using tolerance too, but there is slight displacement of frames when moved from portrait to landscape.
– bhatejaud
Dec 6 '18 at 16:18
@bhatejaud can you provide updated code where you are capturing the time and then using it to play?
– jacob bullock
Dec 8 '18 at 3:34
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53394544%2favplayer-duration-changes-when-passed-to-differrent-controller%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You should try capturing the currentTime and then use the seek(to time: CMTime) method on the player to start at that exact time. I am not sure exactly why this is happening to you, but I think this would get you the result you are looking for.
I've tried seeking but I am unable to seek to exact pos. because the difference is in milliseconds.
– bhatejaud
Nov 28 '18 at 13:11
are you familiar with how CMTime works? You can provide an exact time with it. developer.apple.com/documentation/coremedia/cmtime-u58
– jacob bullock
Nov 29 '18 at 1:43
1
if u get player.currentTime when orientation changes, you can seek that time. for precise seek use seek(to:toleranceBefore:toleranceAfter:) with kCMTimeZero for both tolerances instead.
– ugur
Dec 2 '18 at 23:50
I am using tolerance too, but there is slight displacement of frames when moved from portrait to landscape.
– bhatejaud
Dec 6 '18 at 16:18
@bhatejaud can you provide updated code where you are capturing the time and then using it to play?
– jacob bullock
Dec 8 '18 at 3:34
add a comment |
You should try capturing the currentTime and then use the seek(to time: CMTime) method on the player to start at that exact time. I am not sure exactly why this is happening to you, but I think this would get you the result you are looking for.
I've tried seeking but I am unable to seek to exact pos. because the difference is in milliseconds.
– bhatejaud
Nov 28 '18 at 13:11
are you familiar with how CMTime works? You can provide an exact time with it. developer.apple.com/documentation/coremedia/cmtime-u58
– jacob bullock
Nov 29 '18 at 1:43
1
if u get player.currentTime when orientation changes, you can seek that time. for precise seek use seek(to:toleranceBefore:toleranceAfter:) with kCMTimeZero for both tolerances instead.
– ugur
Dec 2 '18 at 23:50
I am using tolerance too, but there is slight displacement of frames when moved from portrait to landscape.
– bhatejaud
Dec 6 '18 at 16:18
@bhatejaud can you provide updated code where you are capturing the time and then using it to play?
– jacob bullock
Dec 8 '18 at 3:34
add a comment |
You should try capturing the currentTime and then use the seek(to time: CMTime) method on the player to start at that exact time. I am not sure exactly why this is happening to you, but I think this would get you the result you are looking for.
You should try capturing the currentTime and then use the seek(to time: CMTime) method on the player to start at that exact time. I am not sure exactly why this is happening to you, but I think this would get you the result you are looking for.
answered Nov 27 '18 at 23:29
jacob bullockjacob bullock
596313
596313
I've tried seeking but I am unable to seek to exact pos. because the difference is in milliseconds.
– bhatejaud
Nov 28 '18 at 13:11
are you familiar with how CMTime works? You can provide an exact time with it. developer.apple.com/documentation/coremedia/cmtime-u58
– jacob bullock
Nov 29 '18 at 1:43
1
if u get player.currentTime when orientation changes, you can seek that time. for precise seek use seek(to:toleranceBefore:toleranceAfter:) with kCMTimeZero for both tolerances instead.
– ugur
Dec 2 '18 at 23:50
I am using tolerance too, but there is slight displacement of frames when moved from portrait to landscape.
– bhatejaud
Dec 6 '18 at 16:18
@bhatejaud can you provide updated code where you are capturing the time and then using it to play?
– jacob bullock
Dec 8 '18 at 3:34
add a comment |
I've tried seeking but I am unable to seek to exact pos. because the difference is in milliseconds.
– bhatejaud
Nov 28 '18 at 13:11
are you familiar with how CMTime works? You can provide an exact time with it. developer.apple.com/documentation/coremedia/cmtime-u58
– jacob bullock
Nov 29 '18 at 1:43
1
if u get player.currentTime when orientation changes, you can seek that time. for precise seek use seek(to:toleranceBefore:toleranceAfter:) with kCMTimeZero for both tolerances instead.
– ugur
Dec 2 '18 at 23:50
I am using tolerance too, but there is slight displacement of frames when moved from portrait to landscape.
– bhatejaud
Dec 6 '18 at 16:18
@bhatejaud can you provide updated code where you are capturing the time and then using it to play?
– jacob bullock
Dec 8 '18 at 3:34
I've tried seeking but I am unable to seek to exact pos. because the difference is in milliseconds.
– bhatejaud
Nov 28 '18 at 13:11
I've tried seeking but I am unable to seek to exact pos. because the difference is in milliseconds.
– bhatejaud
Nov 28 '18 at 13:11
are you familiar with how CMTime works? You can provide an exact time with it. developer.apple.com/documentation/coremedia/cmtime-u58
– jacob bullock
Nov 29 '18 at 1:43
are you familiar with how CMTime works? You can provide an exact time with it. developer.apple.com/documentation/coremedia/cmtime-u58
– jacob bullock
Nov 29 '18 at 1:43
1
1
if u get player.currentTime when orientation changes, you can seek that time. for precise seek use seek(to:toleranceBefore:toleranceAfter:) with kCMTimeZero for both tolerances instead.
– ugur
Dec 2 '18 at 23:50
if u get player.currentTime when orientation changes, you can seek that time. for precise seek use seek(to:toleranceBefore:toleranceAfter:) with kCMTimeZero for both tolerances instead.
– ugur
Dec 2 '18 at 23:50
I am using tolerance too, but there is slight displacement of frames when moved from portrait to landscape.
– bhatejaud
Dec 6 '18 at 16:18
I am using tolerance too, but there is slight displacement of frames when moved from portrait to landscape.
– bhatejaud
Dec 6 '18 at 16:18
@bhatejaud can you provide updated code where you are capturing the time and then using it to play?
– jacob bullock
Dec 8 '18 at 3:34
@bhatejaud can you provide updated code where you are capturing the time and then using it to play?
– jacob bullock
Dec 8 '18 at 3:34
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53394544%2favplayer-duration-changes-when-passed-to-differrent-controller%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown