Android Tutorial [java] Creating libraries for B4A

agraham

Expert
Licensed User
Longtime User
Sorry, my mistake :( though I would have expected a line of Java code with the error to be displayed after the "javac 1.6.0_20" line :confused:. Does Mp3tag appear in the Intellisense after typing "Dim As"? If not then you have probably not used an @ShortName attribute on it in the Java code or you have not created the Javadoc xml correctly.
 
Last edited:

Gigatron

Member
Licensed User
Longtime User
Echo library does not work !!

Ok after 2 days with eclipse i am here


After question What's people like on android market the result is;

Games and Audio %90 ....

For this reason i made little MP3 player with B4A .. Many thanks again to Erel Agraham Stevel05 and all i forgot.

I had read 100 times how done library to extend B4A functions, and compile wihout errors in eclipse mp3tag library, audio reverb ....

I asked help to Steve .
With help from Steve i compiled a small library under API 9.

Echo library is PreserReverb from android, all my work is based from
Steve EQualizer library.

So can someone test my library ? Whats wrong ? I have tested but
no echo in playing mp3 file ?

Later
 

Attachments

  • testlib.zip
    4.8 KB · Views: 856
Last edited:

Gigatron

Member
Licensed User
Longtime User
Here is the entire code of eclipse


B4X:
package com.gtr.echoing;

 


//import android.os.Build;
import android.media.audiofx.AudioEffect;
import anywheresoftware.b4a.BA.Author;
import anywheresoftware.b4a.BA.ShortName;
import anywheresoftware.b4a.BA.Version;
import anywheresoftware.b4a.BA.Permissions;


@ShortName("Echo")
@Author("GTR")
@Version(1.20f)
@Permissions(values={"android.permission.MODIFY_AUDIO_SETTINGS"})

  public class Echo{
  
   /**
     * Preset. Parameter ID for
     * {@link android.media.audiofx.PresetReverb.OnParameterChangeListener}
     */
    public static final int PARAM_PRESET = 0;

    /**
     * No reverb or reflections
     */
    public static final short PRESET_NONE        = 0;
    /**
     * Reverb preset representing a small room less than five meters in length
     */
    public static final short PRESET_SMALLROOM   = 1;
    /**
     * Reverb preset representing a medium room with a length of ten meters or less
     */
    public static final short PRESET_MEDIUMROOM  = 2;
    /**
     * Reverb preset representing a large-sized room suitable for live performances
     */
    public static final short PRESET_LARGEROOM   = 3;
    /**
     * Reverb preset representing a medium-sized hall
     */
    public static final short PRESET_MEDIUMHALL  = 4;
    /**
     * Reverb preset representing a large-sized hall suitable for a full orchestra
     */
    public static final short PRESET_LARGEHALL   = 5;
    /**
     * Reverb preset representing a synthesis of the traditional plate reverb
     */
    public static final short PRESET_PLATE       = 6;

      
   
   
      private static boolean IsAvailable = false;
      private static boolean IsInit = false;
     

      
      /* Check if required API is available even before compiling so as to avoid runtime errors
       *
       */
       
      static {
          try {
             Echovalid.CheckAvailable();
              IsAvailable=true;
          }catch (Throwable t) {
              IsAvailable=false;
          }
          
      }
      
      
      /**
       * Checks whether the Echo is available and Initializes the object,
       * only for all output and not for specific media players.
       * For backwards compatibility, the library will not throw an exception 
       * on failure to initialize the Equalizer, but will return true or false 
       * and set the IsInitialized flag appropriately if it fails for any reason.
       * check either of these before running the other methods as if the equalizer
       * is not initialized, you will get a runtime error.
       *  
       */
      @SuppressWarnings("finally")
   public boolean Initialize() {
          try {
             Echovalid.Initialize();
              IsInit=true;
          } catch (Throwable t) {
              IsInit = false;
          } 
          finally 
          { return IsInit;
          }
          
      }

          /**
           * Enables or disables the Audio effect engine.
           */
          public static int Enable(boolean enable)throws Exception {
              if (!IsInitialized()) {
                  throw new Exception("Echo should be initialized first");
              }
              return Echovalid.Enable(enable);
          }
          
          /**
           * Gets the Enabled status of the Audio effect engine.
          */            
          public static boolean GetEnabled()throws Exception {
              if (!IsInitialized()) {
                  throw new Exception("Echo should be initialized first");
              }
              return Echovalid.GetEnabled();
          }
          
          /**
           * Checks if this AudioEffect object is controlling the effect engine
           */
          public static boolean HasControl()throws Exception {
              if (!IsInitialized()) {
                  throw new Exception("Echo should be initialized first");
              }
              return Echovalid.HasControl();
          }
          /**
           * Releases the native AudioEffect resources. 
           * It is a good practice to release the effect engine when not 
           * in use as control can be returned to other 
           * applications or the native resources released.
           */
          
          public static void Release()throws Exception {
              if (!IsInitialized()) {
                  throw new Exception("Echo should be initialized first");
              }
              Echovalid.Release();
          }
          
           /**
           * Returns the status of the Equalizer object, it must be available for
           * the devices API level and initialized
           * 
           */
          
          public static boolean IsInitialized(){
          if (IsAvailable && Echovalid.IsInitialized()) {
              return true;
          } else {
              return false;
          }
          }
          
                   /**
           * Returns the availability of the Equalizer object, it must be available for
           * the devices API level (9 or later) 
           */
           public boolean IsAvailable(){
               return IsAvailable;
           }
           
            
           
            
           
           // Echo Start here
           
          
           /**
            *  Enables a preset on the reverb.
            *  <p>The reverb PRESET_NONE disables any reverb from the current output but does not free the
            *  resources associated with the reverb. For an application to signal to the implementation
            *  to free the resources, it must call the release() method.
            * @param preset this must be one of the the preset constants defined in this class.
            * e.g. {@link #PRESET_SMALLROOM}
            * @throws IllegalStateException
            * @throws IllegalArgumentException
            * @throws UnsupportedOperationException
            */
           public void setPreset(short preset)throws Exception {
               if (!IsInitialized()) {
                   throw new Exception("Echo should be initialized first");
               }
                Echovalid.setPreset(preset);
           }
           
           
           
           public void onParameterChange(AudioEffect effect, int status, byte[] param, byte[] value)throws Exception {
               if (!IsInitialized()) {
                   throw new Exception("Echo should be initialized first");
               }
               Echovalid.onParameterChange(null, status, param, value);
               {
                
         
           
           
               }
           }
           
}


And Ecvalide Class

B4X:
package com.gtr.echoing;

import android.media.audiofx.AudioEffect;
import android.media.audiofx.PresetReverb;

public class Echovalid {

    private static boolean IsInit = false;
    private static PresetReverb EC;
    
    static {
        try {
            Class.forName("android.media.audiofx.PresetReverb");
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }

    public static void CheckAvailable() {}
    
    public static void Initialize() {

    try {
        EC = new PresetReverb(0, 0);
        IsInit=true;
    } catch (RuntimeException runtimeException) {
        IsInit=false;
    }

    }

    public static boolean IsInitialized(){
        return IsInit;
    }

    public static int Enable(boolean enable){
        return EC.setEnabled(enable);
    }
    
    public static boolean GetEnabled(){
        return EC.getEnabled();
    }
    
    public static boolean HasControl(){
        return EC.hasControl();
    }
    
    public static void Release(){
        IsInit=false;
        EC.release();
    }
    
    
    public static void onParameterChange(AudioEffect effect, int status, byte[] param, byte[] value) {
        EC.setParameterListener(null);
    }
    
     
   
    
    
    public static void setPreset(short preset){
          EC.setPreset(preset);
           }
    
    
    
    
    
    
}
 

stevel05

Expert
Licensed User
Longtime User
Hi Gigatron,

The first thing I notice is that the setPreset method is not available within B4A, did you run the JavaDocs before you put that in?

Steve
 

Gigatron

Member
Licensed User
Longtime User
Hi Steve

Thank you for your help, after over 100 compilation i failed ...Echo lib no interract whith mp3 player...dont know why?

I will restart after meditation :)

There is no errors , and java doc was good, i can't see
setPreset method on B4A, i will work ...


Later
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Sure. Here is the relevant section from AdMob library:
@Version(1.3f)
@ShortName("AdView")
@Events(values={"ReceiveAd", "FailedToReceiveAd (ErrorCode As String)",
"AdScreenDismissed"})
@ActivityObject
@DontInheritEvents
@Permissions(values={"android.permission.INTERNET", "android.permission.ACCESS_NETWORK_STATE"})
@DependsOn(values={"GoogleAdMobAdsSdk"})
public class AdViewWrapper extends ViewWrapper<AdView> {
The B4A compiler will add GoogleAdMobAdsSdk.jar to the project during compilation.
 

gkumar

Active Member
Licensed User
Longtime User
Can I get a sample app for this?

Hello,

I have both eclipse installed and B4A installed in my laptop. I need a sample code of eclipse java code library used by B4a. anybody have posted already for this?
And what are the limitations while importing java classes to B4A?
 

gawie007

Member
Licensed User
Longtime User
Unable to create libraries

I apologise in advance! I do not normally ask for help.
It is not without trying (4 days so far) that I am still not able to create a simple library utilising the demo.
I am using Eclipse Juno 64 on my Windows laptop.
At one stage, I had Java 1.7 but have changed it in the program to 1.6.
I have gone over everything a hundred times.
Also for some reason, I am unable to log in to add attachments of the screen with this message.

If I select the javadoc ant build checkbox, this is the output to the XML file:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project default="javadoc">
<target name="javadoc">
<javadoc access="public" additionalparam="-b4atarget&quot;D:\Users\Gav\Downloads\B4A\BADoclet\FirstLib.xml&quot;" classpath="C:\Program Files (x86)\Anywhere Software\Basic4android\Libraries\Core.jar;C:\Program Files (x86)\Anywhere Software\Basic4android\Libraries\B4AShared.jar;C:\Android\android-sdk\platforms\android-4\android.jar" packagenames="anywheresoftware.b4a.sample" sourcepath="C:\Users\Gav\Eclipse\workspace\FirstLib\src">
<doclet name="BADoclet" path="D:\Users\Gav\Downloads\B4A\BADoclet"/>
</javadoc>
</target>
</project>

The common (that everyone seems to have) error I get is: No packages or classes specified but I have checked the project and it is checked all the way down to the class file.

Should I use an early version of Eclipse or is this Windows 7 stonewalling everything again - I have copied the java directories to my D drive so that they are not within the protected Program... directories.

Any assistance would be greatly appreciated!

Kind regards

Gavin

I have now installed Eclipse and Java SDK 1.7 & JRE 1.7 all 32 bit and have installed everything on my D: drive.
Still no change!
I must be missing something simple.
Is there a way of compiling a library without using the Doclet (The first general option button)- I have quickly tested this and it works (with one error - the badocklet reference which is obviously bypassed).

Kind regards

Gavin

I DID NOT HAVE A SPACE BETWEEN -b4atarget and "D....
-b4atarget "D:\Users\Gav\Downloads\B4A\BADoclet\FirstLib.xml"
:BangHead:
 
Last edited:

Roger Garstang

Well-Known Member
Licensed User
Longtime User
How important is it to reference android.jar version 4? The way I understand it they are all backward compatible and only when you use code not in the older api without checking the api version does it cause problems. I've read 2 Android Development books as well as online documents and they all said the same thing. In both normal Android Eclipse development and in B4A I started using the latest and saw no problems. Is there something different in a library requiring it? I'd think a newer jar would be needed, especially since the latest B4A started making more use of Honeycomb+ features.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
In the IDE you can target the latest Android SDK.

If you want to make sure that your library is compatible with Android 1.6 and above then it is recommended to target API level 4 (in Eclipse). Otherwise you may accidentally call APIs not available in Android 1.6.

If you make a library that requires such an API then you should target a more recent SDK.
 

fabioferreiracs

Member
Licensed User
Longtime User
Problem Using Twitter4j integration B4A

I am creating a prog that use integration
Twitter4j in Basic4Android.Twitter4j is a lib used to acess Twitter in ABtwitter by example.
well;
B4A program does a hometimeline requesting
asking for an external library jar (
ABTwitter example) and this (ABTwitter)
request Hometimeline by twitter4j.
Twitter4j responds to ABTwitter and it
responds to B4A program .
I want request hometimeline direct, asking
by twitter4j.
B4A suport external libs, so i intend
compile Twitter4j in Eclipse,
generating Twitter4j.jar and
Twitter4j.xml.
i thinking in calling the Twitter4J
methods directly. Compile it and creating Twitter4j jar and xml to import directly in B4A !
I found some issues :
B4A is alerting that , in import the compiled files , in StatusUpdate ,
that found 2 "setMedia" (see
above) :
Do you can help me about 2 setMedia? I
need resolve this conflict and other
similars.
In Twitter4j and in Eclipse no problem but B4A see this methods and other how duplicates!

Thanks

part source Twitter4J:

/**
* @since Twitter4J 2.2.5
*/
public void setMedia(File file) {
this.mediaFile = file;
}
/**
* @since Twitter4J 2.2.5
*/
public StatusUpdate media(File file) {
setMedia(file);
return this;
}
/**
* @since Twitter4J 2.2.5
*/
public void setMedia(String name,
InputStream body) {
this.mediaName = name;
this.mediaBody = body;
}
 
Top