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

Discussion in 'Libraries developers questions' started by fjfj, Apr 4, 2018.

  1. fjfj

    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:

    Code:
    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:
    Code:
    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.
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    No need to add <code> for each row.
    Code:
    /**
         * 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.
     
  3. fjfj

    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?
     
  4. Erel

    Erel Administrator Staff Member Licensed User

    You need to return an object from SetVolume and that object should include the nested members.
     
  5. fjfj

    fjfj Member Licensed User

Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice