problems with android examples in processing 3

edited October 2015 in Android Mode

answering a question about playing sound with android mode i have given 2 solutions (one with APWidget lib, the other with android mediaPlayer class); the twos tested with 3 phones and processing 2, they run fine. However the user tells me that it does not work... and i understood (finally!) that he was using Processing 3 android mode. So i downloaded the last version of P3 (it is 3.06 beta , osX) and installed it on OSX, yosemite.

you can see the question && snippet i posted here: forum.processing.org/two/discussion/12819/how-do-i-get-sound-to-play-on-the-phone#latest

When i runned the code i have posted for android mediaPlayer class (because i was quite sure that APWidgets which was not updated since 2011 cannot work with P3) i firstly get an error from the compiler at the very beginning when i code:

AssetFileDescriptor afd = getAssets().openFd("myp3.mp3");

--- which is a line of code i have often used for that in Eclipse.

Compiler says, as the oop tells (see its picture) that getAssets() does not exists. Of course i have all imports ok.

----I thought that it was a problem with context.

So i tried ::

AssetFileDescriptor afd = this.getAssets().openFd("myp3.mp3");

---same error from ...

So i tried to add to imports the android Context class && wrote::

  (Context)  context = context.getApplicationContext();
    AssetFileDescriptor afd = context.getAssets().openFd("myp3.mp3");

--- here it accepts to compile and install the sketch on the phone... Happy! ----But once installed the app crashes and i get another error, NPE::

"Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference"

----which means that context is null

--- So i decided, in order to verify, to add a very simple class extending Application class from android:::

      public class Appverify extends Application  {

        private  Context ctx;

        public  Context getContext() {
            return ctxt;
        }

        public  void setContext(Context mContext) {
            this.ctxt = ctxt;
        }

    --- then i created an instance of Appverify and called this class + println(context) from its return::

--------result was as i was afraid of:: null

------- Here i begin to be really puzzled and decided to verify with the examples for android, compass && accelerometer -------and found exactly the same results

--------in the twos the constructor does not compile with "this"

(compass = new CompassManager(this);

--------if i change this for context (same method, imports and so on) the error disappears, it compiles, but the app crashes immediately on the phone with the same error "Attempt to invoke virtual method on a null object reference"

----- so, what to do???

last phone with which i tested is running lollipop 5.1, if it matters...

thanks in advance

PS another error i have found is that the line number indicated by console (for errors) does not match the left numbers for the coded lines.

Answers

  • edited October 2015

    I am currently grappling with the same issue. thanks for the work! I will post here if I have any breakthroughs

  • PS another error i have found is that the line number indicated by console (for errors) does not match the left numbers for the coded lines.

    the line numbers are for the pre-processed version, the .java, not the .pde, with added imports etc. it's probably somewhere in your /tmp directory or, if you export your application, then it's in there under the src (based on the linux version)

  • @sgrigg== i have tried a lot of things (contextWrapper class and so on) since my post: not any result till now...But it is not the first strange problem that i note with android mode processing 3, and usually don't use it...

  • @ koogs:: ah, ok; yet not very easy to use...

  • ---tried also writing (in order to learn, because i do know that it is stupid as P3 android mode inherits only from Object)

    (Context) context = super.getApplicationContext();

    returns:: error The method getApplicationContext() is undefined for the type PApplet... so WHAT TO DO???

    thanks in advance

  • @koogs && others is android mode supposed (like processing java mode) to make things simpler than they are ( i know java && processing) when using eclipse or netbeans??? ---not sure that the goal is obtained, till now!! (i see a lot of students who try to code android with processing && cannot do the first steps:: installing - and when somebody ("i")answer saying::: (depends /os) open terminal create .bash profile an so on) is it in the processing spirit???

  • @androidSpecialistsProcessing:: sorry to insist Who can explain me how to run the android examples with processing 3??? -i am mot completely stupid -- they run with 2 -- they don't run (is it my phone???-- i don't think so...see post before)

    more generally how can i access (P3) to ApplicationContext && context??

  • edited October 2015

    @sgrigg @koogs @others

    Finally i have solved the problem by myself.

    Not so easy.

    1) understanding what p3 android is doing:

    A) creating an activity (launcher) called "MainActivity" as you can see from the Manifest B) Creating a frame Layout and a fragment C) Creating a class from your code (new yourClass()) and extending it to PApplet D) Adding that to the fragment

    ----- i cannot assume that it is exactly that (for B,C,D)

    In order to find or verify that i added some code in setup()::

        public void setup(){
        orientation (PORTRAIT);
        background(255,0,0);
        testClass();
        }
        public void draw(){
    
        }
    
         protected void testClass() {
    
            Class<?> WhatClass = this.getClass();
            while (WhatClass!= null) {
              println("Classe=========" + WhatClass);
              WhatClass = WhatClass.getSuperclass();
              if (WhatClass != null) {
                if (WhatClass.toString().toLowerCase().indexOf("android.app.") > -1) {
                  AppliAndroid = true;
                  break;
                }else{
                  println("Class=======instanceof");
                }
              }
            }
                };
    

    Result in console is:: [my android sketch is called forumtetu];

    Classe=========class processing.test.forumtetu.forumtetu==== my class Class=======instanceofclass processing.core.PApplet------which extends What SuperClass===class android.app.Fragment===== and is inside fragment

    ---Knowing that i understand why, in the first class i cannot get "context", even adding "super". ---Hence the solution:: as the whole stuff is made by the activity launcher called MainActivity:::

    I have to get this activity, which can be made in a a very classical way in a fragment using getActivity(); then you can get the context (ApplicationContext()) and finally you can do what you want.

    So if you want that the android examples run with P3 (e.g. the accelerometer) you have to take the initial code and change it to:

        import android.content.Context;
        import android.app.Activity;
        ////
        AccelerometerManager accel;
        float ax, ay, az;
        Activity act;
        Context context;
    
    
       void setup() {
          act = this.getActivity();
          context = act.getApplicationContext();
    
          accel = new AccelerometerManager(context);
          orientation(PORTRAIT);
          noLoop();
        }
    
    
        void draw() {
          background(0);
          fill(255);
          textSize(70);
          textAlign(CENTER, CENTER);
          text("x: " + nf(ax, 1, 2) + "\n" + 
               "y: " + nf(ay, 1, 2) + "\n" + 
               "z: " + nf(az, 1, 2), 
               0, 0, width, height);
        }
    
    
        public void resume() {
          if (accel != null) {
            accel.resume();
          }
        }
    
    
        public void pause() {
          if (accel != null) {
            accel.pause();
          }
        }
    
    
        public void shakeEvent(float force) {
          println("shake : " + force);
        }
    
    
        public void accelerationEvent(float x, float y, float z) {
        //  println("acceleration: " + x + ", " + y + ", " + z);
          ax = x;
          ay = y;
          az = z;
          redraw();
        }
    

    //test (with P3) with the initial code, then test with my solution (that i have tried with real devices Samsung && Sony, gingerbread, lollypop) and tell me (perhaps problems with other phones or OS; tell me)

    ///And now i can answer to the first question (putting sound on my phone)!

  • oh wow! I will check it out on my end when I get a chance. thanks for all your hard work @akenaton

  • so I tested on my samsung tab 3 running 4.4.2. The accelorometer example would never even build before but now it's building and displaying properly with no crashes. The variables aren't by updated by the sensor unfortunately.

    with the ApMediaPlayer example, I copied your code from the other thread and it launches and I get a red screen that says "your song is playing" but there is no sound. Before when working with Apwidgets my projects would crash upon running so this is a step forward but I still don't have sound. I have tried to find any errors on my end and I am pretty confident I have done everything correctly. My console is not throwing anything to indicate an error.

    Thanks again for your work @akenaton

  • @sgrigg== as for the accele i cannot solve the way in which this code is supposed to work!!!- Happy to see that i am not crazy && that this example (or Compass) which didn't run nor compile now are running as for sund i dont understand whu you are speaking about APWidgets; my code (the final one) does not use this library, probably not updated && unable to work with P3.Anyway hou have to put a sound in data (the name of the sound is hardcoded in my snippet, change it of course); looking at that i see also that, working i have created an "assets" folder at the same level and put the sound also in this folder. Perhaps it s the reason , i have to verify with and without it.

  • @sgrigg== as for the accele i cannot solve the way in which this code is supposed to work!!!- Happy to see that i am not crazy && that this example (or Compass) which didn't run nor compile now are running as for sund i dont understand whu you are speaking about APWidgets; my code (the final one) does not use this library, probably not updated && unable to work with P3.Anyway you have to put a sound in data && perhaps create the assets folder && put also the sound in it

  • @akenaton sorry I forgot what I was doing didn't mean to mention APWidgets.Last night when I was testing I had the mp3 in a data folder and I changed the name in the code to test.mp3. This morning I tested with an identical "assets" folder. It still launches to "SONG IS PLAYING" on red screen but no sound. it is throwing the "unable to prepare" from the IOException catch.

  • @sgrigg = strange, perhaps that when i copy pas from the initial code (which was much more sophisticated because i was trying to understand what processing was doing in background i forgot something; what is sure is that error is "classical" with the media player class && usually can be easily solved; yet ioe means that the problem is that it does not find the file. I have to look at these things, tomorrow.

  • Answer ✓

    @akenaton okay sorry the problem was on my end. I needed to remove the id3 tags from the mp3 for the code to be able to find it. in the past I've gotten by just changing names but I guess one of the id3 tags was confusing it.

    but mp3s perfectly now! thanks so much @akenaton!

Sign In or Register to comment.