Is it possible to play audio in the Background or when the app is Terminated? If yes, how can I play audio in the Background or when the app is Terminated in iOS using Swift? I am receiving an audio link in a Firebase notification. How can I play this audio link when the app is in the Background or Terminated?
Post
Replies
Boosts
Views
Activity
I am using PushToTalk in my project for using only listing audio.
steps :-
App Launch :- Create PTT Channel
PTT Token :- Send Token in Server
App Kill :- It's Automatically restored channel using :- channelDescriptor(restoredChannelUUID channelUUID: UUID) -> PTChannelDescriptor
Play audio given by incomingPushResult method
issue :-
I am receiving an audio link through incomingPushResult.
When incomingPushResult is called, it automatically restores the channel. However, the channel has already been created, resulting in two channels being created.
When I send the link from the server, the audio plays properly.
However, if I resend the same link after 5-6 seconds, the audio does not play.
After I leave the first channel, the same audio starts playing in the second channel. When I send the link again, the audio does not play because I left the first (main) channel.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
UNUserNotificationCenter.current().delegate = self
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { granted, error in
if granted {
DispatchQueue.main.async {
application.registerForRemoteNotifications()
}
}
}
Task {
await self.createChannel()
}
return true
}
func createChannel() async {
do {
channelManager = try await PTChannelManager.channelManager(delegate: self, restorationDelegate: self)
let channelImage = UIImage(named: "ic_p")
channelDescriptor = PTChannelDescriptor(name: "Pikachu", image: channelImage)
let channelUUID = UUID()
self.currentChannelUUID = channelUUID
channelManager?.requestJoinChannel(channelUUID: channelUUID, descriptor: channelDescriptor!)
print("PTT creating channel")
} catch {
print("Error creating channel: \(error)")
}
}
func incomingPushResult(channelManager: PTChannelManager, channelUUID: UUID, pushPayload: [String : Any]) -> PTPushResult {
guard let data = pushPayload["data"] as? [String: Any],
let mediaLink = data["media_link"] as? String else {
return .leaveChannel
}
print("incomingPTT")
// URL to fetch the audio data from
self.audioURL = mediaLink
// Play the audio from the URL
DispatchQueue.main.async {
self.playSound(url: self.audioURL)
}
let participant = PTParticipant(name: mediaLink, image: .checkmark)
return .activeRemoteParticipant(participant)
}
func channelDescriptor(restoredChannelUUID channelUUID: UUID) -> PTChannelDescriptor {
let channelImage = UIImage(named: "ic_r")
return PTChannelDescriptor(name: "Restored Channel", image: channelImage)
}
func channelManager(_ channelManager: PTChannelManager, didActivate audioSession: AVAudioSession) {
print("Activated audio session")
self.playSound(url: self.audioURL)
}
Output: -
App Launch
After App Kill Play audio :- Audio Play Success and leave the channel
(Before Leave Channel View)
After Leave Channel View