UISlider jumps when updating for AVPlayerAdd custom controls to AVPlayer in swiftHow can I make a UITextField move up when the keyboard is present - on starting to edit?iPhone AVPlayer - Execute Code in BackgroundAVPlayer not reaching the end of playbackAVPlayer not synchronizedStreaming multiple music with AVPlayeriOS - How to make a UISlider into a “Scrubber / Seekbar” for video playback?Setting Slider Value to Set SeekToTime in AVPlayerAVaudioplayer audio player progess as Uislider crashesPlaying AVPlayerItem CurrentTime in Seconds Return Inconsistent ValuesUISlider & UILabels on custom tableviewcell not updating with avaudioplayer until audio is played a second time

Is there an wasy way to program in Tikz something like the one in the image?

Female=gender counterpart?

Why is delta-v is the most useful quantity for planning space travel?

Hostile work environment after whistle-blowing on coworker and our boss. What do I do?

Simple image editor tool to draw a simple box/rectangle in an existing image

Can I create an upright 7-foot × 5-foot wall with the Minor Illusion spell?

Does "Dominei" mean something?

Lightning Web Component - do I need to track changes for every single input field in a form

For airliners, what prevents wing strikes on landing in bad weather?

Freedom of speech and where it applies

Is there a problem with hiding "forgot password" until it's needed?

Are taller landing gear bad for aircraft, particulary large airliners?

Is it okay / does it make sense for another player to join a running game of Munchkin?

Science Fiction story where a man invents a machine that can help him watch history unfold

What is the oldest known work of fiction?

Why are all the doors on Ferenginar (the Ferengi home world) far shorter than the average Ferengi?

Can a Bard use an arcane focus?

Why isn't KTEX's runway designation 10/28 instead of 9/27?

Could solar power be utilized and substitute coal in the 19th century?

Is a naturally all "male" species possible?

Can the electrostatic force be infinite in magnitude?

My boss asked me to take a one-day class, then signs it up as a day off

Visiting the UK as unmarried couple

The One-Electron Universe postulate is true - what simple change can I make to change the whole universe?



UISlider jumps when updating for AVPlayer


Add custom controls to AVPlayer in swiftHow can I make a UITextField move up when the keyboard is present - on starting to edit?iPhone AVPlayer - Execute Code in BackgroundAVPlayer not reaching the end of playbackAVPlayer not synchronizedStreaming multiple music with AVPlayeriOS - How to make a UISlider into a “Scrubber / Seekbar” for video playback?Setting Slider Value to Set SeekToTime in AVPlayerAVaudioplayer audio player progess as Uislider crashesPlaying AVPlayerItem CurrentTime in Seconds Return Inconsistent ValuesUISlider & UILabels on custom tableviewcell not updating with avaudioplayer until audio is played a second time













3















I try to implement simple player with UISlider to indicate at what time is current audio file.



enter image description here



In code I have added two observers:



 slider.rx.value.subscribe(onNext: value in
let totalTime = Float(CMTimeGetSeconds(self.player.currentItem!.duration))
let seconds = value * totalTime
let time = CMTime(seconds: Double(seconds), preferredTimescale: CMTimeScale(NSEC_PER_SEC))
self.player.seek(to: time)
).disposed(by: bag)
let interval = CMTime(seconds: 0.1, preferredTimescale: CMTimeScale(NSEC_PER_SEC))
player.addPeriodicTimeObserver(forInterval: interval, queue: nil) [weak self] time in
self?.updateSlider(with: time)



with one private function:



private func updateSlider(with time: CMTime) 
let currentTime = CMTimeGetSeconds(time)
var totalTime = CMTimeGetSeconds(player.currentItem!.duration)
if totalTime.isNaN
totalTime = 0

startLabel.text = Int(currentTime).descriptiveDuration
endLabel.text = Int(totalTime).descriptiveDuration
slider.value = Float(currentTime / totalTime)



When audio plays, everything is fine and slider is pretty much updated. The problem occurs when I try to move slider manually while audio is playing, then it jumps. Why?



UPDATE:



I know why actually. Because I update it twice: manually and from player observer, but how to prevent from this behaviour? I have no idea;) please, help.










share|improve this question
























  • see this for help : stackoverflow.com/questions/43062870/…

    – Anbu.Karthik
    Mar 8 at 8:46






  • 1





    How about case handling within addPeriodicTimeObserver block to return immediately when slider is being touched?

    – staticVoidMan
    Mar 8 at 9:13












  • @staticVoidMan how would you like to implement it?;) Please answer your idea and I will try it.

    – Bartłomiej Semańczyk
    Mar 8 at 9:27






  • 1





    @BartłomiejSemańczyk Well, the basic idea is to prevent slider from updating when it is being touched/moved. I will chalk up a sample example :)

    – staticVoidMan
    Mar 8 at 9:47















3















I try to implement simple player with UISlider to indicate at what time is current audio file.



enter image description here



In code I have added two observers:



 slider.rx.value.subscribe(onNext: value in
let totalTime = Float(CMTimeGetSeconds(self.player.currentItem!.duration))
let seconds = value * totalTime
let time = CMTime(seconds: Double(seconds), preferredTimescale: CMTimeScale(NSEC_PER_SEC))
self.player.seek(to: time)
).disposed(by: bag)
let interval = CMTime(seconds: 0.1, preferredTimescale: CMTimeScale(NSEC_PER_SEC))
player.addPeriodicTimeObserver(forInterval: interval, queue: nil) [weak self] time in
self?.updateSlider(with: time)



with one private function:



private func updateSlider(with time: CMTime) 
let currentTime = CMTimeGetSeconds(time)
var totalTime = CMTimeGetSeconds(player.currentItem!.duration)
if totalTime.isNaN
totalTime = 0

startLabel.text = Int(currentTime).descriptiveDuration
endLabel.text = Int(totalTime).descriptiveDuration
slider.value = Float(currentTime / totalTime)



When audio plays, everything is fine and slider is pretty much updated. The problem occurs when I try to move slider manually while audio is playing, then it jumps. Why?



UPDATE:



I know why actually. Because I update it twice: manually and from player observer, but how to prevent from this behaviour? I have no idea;) please, help.










share|improve this question
























  • see this for help : stackoverflow.com/questions/43062870/…

    – Anbu.Karthik
    Mar 8 at 8:46






  • 1





    How about case handling within addPeriodicTimeObserver block to return immediately when slider is being touched?

    – staticVoidMan
    Mar 8 at 9:13












  • @staticVoidMan how would you like to implement it?;) Please answer your idea and I will try it.

    – Bartłomiej Semańczyk
    Mar 8 at 9:27






  • 1





    @BartłomiejSemańczyk Well, the basic idea is to prevent slider from updating when it is being touched/moved. I will chalk up a sample example :)

    – staticVoidMan
    Mar 8 at 9:47













3












3








3








I try to implement simple player with UISlider to indicate at what time is current audio file.



enter image description here



In code I have added two observers:



 slider.rx.value.subscribe(onNext: value in
let totalTime = Float(CMTimeGetSeconds(self.player.currentItem!.duration))
let seconds = value * totalTime
let time = CMTime(seconds: Double(seconds), preferredTimescale: CMTimeScale(NSEC_PER_SEC))
self.player.seek(to: time)
).disposed(by: bag)
let interval = CMTime(seconds: 0.1, preferredTimescale: CMTimeScale(NSEC_PER_SEC))
player.addPeriodicTimeObserver(forInterval: interval, queue: nil) [weak self] time in
self?.updateSlider(with: time)



with one private function:



private func updateSlider(with time: CMTime) 
let currentTime = CMTimeGetSeconds(time)
var totalTime = CMTimeGetSeconds(player.currentItem!.duration)
if totalTime.isNaN
totalTime = 0

startLabel.text = Int(currentTime).descriptiveDuration
endLabel.text = Int(totalTime).descriptiveDuration
slider.value = Float(currentTime / totalTime)



When audio plays, everything is fine and slider is pretty much updated. The problem occurs when I try to move slider manually while audio is playing, then it jumps. Why?



UPDATE:



I know why actually. Because I update it twice: manually and from player observer, but how to prevent from this behaviour? I have no idea;) please, help.










share|improve this question
















I try to implement simple player with UISlider to indicate at what time is current audio file.



enter image description here



In code I have added two observers:



 slider.rx.value.subscribe(onNext: value in
let totalTime = Float(CMTimeGetSeconds(self.player.currentItem!.duration))
let seconds = value * totalTime
let time = CMTime(seconds: Double(seconds), preferredTimescale: CMTimeScale(NSEC_PER_SEC))
self.player.seek(to: time)
).disposed(by: bag)
let interval = CMTime(seconds: 0.1, preferredTimescale: CMTimeScale(NSEC_PER_SEC))
player.addPeriodicTimeObserver(forInterval: interval, queue: nil) [weak self] time in
self?.updateSlider(with: time)



with one private function:



private func updateSlider(with time: CMTime) 
let currentTime = CMTimeGetSeconds(time)
var totalTime = CMTimeGetSeconds(player.currentItem!.duration)
if totalTime.isNaN
totalTime = 0

startLabel.text = Int(currentTime).descriptiveDuration
endLabel.text = Int(totalTime).descriptiveDuration
slider.value = Float(currentTime / totalTime)



When audio plays, everything is fine and slider is pretty much updated. The problem occurs when I try to move slider manually while audio is playing, then it jumps. Why?



UPDATE:



I know why actually. Because I update it twice: manually and from player observer, but how to prevent from this behaviour? I have no idea;) please, help.







ios swift avplayer uislider






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 8 at 13:01









AtulParmar

1,334623




1,334623










asked Mar 8 at 8:17









Bartłomiej SemańczykBartłomiej Semańczyk

34.4k25170237




34.4k25170237












  • see this for help : stackoverflow.com/questions/43062870/…

    – Anbu.Karthik
    Mar 8 at 8:46






  • 1





    How about case handling within addPeriodicTimeObserver block to return immediately when slider is being touched?

    – staticVoidMan
    Mar 8 at 9:13












  • @staticVoidMan how would you like to implement it?;) Please answer your idea and I will try it.

    – Bartłomiej Semańczyk
    Mar 8 at 9:27






  • 1





    @BartłomiejSemańczyk Well, the basic idea is to prevent slider from updating when it is being touched/moved. I will chalk up a sample example :)

    – staticVoidMan
    Mar 8 at 9:47

















  • see this for help : stackoverflow.com/questions/43062870/…

    – Anbu.Karthik
    Mar 8 at 8:46






  • 1





    How about case handling within addPeriodicTimeObserver block to return immediately when slider is being touched?

    – staticVoidMan
    Mar 8 at 9:13












  • @staticVoidMan how would you like to implement it?;) Please answer your idea and I will try it.

    – Bartłomiej Semańczyk
    Mar 8 at 9:27






  • 1





    @BartłomiejSemańczyk Well, the basic idea is to prevent slider from updating when it is being touched/moved. I will chalk up a sample example :)

    – staticVoidMan
    Mar 8 at 9:47
















see this for help : stackoverflow.com/questions/43062870/…

– Anbu.Karthik
Mar 8 at 8:46





see this for help : stackoverflow.com/questions/43062870/…

– Anbu.Karthik
Mar 8 at 8:46




1




1





How about case handling within addPeriodicTimeObserver block to return immediately when slider is being touched?

– staticVoidMan
Mar 8 at 9:13






How about case handling within addPeriodicTimeObserver block to return immediately when slider is being touched?

– staticVoidMan
Mar 8 at 9:13














@staticVoidMan how would you like to implement it?;) Please answer your idea and I will try it.

– Bartłomiej Semańczyk
Mar 8 at 9:27





@staticVoidMan how would you like to implement it?;) Please answer your idea and I will try it.

– Bartłomiej Semańczyk
Mar 8 at 9:27




1




1





@BartłomiejSemańczyk Well, the basic idea is to prevent slider from updating when it is being touched/moved. I will chalk up a sample example :)

– staticVoidMan
Mar 8 at 9:47





@BartłomiejSemańczyk Well, the basic idea is to prevent slider from updating when it is being touched/moved. I will chalk up a sample example :)

– staticVoidMan
Mar 8 at 9:47












1 Answer
1






active

oldest

votes


















2














One simple way to go about this would be to prevent addPeriodicTimeObserver from calling self?.updateSlider(with: time) when the slider is being touched.



This can be determined via the UISliders isTracking property:




isTracking



A Boolean value indicating whether the control is currently tracking
touch events.



While tracking of a touch event is in progress, the control sets the
value of this property to true. When tracking ends or is cancelled for
any reason, it sets this property to false.



Ref: https://developer.apple.com/documentation/uikit/uicontrol/1618210-istracking




This is present in all UIControl elements which you can use in this way:



player.addPeriodicTimeObserver(forInterval: interval, queue: nil) [weak self] time in
//check if slider is being touched/tracked
guard self?.slider.isTracking == false else return

//if slider is not being touched, then update the slider from here
self?.updateSlider(with: time)




Generic Example:



@IBOutlet var slider: UISlider!
//...
func startSlider()
slider.value = 0
slider.maximumValue = 10

Timer.scheduledTimer(withTimeInterval: 0.1, repeats: true) [weak self] (timer) in
print("Slider at: (self?.slider.value)")
guard self?.slider.isTracking == false else return
self?.updateSlider(to: self!.slider.value + 0.1)



private func updateSlider(to value: Float)
slider.value = value




I'm sure there are other (better) ways out there but I haven't done much in RxSwift (yet).

I hope this is good enough for now.






share|improve this answer

























  • @BartłomiejSemańczyk I've updated the answer. Stuff is easier. No need for the previous implementation. My bad, I hardly use it so forgot :P

    – staticVoidMan
    Mar 8 at 10:48










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%2f55059162%2fuislider-jumps-when-updating-for-avplayer%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














One simple way to go about this would be to prevent addPeriodicTimeObserver from calling self?.updateSlider(with: time) when the slider is being touched.



This can be determined via the UISliders isTracking property:




isTracking



A Boolean value indicating whether the control is currently tracking
touch events.



While tracking of a touch event is in progress, the control sets the
value of this property to true. When tracking ends or is cancelled for
any reason, it sets this property to false.



Ref: https://developer.apple.com/documentation/uikit/uicontrol/1618210-istracking




This is present in all UIControl elements which you can use in this way:



player.addPeriodicTimeObserver(forInterval: interval, queue: nil) [weak self] time in
//check if slider is being touched/tracked
guard self?.slider.isTracking == false else return

//if slider is not being touched, then update the slider from here
self?.updateSlider(with: time)




Generic Example:



@IBOutlet var slider: UISlider!
//...
func startSlider()
slider.value = 0
slider.maximumValue = 10

Timer.scheduledTimer(withTimeInterval: 0.1, repeats: true) [weak self] (timer) in
print("Slider at: (self?.slider.value)")
guard self?.slider.isTracking == false else return
self?.updateSlider(to: self!.slider.value + 0.1)



private func updateSlider(to value: Float)
slider.value = value




I'm sure there are other (better) ways out there but I haven't done much in RxSwift (yet).

I hope this is good enough for now.






share|improve this answer

























  • @BartłomiejSemańczyk I've updated the answer. Stuff is easier. No need for the previous implementation. My bad, I hardly use it so forgot :P

    – staticVoidMan
    Mar 8 at 10:48















2














One simple way to go about this would be to prevent addPeriodicTimeObserver from calling self?.updateSlider(with: time) when the slider is being touched.



This can be determined via the UISliders isTracking property:




isTracking



A Boolean value indicating whether the control is currently tracking
touch events.



While tracking of a touch event is in progress, the control sets the
value of this property to true. When tracking ends or is cancelled for
any reason, it sets this property to false.



Ref: https://developer.apple.com/documentation/uikit/uicontrol/1618210-istracking




This is present in all UIControl elements which you can use in this way:



player.addPeriodicTimeObserver(forInterval: interval, queue: nil) [weak self] time in
//check if slider is being touched/tracked
guard self?.slider.isTracking == false else return

//if slider is not being touched, then update the slider from here
self?.updateSlider(with: time)




Generic Example:



@IBOutlet var slider: UISlider!
//...
func startSlider()
slider.value = 0
slider.maximumValue = 10

Timer.scheduledTimer(withTimeInterval: 0.1, repeats: true) [weak self] (timer) in
print("Slider at: (self?.slider.value)")
guard self?.slider.isTracking == false else return
self?.updateSlider(to: self!.slider.value + 0.1)



private func updateSlider(to value: Float)
slider.value = value




I'm sure there are other (better) ways out there but I haven't done much in RxSwift (yet).

I hope this is good enough for now.






share|improve this answer

























  • @BartłomiejSemańczyk I've updated the answer. Stuff is easier. No need for the previous implementation. My bad, I hardly use it so forgot :P

    – staticVoidMan
    Mar 8 at 10:48













2












2








2







One simple way to go about this would be to prevent addPeriodicTimeObserver from calling self?.updateSlider(with: time) when the slider is being touched.



This can be determined via the UISliders isTracking property:




isTracking



A Boolean value indicating whether the control is currently tracking
touch events.



While tracking of a touch event is in progress, the control sets the
value of this property to true. When tracking ends or is cancelled for
any reason, it sets this property to false.



Ref: https://developer.apple.com/documentation/uikit/uicontrol/1618210-istracking




This is present in all UIControl elements which you can use in this way:



player.addPeriodicTimeObserver(forInterval: interval, queue: nil) [weak self] time in
//check if slider is being touched/tracked
guard self?.slider.isTracking == false else return

//if slider is not being touched, then update the slider from here
self?.updateSlider(with: time)




Generic Example:



@IBOutlet var slider: UISlider!
//...
func startSlider()
slider.value = 0
slider.maximumValue = 10

Timer.scheduledTimer(withTimeInterval: 0.1, repeats: true) [weak self] (timer) in
print("Slider at: (self?.slider.value)")
guard self?.slider.isTracking == false else return
self?.updateSlider(to: self!.slider.value + 0.1)



private func updateSlider(to value: Float)
slider.value = value




I'm sure there are other (better) ways out there but I haven't done much in RxSwift (yet).

I hope this is good enough for now.






share|improve this answer















One simple way to go about this would be to prevent addPeriodicTimeObserver from calling self?.updateSlider(with: time) when the slider is being touched.



This can be determined via the UISliders isTracking property:




isTracking



A Boolean value indicating whether the control is currently tracking
touch events.



While tracking of a touch event is in progress, the control sets the
value of this property to true. When tracking ends or is cancelled for
any reason, it sets this property to false.



Ref: https://developer.apple.com/documentation/uikit/uicontrol/1618210-istracking




This is present in all UIControl elements which you can use in this way:



player.addPeriodicTimeObserver(forInterval: interval, queue: nil) [weak self] time in
//check if slider is being touched/tracked
guard self?.slider.isTracking == false else return

//if slider is not being touched, then update the slider from here
self?.updateSlider(with: time)




Generic Example:



@IBOutlet var slider: UISlider!
//...
func startSlider()
slider.value = 0
slider.maximumValue = 10

Timer.scheduledTimer(withTimeInterval: 0.1, repeats: true) [weak self] (timer) in
print("Slider at: (self?.slider.value)")
guard self?.slider.isTracking == false else return
self?.updateSlider(to: self!.slider.value + 0.1)



private func updateSlider(to value: Float)
slider.value = value




I'm sure there are other (better) ways out there but I haven't done much in RxSwift (yet).

I hope this is good enough for now.







share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 8 at 10:47

























answered Mar 8 at 10:06









staticVoidManstaticVoidMan

10.9k43875




10.9k43875












  • @BartłomiejSemańczyk I've updated the answer. Stuff is easier. No need for the previous implementation. My bad, I hardly use it so forgot :P

    – staticVoidMan
    Mar 8 at 10:48

















  • @BartłomiejSemańczyk I've updated the answer. Stuff is easier. No need for the previous implementation. My bad, I hardly use it so forgot :P

    – staticVoidMan
    Mar 8 at 10:48
















@BartłomiejSemańczyk I've updated the answer. Stuff is easier. No need for the previous implementation. My bad, I hardly use it so forgot :P

– staticVoidMan
Mar 8 at 10:48





@BartłomiejSemańczyk I've updated the answer. Stuff is easier. No need for the previous implementation. My bad, I hardly use it so forgot :P

– staticVoidMan
Mar 8 at 10:48



















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%2f55059162%2fuislider-jumps-when-updating-for-avplayer%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

Identity Server 4 is not redirecting to Angular app after login2019 Community Moderator ElectionIdentity Server 4 and dockerIdentityserver implicit flow unauthorized_clientIdentityServer Hybrid Flow - Access Token is null after user successful loginIdentity Server to MVC client : Page Redirect After loginLogin with Steam OpenId(oidc-client-js)Identity Server 4+.NET Core 2.0 + IdentityIdentityServer4 post-login redirect not working in Edge browserCall to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an objectIdentityServer4 without HTTPS not workingHow to get Authorization code from identity server without login form

2005 Ahvaz unrest Contents Background Causes Casualties Aftermath See also References Navigation menue"At Least 10 Are Killed by Bombs in Iran""Iran"Archived"Arab-Iranians in Iran to make April 15 'Day of Fury'"State of Mind, State of Order: Reactions to Ethnic Unrest in the Islamic Republic of Iran.10.1111/j.1754-9469.2008.00028.x"Iran hangs Arab separatists"Iran Overview from ArchivedConstitution of the Islamic Republic of Iran"Tehran puzzled by forged 'riots' letter""Iran and its minorities: Down in the second class""Iran: Handling Of Ahvaz Unrest Could End With Televised Confessions""Bombings Rock Iran Ahead of Election""Five die in Iran ethnic clashes""Iran: Need for restraint as anniversary of unrest in Khuzestan approaches"Archived"Iranian Sunni protesters killed in clashes with security forces"Archived

Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme