Thank you, I doubt my code would be very useful. It is iOS-only, and I did all the actual work in Swift. It is very simple and not completely debugged. I posted the finished module below. As you see, it is very simple, but does what I need. The critical pieces for background are in the session.setCategory
//
// SwSound.swift
// SoundScape
//
// Created by David Marques on 5/2/24.
//
import Foundation
import AVFoundation
var player : AVPlayer!
var inactive = true
public class SwSound : NSObject {
var session = AVAudioSession.sharedInstance()
var ctstime = CMTime()
// override init() {
func activateSession() {
do {
try session.setCategory(
.playback, //allows app to keep playing while in the background
mode: .default,
options: [.mixWithOthers] // allows other app sounds to mix
)
} catch _ {}
do {
try session.setActive(true, options: .notifyOthersOnDeactivation)
} catch _ {}
do {
try session.overrideOutputAudioPort(.speaker)
} catch _ {}
// player = AVPlayer()
}
@objc func pause() {
print("in Swift pause");
if (player.rate != 0) {
print("pauseing");
player.pause()
}
}
@objc func setup() {
print("swsound setup")
activateSession()
createPlayer()
}
@objc func seek(_ stime: Double) {
ctstime = CMTimeMakeWithSeconds(stime, preferredTimescale: 1)
// player.seek(to: ctstime)
}
@objc func currentTime() -> Double {
// print("in Swift currentTime");
var ct = 0.1
if (player.rate != 0) {
ct = Double(Float(CMTimeGetSeconds(player.currentTime())))
print("currentTime: " + String(format: "%f", ct))
}
return ct
}
@objc func createPlayer(){
player = AVPlayer()
}
@objc func playRemote(_ fileName: NSString) {
print("in Swift " + (fileName as String));
let url = URL(string: fileName as String)!
//------------
// Standard AVPlayer
let playerItem: AVPlayerItem = AVPlayerItem(url: url)
player.replaceCurrentItem(with: playerItem)
player.seek(to: ctstime)
player.play()
//---------
}
}