iOS Question Solved - Media player play everything in iphone speaker

cloner7801

Active Member
Licensed User
Longtime User
Hi,
I am using media player for play a .dat file but its play file in iPhone speaker(local speaker).

How can I change it to external speaker?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Try it with this code:
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, "01.mp3", "mp")
  mp.Play
End Sub


#If OBJC
#import <AVFoundation/AVFoundation.h>
- (void) setAudioSession {
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker error: nil];
}
#end if
 
Upvote 0

cloner7801

Active Member
Licensed User
Longtime User
Try it with this code:
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, "01.mp3", "mp")
  mp.Play
End Sub


#If OBJC
#import <AVFoundation/AVFoundation.h>
- (void) setAudioSession {
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker error: nil];
}
#end if
No :(

My sound recorded by AudioRecorder and its format is .dat
 
Upvote 0

cloner7801

Active Member
Licensed User
Longtime User
I solve my problem.

I put this code after AudioRecorder initialize code :

B4X:
Dim jo As NativeObject = Me
jo.RunMethod("setAudioSession", Null)

And put this code at the end of codes :
B4X:
#If OBJC

#import <AVFoundation/AVFoundation.h>
- (void) setAudioSession {
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker error: nil];
}

#end if

Thank you @Erel
 
Upvote 0
Top