UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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()

        }
    }
        } }

1      

Any ideas? :-(

1      

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)

1      

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

2      

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.

1      

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

1      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.