iOS Question B4i playing MP3 file

james_sgp

Active Member
Licensed 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
 

james_sgp

Active Member
Licensed User
Nope, the code from post 10 didn`t work.... I have been going through the forum systematically trying every option. I just found this on to work. from this post.


I don`t really understand why....

B4X:
Sub Process_Globals
    Public App As Application
    Public NavControl As NavigationController
    Private Page1 As Page
    Private mp As MediaPlayer
End Sub

Private Sub Application_Start (Nav As NavigationController)
    NavControl = Nav
    Dim jo As NativeObject = Me
    jo.RunMethod("setAudioSession", Null)
    Page1.Initialize("Page1")
    NavControl.ShowPage(Page1)
    mp.Initialize(File.DirAssets, "106.mp3", "mp")
    mp.Play
End Sub


#If OBJC
#import <AVFoundation/AVFoundation.h>
- (void) setAudioSession {
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
BOOL ok;
NSError *setCategoryError = nil;
ok = [audioSession setCategory:AVAudioSessionCategoryPlayback
  error:&setCategoryError];
if (!ok) {
  NSLog(@"%s setCategoryError=%@", __PRETTY_FUNCTION__, setCategoryError);
}
}
#end if
 
Upvote 0

Alex_197

Active Member
Licensed User
Maybe a stupid question - are you sure that MP3 files can be played on the same phone with a native MP3 player just by click on them in file explorer?
Are you sure that they are not corrupted?
 
Upvote 0
Top