iOS Question How to play sound?

Turbo3

Active Member
Licensed User
Longtime User
Since there is no Beep in B4i and Mediaplayer should be used instead I tried this simple test code but got no sound on an iPhone 4. What am I missing? Log shows duration of 992 (milliseconds) so file loaded ok.

B4X:
Private Sub Application_Start (Nav As NavigationController)
    NavControl = Nav
    Page1.Initialize("Page1")
    Page1.Title = "Page 1"
    Page1.RootPanel.Color = Colors.White
    NavControl.ShowPage(Page1)
    mp1.Initialize(File.DirAssets,"beep-01a.mp3","MPlayer")
    Log("Duration= "&mp1.Duration)
    mp1.Volume=1
    mp1.play
End Sub
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
It does play the sound here.

Try to play it when you click the page:
B4X:
Private Sub Application_Start (Nav As NavigationController)
   NavControl = Nav
   Page1.Initialize("Page1")
   Page1.Title = "Page 1"
   Page1.RootPanel.Color = Colors.White
   NavControl.ShowPage(Page1)
   mp1.Initialize(File.DirAssets,"beep-07.mp3","MPlayer")
   Log("Duration= "&mp1.Duration)
   mp1.play
End Sub
Sub Page1_Click
   mp1.Play
End Sub
Is it playing?
 
Upvote 0

Turbo3

Active Member
Licensed User
Longtime User
No sound on iPhone4.

But I did just try on an iTouch and it makes a sound on start up and again if you touch the screen.

What device did you test it with?

Tried rebooting iPhone4 but no help.

Then I tried plugging in ear buds and I can hear the sound now.

How do we select between speaker and external sound connector?
 
Last edited:
Upvote 0

Turbo3

Active Member
Licensed User
Longtime User
On the iPhone 4 I tried playing music with the Apple player and it works as expected. Sound out speaker when no ear buds plugged in and out ear buds when they are plugged in.

But the B4i app only plays out the ear buds and never out the speaker.
 
Upvote 0

Turbo3

Active Member
Licensed User
Longtime User
I tested on an iTouch and iPad and it works properly too. It is the iPhone 4 that is having the problem. You did not say you actually tested on an iPhone 4 so I will have to assume you did not.

It is not the issue you linked to as the speaker works fine for all other apps. I just installed a Piano app and it plays through the speaker and if I plug in the ear buds it comes out the ear buds. Remove ear buds and the sound comes out the speaker again.

Only the B4i app exhibits this problem. If I play music with the Apple music app the music plays through the speaker. The moment I start the B4i Sound app the music stops and no sound comes out the speakers. Plug in the ear buds and I can hear the beep from the B4i Sound app. Unplug ear buds and start up the Apple player and music plays through the speaker again.

Phone calls also come out the speaker so the hardware is definitely not stuck in some mode where it only output through the external connector. That is the issue the link discusses.

Is there no command in Objective C to switch between speaker and external connector? That is what is needed.

Can someone else with access to an iPhone 4 confirm this issue? That will help determine if it is repeatable on other iPhone 4 devices.
 
Upvote 0

Turbo3

Active Member
Licensed User
Longtime User
If you go to the Voice Memo app that comes with the iPhone 4 you can select one of two speakers for playback. Either the small one at the top of the phone used for normal phone calls or the one (two?) at the bottom of the phone used for speaker phone and music. So there is a program way to select between these two speakers.

Just tap the speaker icon at top right of the screen to select between these two speakers.
 
Upvote 0

Turbo3

Active Member
Licensed User
Longtime User
After a quick look at this page I found the problem. Keep in mind the iPhone 4 is just a test device for me, not my actual phone so I don't use it for anything else but testing.

https://developer.apple.com/library...ogrammingGuide/Introduction/Introduction.html

I found the solution when I read "Should your app's audio be silenced by the Ring/Silent switch?.."

So that is what that little switch is on the side of the phone;-)

So I toggled the switch and now I get sound with the B4i app.

Can B4i add an option to control whether to use this silence switch? The default would seem to be that B4i mediaplayer honors it which may not be what the developer wants to happen.

I think the default should be to not honor the silence switch like all the other apps that play sound.

This explains why the Piano app was able to play sound and B4i was not and also why the ear buds always worked.
 
Upvote 0
Top