Qualcuno potrebbe avere un collegamento a un tutorial per aggiungere una barra di avanzamento della riproduzione a AVAudioPlayer?avanzamento riproduzione avaudioplayer
Ho cercato a lungo su questo sito e su google inutilmente
Qualcuno potrebbe avere un collegamento a un tutorial per aggiungere una barra di avanzamento della riproduzione a AVAudioPlayer?avanzamento riproduzione avaudioplayer
Ho cercato a lungo su questo sito e su google inutilmente
ho capito ed era troppo male.
Basta aggiornarla in un timer
playbackTimer=[NSTimer scheduledTimerWithTimeInterval:0.5
target:self
selector:@selector(myMethod:)
userInfo:nil
repeats:YES];
}
-(void)myMethod:(NSTimer*)timer {
float total=audioPlayer.duration;
float f=audioPlayer.currentTime/total;
NSString *str = [NSString stringWithFormat:@"%f", f];
playbackProgress.progress=f;
NSLog(str);
}
Aggiungi un tempo osservatore periodica e aggiornare il dispositivo di scorrimento - a Swift sarà in questo modo -
func addPlayBackSlider()
{
playbackSlider = UISlider(frame:CGRect(x:10, y:300, width:300, height:20))
playbackSlider?.minimumValue = 0
let duration : CMTime = playerItem.asset.duration
let seconds : Float64 = CMTimeGetSeconds(duration)
playbackSlider?.maximumValue = Float(seconds)
playbackSlider?.isContinuous = true
//If you want to manually set progressBar
playbackSlider?.addTarget(self, action: #selector(AddAudioController.playbackSliderValueChanged(_:)), for: .valueChanged)
//Here is the Observer
player!.addPeriodicTimeObserver(forInterval: CMTimeMakeWithSeconds(1, 1), queue: DispatchQueue.main) { (CMTime) -> Void in
if self.player!.currentItem?.status == .readyToPlay {
let time : Float64 = CMTimeGetSeconds(self.player!.currentTime());
self.playbackSlider!.value = Float (time);
}
}
}
//If you want to manually set progressBar
func playbackSliderValueChanged(_ playbackSlider:UISlider)
{
let seconds : Int64 = Int64(playbackSlider.value)
targetTime = CMTimeMake(seconds, 1)
print(targetTime)
player!.seek(to: targetTime)
if player!.rate == 0
{
player?.play()
playButton!.setTitle("Pause", for: UIControlState.normal)
}
}
Spero che questo aiuti :) – Shawon91
Aggiunta di un l'osservatore di tempo periodico funziona purtroppo solo per AVPlayer, non per AVAudioPlayer. –