AVPlayer duration changes when passed to differrent controller












3















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
Video Paused In Portrait



LANDSCAPE
When rotated to landscape



When orientation changes, video moves forward even on pause state.



Thanks for help in advance.










share|improve this question





























    3















    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
    Video Paused In Portrait



    LANDSCAPE
    When rotated to landscape



    When orientation changes, video moves forward even on pause state.



    Thanks for help in advance.










    share|improve this question



























      3












      3








      3


      1






      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
      Video Paused In Portrait



      LANDSCAPE
      When rotated to landscape



      When orientation changes, video moves forward even on pause state.



      Thanks for help in advance.










      share|improve this question
















      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
      Video Paused In Portrait



      LANDSCAPE
      When rotated to landscape



      When orientation changes, video moves forward even on pause state.



      Thanks for help in advance.







      ios swift video avplayer avplayerviewcontroller






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 22 '18 at 8:14







      bhatejaud

















      asked Nov 20 '18 at 13:53









      bhatejaudbhatejaud

      6112




      6112
























          1 Answer
          1






          active

          oldest

          votes


















          2





          +25









          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.






          share|improve this answer
























          • 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











          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
          });


          }
          });














          draft saved

          draft discarded


















          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









          2





          +25









          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.






          share|improve this answer
























          • 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
















          2





          +25









          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.






          share|improve this answer
























          • 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














          2





          +25







          2





          +25



          2




          +25





          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.






          share|improve this answer













          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.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          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



















          • 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




















          draft saved

          draft discarded




















































          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.




          draft saved


          draft discarded














          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





















































          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







          Popular posts from this blog

          Tonle Sap (See)

          I get strange results when I access the Sqlitedatabase with Unity C# via XAMPP

          Guatemaltekische Davis-Cup-Mannschaft