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
I try to implement simple player with UISlider to indicate at what time is current audio file.
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
add a comment |
I try to implement simple player with UISlider to indicate at what time is current audio file.
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
see this for help : stackoverflow.com/questions/43062870/…
– Anbu.Karthik
Mar 8 at 8:46
1
How about case handling withinaddPeriodicTimeObserver
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
add a comment |
I try to implement simple player with UISlider to indicate at what time is current audio file.
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
I try to implement simple player with UISlider to indicate at what time is current audio file.
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
ios swift avplayer uislider
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 withinaddPeriodicTimeObserver
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
add a comment |
see this for help : stackoverflow.com/questions/43062870/…
– Anbu.Karthik
Mar 8 at 8:46
1
How about case handling withinaddPeriodicTimeObserver
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
add a comment |
1 Answer
1
active
oldest
votes
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 UISlider
s 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.
@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
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%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
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 UISlider
s 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.
@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
add a comment |
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 UISlider
s 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.
@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
add a comment |
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 UISlider
s 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.
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 UISlider
s 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.
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
add a comment |
@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
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%2f55059162%2fuislider-jumps-when-updating-for-avplayer%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
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