iOS Question B4i playing MP3 file

james_sgp

Active Member
Licensed User
Longtime User
I`m trying to play an MP3 file from my B4XPages app, here is what I`m doing...

B4X:
Process Globals
  Public mp1 As MediaPlayer
  ....
 
On B4Xpages
    Main.mp1.Initialize(File.DirAssets,"106.mp3","MPlayer")
    Main.mp1.Volume = 10
    Main.mp1.play

The MP3 plays when using the simulator, but not on B4i Bridge or real devices (tested on iPhone 6 & 10).


Thanks...James
 

roumei

Active Member
Licensed User
Your code works for me, so I guess the error must be somewhere else. Could you upload a small project that demonstrates the issue?
Notice that the volume should be between 0 and 1.
 
Upvote 0

james_sgp

Active Member
Licensed User
Longtime User
Here is a small app, that shows my issue...

Thanks for any advice.
 

Attachments

  • test1.zip
    65.9 KB · Views: 239
Upvote 0

roumei

Active Member
Licensed User
Thank you. The issue is that you're playing the sound in B4XPage_Created, so it will only be played once. Move the call to play_voice to the B4XPage_Appear event and it should work.

B4X:
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("main")
    
    ' Remove the call to play_voice from B4XPage_Created
    'play_voice("106.mp3")
    
End Sub

' Add the _Appear event and play the sound here
Private Sub B4XPage_Appear
    play_voice("106.mp3")
End Sub
 
Upvote 0

james_sgp

Active Member
Licensed User
Longtime User
Roumei, Actually in my app 'Play_voice' is called multiple times through the app; none of them work.

As you suggested, i changed the code to this:

B4X:
private Sub B4XPages_appear
        
    play_voice("106.mp3")
    
End Sub

But still does not play...
 
Upvote 0

james_sgp

Active Member
Licensed User
Longtime User
Didn`t make any difference, still doesn`t play. I stepped through the code and confirmed it's calling Play_voice...
 
Upvote 0

roumei

Active Member
Licensed User
That's strange, it certainly works for me. Did you check that your phone can play other sounds?
 
Upvote 0

james_sgp

Active Member
Licensed User
Longtime User
Yep, as I said it's on 2 different iPhones ( a 6 and a 10). And I`ve tried the MP3 file on my iPhone 6 and it plays. So definitely the app issue.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
1. Use the zipper comment link to create the zip file.
2. It is never a good idea to initialize objects of other modules.
3. Don't use File.Exists with File.DirAssets. It is not needed as you know that the file is there. It can fail as it is not a real folder.
4. Why initialize the MediaPlayer each time?

B4X:
Sub Class_Globals
    Private Root As B4XView 'ignore
    Private xui As XUI 'ignore

    Private info_btn As Button
    Private brand_pnl As Panel
    Private MediaPlayer As MediaPlayer
    
End Sub

'You can add more parameters here.
Public Sub Initialize As Object
    Return Me
End Sub

Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    'load the layout to Root
    Root.LoadLayout("main")
    MediaPlayer.Initialize(File.DirAssets,"106.mp3","MPlayer")
End Sub

Private Sub B4XPage_Appear
    MediaPlayer.play
End Sub
 
Upvote 0

james_sgp

Active Member
Licensed User
Longtime User
1. ok
2. ok
3. ok
4. how to give mediaplayer the file to play without initializing it? I actually have about 30 different MP3's that play throughout the app.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
If you need to play different files then you should initialize it again.
I don't expect it to cause any problem, but if it does then you can use multiple MediaPlayers, each one with its own file, or use X2SoundPool class which depends on iGameView.

 
Upvote 0

james_sgp

Active Member
Licensed User
Longtime User
Erel, Ok, thanks...so from your view, there is nothing wrong with the B4XPages code I`m using in the Zip example I shared with 'roumei' (post #3)? Because it still doesn`t work for me on B4ibridge or 2 different actual devices.
 
Upvote 0

james_sgp

Active Member
Licensed User
Longtime User
Erel, here is my text B4Xpages app using iGameView...it also does not play the MP3 :eek:

Obviously, I have something fundamentally wrong...is it because I`m using B4xpages?
 

Attachments

  • test1a.zip
    65.8 KB · Views: 206
Upvote 0

ilan

Expert
Licensed User
Longtime User
4. Why initialize the MediaPlayer each time?

if you are making a game and would like to play a sound but don't wait until it finished and play the same sound again (before the first one has finished) like in a breakout game where you have a ball that hits multiple bricks in a shorter time then the sound file length then the only way to do that would be each time initialize a new mp and play the sound. with b4j and b4a i had no problem although b4i doesn't like it much (i guess it has something to do with iOS)

you can use multiple MediaPlayers

this is the solution for b4i that will work although you cannot play the same sound simultaneously (like in the problem explained above)

or use X2SoundPool class which depends on iGameView.

this is a better solution!

is there a way to have X2SoundPool as a library (or class) without the need to include the whole X2 library?
 
Last edited:
Upvote 0

ilan

Expert
Licensed User
Longtime User
Erel, here is my text B4Xpages app using iGameView...it also does not play the MP3 :eek:

Obviously, I have something fundamentally wrong...is it because I`m using B4xpages?

X2SoundPool is part of the X2 library (as far as i remember)
i have opened your project but don't see any code of playing sound.

how are you trying to play the sound file?
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
if you are using SoundPool it should look like this:

B4X:
Sub Class_Globals
    Private sp As X2SoundPool
End Sub

initialize SoundPool

B4X:
Public Sub Initialize
    sp.Initialize
    sp.AddSound("106",File.DirAssets,"106.mp3")
    'sp.AddSound("",File.DirAssets,"*.mp3") ' add more sound files   
End Sub

play the sound by clicking on button1

B4X:
Sub Button1_Click
    sp.PlaySound2("106",1)
End Sub
 
Upvote 0

james_sgp

Active Member
Licensed User
Longtime User
ilan, thanks for the advice...I have tried Xsoundpool as you suggested (see attached app). It also doesn`t play the MP3!!! AAARGGHHHH

I`m using B4i 7.20, B4i Build Server 7.26 and XCode 12.4...

There must be a problem with my setup?
 

Attachments

  • test1b.zip
    66.8 KB · Views: 193
Upvote 0

ilan

Expert
Licensed User
Longtime User
your code is wrong.

on B4XPage_Appear you call a function named play_voice

B4X:
Private Sub B4XPage_Appear
    play_voice("106.mp3")
End Sub

but all you do is initialize the SoundPool object

B4X:
Sub play_voice(fn As String)
    sp.Initialize
    sp.AddSound("106",File.DirAssets,"106.mp3")
End Sub

you never call the sp.PlaySound() function.

just do see if everything works write the same code as i did above.

initialize SoundPool object in the initialize Event

B4X:
Public Sub Initialize
    sp.Initialize
    sp.AddSound("106",File.DirAssets,"106.mp3")
    'sp.AddSound("",File.DirAssets,"*.mp3") ' add more sound files 
End Sub

and call the playSound function in a button click event

B4X:
Sub Button1_Click
    sp.PlaySound2("106",1)
End Sub

somthing like this:

B4X:
Sub Class_Globals
    Private Root As B4XView 'ignore
    Private xui As XUI 'ignore

    Private info_btn As Button
    Private brand_pnl As Panel
   
    Private sp As X2SoundPool
End Sub

'You can add more parameters here.
Public Sub Initialize
    sp.Initialize
    sp.AddSound("106",File.DirAssets,"106.mp3")
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    'load the layout to Root
    Root.LoadLayout("main")
End Sub

Private Sub B4XPage_Appear

End Sub

Sub info_btn_Click
    sp.PlaySound2("106",1)
End Sub
 
Last edited:
Upvote 0

james_sgp

Active Member
Licensed User
Longtime User
Nope, that doesn`t work either...

I also tried a non-B4XPages version (to see if it was related); which it seems not! As this doesn`t play the MP3 either ????

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'Public variables can be accessed from all modules.
    Public App As Application
    Public NavControl As NavigationController
    Private Page1 As Page
    Private xui As XUI
    
    Private sp As X2SoundPool
End Sub

Private Sub Application_Start (Nav As NavigationController)
    NavControl = Nav
    Page1.Initialize("Page1")
    Page1.RootPanel.LoadLayout("Page1")
    NavControl.ShowPage(Page1)
    
    sp.Initialize
    sp.AddSound("106",File.DirAssets,"106.mp3")
End Sub

Sub Button1_Click
    xui.MsgboxAsync("Hello world!", "B4X")
    sp.PlaySound2("106",1)
End Sub

Private Sub Page1_Resize(Width As Int, Height As Int)
    
End Sub
 
Last edited:
Upvote 0
Top