BLACK FRIDAY SALE: Save big on all my Swift books and bundles! >>

SOLVED: Subsonic package only seems to work on headphones audio

Forums > SwiftUI

Hi all, Before I start, I'd like to thank anyone who cares to offere any solutions to this one..

I Just built a tap app that plays a random MP3 file (phrase) everytime the button is pressed.

Simple app design based loosely off Pauls Playground 1st app video

The issue is, i ported to xCode and then installed the app on my iPhone 12 for final test. Upon tapping the button (trigger point) no sound comes from the phones speakers. But, if I plug in headphones, I can hear it perfectly and it works as intended.

Does this mean the Subsonic audio package only works for headphones? If so, has it, or will it be updated for phone speaker sound?

Again, thanks for any advice offered.

Oh, and before anyone asks. No, the phone is not in silent mode or volume turned down. ;-)

Here is the code...


import SwiftUI

import Subsonic

struct ContentView: View {
    func choose() {
        _ = Int.random(in: 1..<31) 
    }    

    // var snd = Int.random(in: 1..<31) 

    var body: some View {
        ZStack{
        Color.white.edgesIgnoringSafeArea(.all)

        // Stick buttong code here to call random & play func()
        Button {

            stopAllManagedSounds()            
            let  snd = Int.random(in: 1..<31)          

            play(sound: "\(snd).mp3")            

            } label: {
        Image("ButtonOn")
            .resizable()
            .scaledToFit()

        }
    }
        } }

   

Any ideas? :-(

   

Most likely your sound is muted (by a hardware switch on the side pf the phone). When you plug your earphones the switch position is ignored.

PS: Friendly note :) The IDE is spelled Xcode, I noticed you wrote xCode in a couple places (including your profile description)

   

Hi ogreswamp,

Thanks for the reply. greatly appreciated. if you re-read the post, I clearly state near the end...

Oh, and before anyone asks. No, the phone is not in silent mode or volume turned down. ;-)

But, thank you anyways.

anyone else?

x

1      

Sorry, I missed that point.

I tried your code and it works fine on my phone (didn't work originally, but then I realized it was in silent mode).

I found a similar issue here: https://stackoverflow.com/questions/18807157/how-do-i-route-audio-to-speaker-without-using-audiosessionsetproperty/18808124#18808124

Try this code, se if it will help:

import SwiftUI
import Subsonic
import AVFoundation

struct ContentView: View {

    var body: some View {
        ZStack{
            Color.white.edgesIgnoringSafeArea(.all)

            // Stick buttong code here to call random & play func()
            Button {

                stopAllManagedSounds()
                let  snd = Int.random(in: 1..<31)
                play(sound: "\(snd).mp3")

            } label: {
                Image("ButtonOn")
                    .resizable()
                    .scaledToFit()

            }
        }
    }

    func play(sound: String) {
        do {
            let session = AVAudioSession.sharedInstance()
            try session.setCategory(.playAndRecord)
            try session.overrideOutputAudioPort(.speaker)
            try session.setActive(true)
        } catch {
            print(error)
        }
        SubsonicController.shared.play(sound: sound, from: .main, volume: 1, repeatCount: 0)
    }
}

PS: That doesn't seem like a SwiftUI issue though. I'd say you picked wrong forum section for the problem.

   

Hi,

Thanks for the reply. I will check later once I finish work. I did wonder what section to pop this into. So, yeah, maybe doesn't belong in SwiftUI. πŸ™ŒπŸ»πŸ‘πŸ»πŸ‘

Cheers for the help

   

Hacking with Swift is sponsored by RevenueCat

SPONSORED In-app subscriptions are a pain to implement, hard to test, and full of edge cases. RevenueCat makes it straightforward and reliable so you can get back to building your app. Oh, and it's free if your app makes less than $10k/mo.

Learn more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.