Comments (15)
not working @sukh1993 sir, i have done audiolayer.stop() in stop button click and in playedUntilEnd i have done as you said
from swiftaudio.
And actually i my case theres no next song when current song ends it just have to start the same song again but when i click stop button the ui resets but when play again it doesnt do anything
from swiftaudio.
pls help @sukh1993 sir
from swiftaudio.
I have faced same problem in my current project but i have handle like this.
player?.event.playbackEnd.addListener(self, handleAudioPlayerStatePlayBackEnd)
func handleAudioPlayerStatePlayBackEnd(state: AudioPlayer.PlaybackEndEventData) {
if state == .playedUntilEnd{
// Reload your audio again and reset UI also
}
}
}
from swiftaudio.
not working @sukh1993 sir,
my code is as follows, can you point me where i am wrong where to add what code
var audioURL:String?
private var isScrubbing: Bool = false
private let audioPlayer = AudioPlayer()
let audioSessionController = AudioSessionController.shared
override func viewDidLoad() {
super.viewDidLoad()
setupAudioPlayer()
configureTrackSlider()
createNowPlayingAnimation()
}
@IBAction func onClick_playAudioBtnTapped(_ sender: UIButton) {
if !audioSessionController.audioSessionIsActive {
try? audioSessionController.activateSession()
}
audioPlayer.togglePlaying()
}
@IBAction func onClick_stopBtnTapped(_ sender: UIButton) {
self.audioPlayer.stop()
self.nowPlayingImageView.stopAnimating()
}
private func setupAudioPlayer() {
audioPlayer.event.stateChange.addListener(self, handleAudioPlayerStateChange)
audioPlayer.event.secondElapse.addListener(self, handleAudioPlayerSecondElapsed)
audioPlayer.event.seek.addListener(self, handleAudioPlayerDidSeek)
audioPlayer.event.updateDuration.addListener(self, handleAudioPlayerUpdateDuration)
audioPlayer.event.didRecreateAVPlayer.addListener(self, handleAVPlayerRecreated)
audioPlayer.event.playbackEnd.addListener(self, handlePlayEnd)
audioPlayer.event.fail.addListener(self, handlePlayerFailure)
handleAudioPlayerStateChange(data: audioPlayer.playerState)
if let url = self.audioURL {
let item = DefaultAudioItem(audioUrl: url, sourceType: .stream)
try? self.audioPlayer.load(item: item, playWhenReady: true)
self.playPauseBtn.setImage(UIImage(named: "ic_audio_pause"), for: .normal)
self.scaleInBookCover(isFirstLoad: false)
self.nowPlayingImageView.startAnimating()
}
}
// MARK: - AudioPlayer Event Handlers
extension AudioPlayerVC {
func updateTimeValues() {
self.trackSlider.maximumValue = CGFloat(self.audioPlayer.duration)
self.trackSlider.setValue(CGFloat(self.audioPlayer.currentTime), animated: true)
self.currentTimeLbl.text = getTimeString(time: self.audioPlayer.currentTime)
self.durationLbl.text = getTimeString(time: (self.audioPlayer.duration - self.audioPlayer.currentTime))
}
func setPlayButtonState(forAudioPlayerState state: AudioPlayerState) {
if state == .playing {
playPauseBtn.setImage(UIImage(named: "ic_audio_pause"), for: .normal)
self.scaleInBookCover(isFirstLoad: false)
self.nowPlayingImageView.startAnimating()
} else {
playPauseBtn.setImage(UIImage(named: "ic_audio_play"), for: .normal)
self.scaleOutBookCover()
self.nowPlayingImageView.stopAnimating()
}
}
func handleAudioPlayerStateChange(data: AudioPlayer.StateChangeEventData) {
print(data)
DispatchQueue.main.async {
self.setPlayButtonState(forAudioPlayerState: data)
switch data {
case .loading:
self.startLoader()
self.updateTimeValues()
case .buffering:
self.startLoader()
case .ready:
self.stopLoader()
self.updateTimeValues()
case .playing, .paused, .idle:
self.stopLoader()
self.updateTimeValues()
}
}
}
func handlePlayEnd(data: AudioPlayer.PlaybackEndEventData) {
DispatchQueue.main.async {
switch data {
case .playedUntilEnd:
if let url = self.audioURL {
let item = DefaultAudioItem(audioUrl: url, sourceType: .stream)
try? self.audioPlayer.load(item: item, playWhenReady: false)
}
case .playerStopped:
self.audioPlayer.stop()
self.stopLoader()
case .skippedToNext:
self.stopLoader()
case .skippedToPrevious:
self.stopLoader()
case .jumpedToIndex:
self.stopLoader()
}
}
}
func handleAudioPlayerSecondElapsed(data: AudioPlayer.SecondElapseEventData) {
if !isScrubbing {
DispatchQueue.main.async {
self.updateTimeValues()
}
}
}
func handleAudioPlayerDidSeek(data: AudioPlayer.SeekEventData) {
isScrubbing = false
}
func handleAudioPlayerUpdateDuration(data: AudioPlayer.UpdateDurationEventData) {
DispatchQueue.main.async {
self.updateTimeValues()
}
}
func handleAVPlayerRecreated() {
try? audioSessionController.set(category: .playback)
}
func handlePlayerFailure(data: AudioPlayer.FailEventData) {
if let error = data as NSError? {
if error.code == -1009 {
DispatchQueue.main.async {
print("Network disconnected. Please try again...")
}
}
}
}
}
from swiftaudio.
@nishant6042 try this its works for me
func handlePlayEnd(data: AudioPlayer.PlaybackEndEventData) {
DispatchQueue.main.async {
switch data {
case .playedUntilEnd:
// pause audio player when playedUntilEnd and reload next song or previous song
self.audioPlayer.pause()
if let url = self.audioURL {
let item = DefaultAudioItem(audioUrl: url, sourceType: .stream)
try? self.audioPlayer.load(item: item, playWhenReady: false)
}
case .playerStopped:
self.audioPlayer.stop()
self.stopLoader()
case .skippedToNext:
self.stopLoader()
case .skippedToPrevious:
self.stopLoader()
case .jumpedToIndex:
self.stopLoader()
}
}
}
from swiftaudio.
@nishant6042 hi
//If things is not working try this with some tricks
var isAudioStops : Bool = false
@IBAction func onClick_playAudioBtnTapped(_ sender: UIButton) {
if isAudioStops == true{
if let url = self.audioURL {
let item = DefaultAudioItem(audioUrl: url, sourceType: .stream)
try? self.audioPlayer.load(item: item, playWhenReady: true)
self.playPauseBtn.setImage(UIImage(named: "ic_audio_pause"), for: .normal)
self.scaleInBookCover(isFirstLoad: false)
self.nowPlayingImageView.startAnimating()
}
isAudioStops = false
}else{
if !audioSessionController.audioSessionIsActive {
try? audioSessionController.activateSession()
}
audioPlayer.togglePlaying()
}
}
@IBAction func onClick_stopBtnTapped(_ sender: UIButton) {
self.audioPlayer.stop()
self.nowPlayingImageView.stopAnimating()
self.isAudioStops = true // bool true
}
func handlePlayEnd(data: AudioPlayer.PlaybackEndEventData) {
DispatchQueue.main.async {
switch data {
case .playedUntilEnd:
self.isAudioStops = true // bool true
case .playerStopped:
self.audioPlayer.stop()
self.stopLoader()
case .skippedToNext:
self.stopLoader()
case .skippedToPrevious:
self.stopLoader()
case .jumpedToIndex:
self.stopLoader()
}
}
}
from swiftaudio.
not working still @sukh1993 sir
from swiftaudio.
@nishant6042 check with break points, Is your player reload the url or not on play button. if It is not working change stop method to pause.
from swiftaudio.
@sukh1993 sir working when doing pause instead of stop but doing that it pauses i want to restart from start when stop clicked
from swiftaudio.
@nishant6042 I will working this issue, this is bug on swift Audio side.
from swiftaudio.
So whats the alternate for now?
from swiftaudio.
and also what should i do to play in background also when i leave the screen @sukh1993 sir
from swiftaudio.
Hi @nishant6042
Are you manage cache?
means i load one audio and then play again, it will play from without load.
If you did this please let me know, how can be?
from swiftaudio.
@nishant6042 also check this link :- #53
about stop functionality
from swiftaudio.
Related Issues (20)
- Skipforward and skipforward issue HOT 5
- Set preferredPeakBitRate
- Catch Expired Auth Token to modify headers HOT 1
- Player state not updated after triggering the play()
- Cannot run Example app HOT 1
- How to know that the song is ended HOT 1
- AVQueuePlayer for gapless audio
- QueuedAudioPlayer.reset() is not accessible due to 'internal' protection level
- Not working with local file in Queue HOT 1
- Load the audio player
- MPRemoteCommandCenter
- handleAudioPlayerStateChange call multiple time HOT 1
- how to clearQueue HOT 2
- .wav file not play
- Repository Future HOT 2
- @available enum issue on XCode 13 HOT 2
- iOS 15 can not play music in the background HOT 6
- Song will not play if it contains any character between "èéêëēėęàáâäæãåāîïíīįìôöòóœøōõûüùúū"
- Xcode 12.5 - Using 'class' keyword for protocol inheritance is deprecated; use 'AnyObject' instead
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from swiftaudio.