Android Question Access AudioManager from a module

Jmu5667

Well-Known Member
Licensed User
Longtime User
Hi All

I need to access the android.media.AudioManager from a module, not an activity. I get the following error when compiling:

B4A version: 6.80
Parsing code. (0.09s)
Compiling code. (0.32s)
Compiling layouts code. (0.04s)
Organizing libraries. (0.00s)
Generating R file. (0.07s)
Compiling generated Java code. Error
B4A line: 74
End Sub
javac 1.8.0_40-ea
src\com\islesystems\pushtotalk\mod_audio_manager.java:164: error: cannot find symbol
AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
^
symbol: variable AUDIO_SERVICE
location: class mod_audio_manager

B4X:
'Code module
'Subs in this code module will be accessible from all modules.
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
   Private nativeMe As JavaObject
   
End Sub

Public Sub Init
   'lower cased module name
   nativeMe.InitializeStatic(Application.PackageName & ".mod_audio_manager")
   
End Sub




Sub setSpeakerPhone(state As Boolean)
   
   
   Dim rr As Reflector
   rr.Target = rr.GetContext
   rr.Target = rr.RunMethod2("getSystemService", "audio", "java.lang.String")
   rr.RunMethod2("setSpeakerphoneOn", state, "java.lang.boolean")
   
   
End Sub

Sub set_audio_mode(pMode As Int)
   
   Dim r As Reflector
   r.Target = r.GetContext
   r.Target = r.RunMethod2("getSystemService", "audio", "java.lang.String")
   r.RunMethod2("setMode", pMode, "java.lang.int")
   
End Sub

Sub get_audio_mode() As Int
   
   Dim r As Reflector
   r.Target = r.GetContext
   r.Target = r.RunMethod2("getSystemService", "audio", "java.lang.String")
   Return r.RunMethod("getMode")
   
End Sub


Sub turnSpeakerOn(pState As Boolean)
   
   
   Dim p As Phone
   'Dim java As JavaObject

   'java.InitializeContext   
   
   If p.SdkVersion < 21 Then
     If pState Then
       nativeMe.RunMethod("setSpeakerOn_20",Null)
     Else
       nativeMe.RunMethod("setSpeakerOff_20",Null)
     End If     
   Else
     If pState Then
       nativeMe.RunMethod("setSpeakerOn_21",Null)
     Else
       nativeMe.RunMethod("setSpeakerOff_21",Null)
     End If
     
     'nativeMe.RunMethod("setSpeakerOn_21",Array(pState))
   End If
       
End Sub

Sub setMicrophoneMute(pState As Boolean)
   
   
   
   Dim p As Phone
   
   If p.SdkVersion < 21 Then
     If pState Then
       nativeMe.RunMethod("setMicroPhoneMuteOn_20",Null)
     Else
       nativeMe.RunMethod("setMicroPhoneMuteOff_20",null)
     End If
     
   Else
     nativeMe.RunMethod("setMicroPhoneMute_21",Array(pState))
   End If
   
   ' // hack to set voice call volume to zero
   If pState Then
     Main.APPSET.currentChannel.voice_call_vol = p.GetVolume(p.VOLUME_VOICE_CALL)
     p.SetVolume(p.VOLUME_VOICE_CALL,0,False)
   Else
     p.SetVolume(p.VOLUME_VOICE_CALL,Main.APPSET.currentChannel.voice_call_vol,False)
   End If

   ' // http://www.programcreek.com/java-api-examples/index.php?class=android.media.AudioManager&method=setMicrophoneMute
   Dim Sleep As Lock
   Sleep.Initialize(True)
   Sleep.WaitFor(200)
   Sleep.Unlock
   
   
End Sub


#If JAVA
  import android.media.AudioManager;
  public void setSpeakerOn_20() {
  AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
  audioManager.setMode(AudioManager.MODE_NORMAL);
  audioManager.setSpeakerphoneOn(true);
  }
#End if
#If JAVA
  import android.media.AudioManager;
  public void setSpeakerOff_20() {
  AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
  audioManager.setMode(AudioManager.MODE_NORMAL);
  audioManager.setSpeakerphoneOn(false);
  }
#End if



#If JAVA
  import android.media.AudioManager;
  public void setSpeakerOn_21() {
  AudioManager audioManager= (AudioManager) getSystemService(AUDIO_SERVICE);
  audioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
  audioManager.setSpeakerphoneOn(true);
  }
#End if

#If JAVA
  import android.media.AudioManager;
  public void setSpeakerOff_21() {
  AudioManager audioManager= (AudioManager) getSystemService(AUDIO_SERVICE);
  audioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
  audioManager.setSpeakerphoneOn(false);
  }
#End if

#If JAVA
   import android.media.AudioManager;
  public void setMicroPhoneMute_21(boolean Value) {
   
    AudioManager audioManager= (AudioManager) getSystemService(AUDIO_SERVICE);
     
    if (Value)
       {
       audioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
       audioManager.setMicrophoneMute(Value);
     }
     else
     {
         audioManager.setMode(AudioManager.MODE_NORMAL);
       audioManager.setMicrophoneMute(Value);   
     }
     
   }
#End if

#If JAVA
   import android.media.AudioManager;
  public void setMicroPhoneMuteOn_20() {
   
    AudioManager audioManager= (AudioManager) getSystemService(AUDIO_SERVICE);
    audioManager.setMode(AudioManager.MODE_IN_CALL);
  audioManager.setMicrophoneMute(true);   
     
   }
#End if

#If JAVA
   import android.media.AudioManager;
  public void setMicroPhoneMuteOff_20() {
   
    AudioManager audioManager= (AudioManager) getSystemService(AUDIO_SERVICE);
    audioManager.setMode(AudioManager.MODE_NORMAL);
  audioManager.setMicrophoneMute(false);   
     
   }
#End if


#End Region

All Help apperciated.

Regards

John.
 

Jmu5667

Well-Known Member
Licensed User
Longtime User
Don't use code modules in B4A. Use a class instead. It will share the context of its "parent" module.


Hi Erel

Em....I get this error after using your suggestion:

src\com\islesystems\pushtotalk\cls_audio.java:139: error: cannot find symbol
AudioManager audioManager= (AudioManager) getSystemService(AUDIO_SERVICE);
^
symbol: variable AUDIO_SERVICE
location: class cls_audio

This is the class

B4X:
Sub Class_Globals
  
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize

End Sub

Sub MicrophoneMute(pState As Boolean)
  
  
  
   Dim p As Phone
   Dim java As JavaObject

   java.InitializeContext
  
   If p.SdkVersion < 21 Then
     java.RunMethod("setMicroPhoneMute_20",Array(pState))
   Else
     java.RunMethod("setMicroPhoneMute_21",Array(pState))
   End If

   ' // http://www.programcreek.com/java-api-examples/index.php?class=android.media.AudioManager&method=setMicrophoneMute
   Sleep(200)
  
  
End Sub



#If JAVA
   import android.media.AudioManager;
  public void setMicroPhoneMute_21(boolean Value) {
   
    AudioManager audioManager= (AudioManager) getSystemService(AUDIO_SERVICE);
     
    if (Value)
       {
       audioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
       audioManager.setMicrophoneMute(Value);
     }
     else
     {
         audioManager.setMode(AudioManager.MODE_NORMAL);
       audioManager.setMicrophoneMute(Value);   
     }
     
   }
#End if

#If JAVA
   import android.media.AudioManager;
  public void setMicroPhoneMute_20(boolean Value) {
   
    AudioManager audioManager= (AudioManager) getSystemService(AUDIO_SERVICE);
     
    if (Value)
       {
       audioManager.setMode(AudioManager.MODE_IN_CALL);
       audioManager.setMicrophoneMute(Value);
     }
     else
     {
         audioManager.setMode(AudioManager.MODE_NORMAL);
       audioManager.setMicrophoneMute(Value);   
     }
     
   }
#End if


Am I missing something ?

Regards

John.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Yes. This code will only run properly from inside an Activity module. However you can easily implement it with JavaObject.

This will allow you to get the context in a class or activity.

Example:
B4X:
Sub SetMicroPhoneMute_21(value As Boolean)
   Dim ctxt As JavaObject
   ctxt.InitializeContext
   Dim audioManager As JavaObject = ctxt.RunMethodJO("getSystemService", Array(ctxt.GetField("AUDIO_SERVICE")))
   Dim audioManagerStatic As JavaObject
   audioManagerStatic.InitializeStatic("android.media.AudioManager")
   If value Then
     audioManager.RunMethod("setMode", Array(audioManagerStatic.GetField("MODE_IN_COMMUNICATION")))
   Else
     audioManager.RunMethod("setMode", Array(audioManagerStatic.GetField("MODE_NORMAL")))
   End If
   audioManager.RunMethod("setMicrophoneMute", Array(value))
End Sub
 
Upvote 0
Top