Java Question How to make "Nested" mebers in a Library?

fjfj

Member
Licensed User
Hello, i am about to make an Audiomanager Library, but to have clearer code, i would like to nest the SetVolume Member

A.I:

B4X:
dim aml as AudioManagerLib      'that would be my lib
aml.initialize("")        'no events
aml.SetVolume.Music       ' keep in mind that ".Music" Part, for not it is aml.SetVolumeMusic

So i would like to be able to aml.SetVolume.Music for STREAM_MUSIC or aml.SetVolume.Ringer for STREAM_RING
instead of aml.SetVolumeMusic or aml.SetVolumeRinger

Here is part of my code for the Library:
B4X:
import android.media.AudioManager;
import android.view.KeyEvent;
import anywheresoftware.b4a.BA;
import anywheresoftware.b4a.BA.ShortName;
import anywheresoftware.b4a.BA.Version;

@Version(value = 1)
@ShortName("AudioManagerLib")
public class AudioManagerLib   {private AudioManager mAudioManager;
    public AudioManagerLib() {
        
    }
    
    /**
     * Minimum APK is 19 for this Method
     * Just type (""), no events in this Library
     * Example:
     * <code> Dim  aml as AudioManagerLib</code>
     * <code> aml.initialize("")</code>
     */   
    public void initialize(final BA ba, String EventName){
        mAudioManager = (AudioManager)ba.applicationContext.getSystemService(ba.applicationContext.AUDIO_SERVICE);
    }
    
    /**
     * Minimum APK is 19 for this Method
     * Toggles Play/Pause Audio from any active Player
     * Example:
     * <code> Dim  aml as AudioManagerLib</code>
     * <code> aml.initialize("")</code>
     * <code> aml.TogglePLayPause</code>
     */   
    public void TogglePLayPause() {   
    KeyEvent downEvent = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE);
        mAudioManager.dispatchMediaKeyEvent(downEvent);

    KeyEvent upEvent = new KeyEvent(KeyEvent.ACTION_UP,KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE);         
        mAudioManager.dispatchMediaKeyEvent(upEvent);
    }

 public void SetVolumeMusic(int i){
     mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, i, 0);
     }
}

Any Hints or better code Example, something like an empty Library with nested Member Placeholders or such would be much appreciated.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
No need to add <code> for each row.
B4X:
 /**
     * Minimum APK is 19 for this Method
     * Just type (""), no events in this Library
     * Example:
     * <code> Dim  aml as AudioManagerLib
     * aml.initialize("")</code>
     */

SetVolume should return an object of a different class with the various methods. However it looks like a strange API.

You should instead pass a constant defining the stream.
Note that it is already implemented in Phone.SetVolume.
 

fjfj

Member
Licensed User
Well it allows to Toggle, Play or Pause etc. any/most Audio playing Apps (like Stitcher, Soundcloud, Mort Player etc.) from another App like a Screensaver (it works)
And Provides easy to use Members for that.

How about that nested Members Part?
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…