Post

Replies

Boosts

Views

Activity

Airplay swiftUI
So we have a media player in our SwiftUI app, and all the buttons go to the media function to play, pause , rewind etc. One feature people are requesting is the ability to airplay to other devices. I know they can do this via the Lock Screen and the notification bar, but wondering what function do I need to add to the media-player function to allow me to add a button to my swiftUI. I am wondering what function code do I need to add to my core mediaplayer.swift file that I can link a button too.
1
1
1.4k
Aug ’22
Fast forward and rewind button (SwiftUI)
So I am trying to add two buttons to the view of my SwiftUI app that will allow a user to fast forward and rewind the audio clip. I already have it working on the Lock Screen and notification bar, but in the app I can't figure out how to link two buttons to these actions. I am wondering if anyone is able to assist. In the MusicCore.swift I have the following func setupRemoteTransportControls() { // Get the shared MPRemoteCommandCenter let commandCenter = MPRemoteCommandCenter.shared() let changePlaybackPositionCommand = commandCenter.changePlaybackPositionCommand changePlaybackPositionCommand.isEnabled = true changePlaybackPositionCommand.addTarget { event in let seconds = (event as? MPChangePlaybackPositionCommandEvent)?.positionTime ?? 0 let time = CMTime(seconds: seconds, preferredTimescale: 1) self.player?.seek(to: time) return .success } let skipBackwardCommand = commandCenter.skipBackwardCommand if(MusicPlayer.mediatype == "podcast") { skipBackwardCommand.isEnabled = true skipBackwardCommand.preferredIntervals = [NSNumber(value: 10)] skipBackwardCommand.addTarget(handler: skipBackward) } else{ skipBackwardCommand.isEnabled = false } let skipForwardCommand = commandCenter.skipForwardCommand if(MusicPlayer.mediatype == "podcast") { skipForwardCommand.isEnabled = true skipForwardCommand.preferredIntervals = [NSNumber(value: 30)] } else{ skipForwardCommand.isEnabled = false } skipForwardCommand.addTarget(handler: skipForward) // Add handler for Play Command commandCenter.playCommand.addTarget { [unowned self] event in if self.player?.rate == 0.0 { self.player?.play() return .success } return .commandFailed } // Add handler for Pause Command commandCenter.pauseCommand.addTarget { [unowned self] event in if self.player?.rate == 1.0 { self.player?.pause() MPNowPlayingInfoCenter.default().nowPlayingInfo?[MPNowPlayingInfoPropertyElapsedPlaybackTime] = Int(Double((self.player?.currentTime().seconds)!)) return .success } return .commandFailed } func skipBackward(_ event: MPRemoteCommandEvent) -> MPRemoteCommandHandlerStatus { //self.player?.seek(to: CMTimeMakeWithSeconds(CMTimeGetSeconds((self.player?.currentTime())!).advanced(by: -30), preferredTimescale: 1)) // print(CMTimeGetSeconds((self.player?.currentTime())!)) //Output: 42 //print(event.interval) //self.player!.seek(to: CMTimeMakeWithSeconds(CMTimeGetSeconds((self.player?.currentTime())!).advanced(by: -30), preferredTimescale: 1)) let currentTime = self.player?.currentTime() self.player?.seek(to: CMTime(seconds: currentTime!.seconds - 10, preferredTimescale: 1), completionHandler: { isCompleted in if isCompleted { MPNowPlayingInfoCenter.default().nowPlayingInfo?[MPNowPlayingInfoPropertyElapsedPlaybackTime] = Int(Double((self.player?.currentTime().seconds)!)) } }) return .success } func skipForward(_ event: MPRemoteCommandEvent) -> MPRemoteCommandHandlerStatus { //self.player?.seek(to: CMTimeMakeWithSeconds(CMTimeGetSeconds((self.player?.currentTime())!).advanced(by: 30), preferredTimescale: 1)) let currentTime = self.player?.currentTime() self.player?.seek(to: CMTime(seconds: currentTime!.seconds + 30, preferredTimescale: 1), completionHandler: { isCompleted in if isCompleted { MPNowPlayingInfoCenter.default().nowPlayingInfo?[MPNowPlayingInfoPropertyElapsedPlaybackTime] = Int(Double((self.player?.currentTime().seconds)!)) } }) return .success } } But how can I call that from inside a View?
0
0
2.0k
Feb ’22
Airplay swiftUI
So we have a media player in our SwiftUI app, and all the buttons go to the media function to play, pause , rewind etc. One feature people are requesting is the ability to airplay to other devices. I know they can do this via the Lock Screen and the notification bar, but wondering what function do I need to add to the media-player function to allow me to add a button to my swiftUI. I am wondering what function code do I need to add to my core mediaplayer.swift file that I can link a button too.
Replies
1
Boosts
1
Views
1.4k
Activity
Aug ’22
Fast forward and rewind button (SwiftUI)
So I am trying to add two buttons to the view of my SwiftUI app that will allow a user to fast forward and rewind the audio clip. I already have it working on the Lock Screen and notification bar, but in the app I can't figure out how to link two buttons to these actions. I am wondering if anyone is able to assist. In the MusicCore.swift I have the following func setupRemoteTransportControls() { // Get the shared MPRemoteCommandCenter let commandCenter = MPRemoteCommandCenter.shared() let changePlaybackPositionCommand = commandCenter.changePlaybackPositionCommand changePlaybackPositionCommand.isEnabled = true changePlaybackPositionCommand.addTarget { event in let seconds = (event as? MPChangePlaybackPositionCommandEvent)?.positionTime ?? 0 let time = CMTime(seconds: seconds, preferredTimescale: 1) self.player?.seek(to: time) return .success } let skipBackwardCommand = commandCenter.skipBackwardCommand if(MusicPlayer.mediatype == "podcast") { skipBackwardCommand.isEnabled = true skipBackwardCommand.preferredIntervals = [NSNumber(value: 10)] skipBackwardCommand.addTarget(handler: skipBackward) } else{ skipBackwardCommand.isEnabled = false } let skipForwardCommand = commandCenter.skipForwardCommand if(MusicPlayer.mediatype == "podcast") { skipForwardCommand.isEnabled = true skipForwardCommand.preferredIntervals = [NSNumber(value: 30)] } else{ skipForwardCommand.isEnabled = false } skipForwardCommand.addTarget(handler: skipForward) // Add handler for Play Command commandCenter.playCommand.addTarget { [unowned self] event in if self.player?.rate == 0.0 { self.player?.play() return .success } return .commandFailed } // Add handler for Pause Command commandCenter.pauseCommand.addTarget { [unowned self] event in if self.player?.rate == 1.0 { self.player?.pause() MPNowPlayingInfoCenter.default().nowPlayingInfo?[MPNowPlayingInfoPropertyElapsedPlaybackTime] = Int(Double((self.player?.currentTime().seconds)!)) return .success } return .commandFailed } func skipBackward(_ event: MPRemoteCommandEvent) -> MPRemoteCommandHandlerStatus { //self.player?.seek(to: CMTimeMakeWithSeconds(CMTimeGetSeconds((self.player?.currentTime())!).advanced(by: -30), preferredTimescale: 1)) // print(CMTimeGetSeconds((self.player?.currentTime())!)) //Output: 42 //print(event.interval) //self.player!.seek(to: CMTimeMakeWithSeconds(CMTimeGetSeconds((self.player?.currentTime())!).advanced(by: -30), preferredTimescale: 1)) let currentTime = self.player?.currentTime() self.player?.seek(to: CMTime(seconds: currentTime!.seconds - 10, preferredTimescale: 1), completionHandler: { isCompleted in if isCompleted { MPNowPlayingInfoCenter.default().nowPlayingInfo?[MPNowPlayingInfoPropertyElapsedPlaybackTime] = Int(Double((self.player?.currentTime().seconds)!)) } }) return .success } func skipForward(_ event: MPRemoteCommandEvent) -> MPRemoteCommandHandlerStatus { //self.player?.seek(to: CMTimeMakeWithSeconds(CMTimeGetSeconds((self.player?.currentTime())!).advanced(by: 30), preferredTimescale: 1)) let currentTime = self.player?.currentTime() self.player?.seek(to: CMTime(seconds: currentTime!.seconds + 30, preferredTimescale: 1), completionHandler: { isCompleted in if isCompleted { MPNowPlayingInfoCenter.default().nowPlayingInfo?[MPNowPlayingInfoPropertyElapsedPlaybackTime] = Int(Double((self.player?.currentTime().seconds)!)) } }) return .success } } But how can I call that from inside a View?
Replies
0
Boosts
0
Views
2.0k
Activity
Feb ’22