Howdy, Stranger!

We are about to switch to a new forum software. Until then we have removed the registration on this forum.

  • How to play 2 consecutive mp3 with MediaPlayer?

    Yes, that is not the proper way to do this. You need to look up the MeidaPlayer APi to see if it offer a callback when the file stops playing. Also feel free to explore previous entries with MediaPlayer keyword. Finally, please post in the new forum.

    Kf

  • How to play 2 consecutive mp3 with MediaPlayer?

    I am trying to play two consecutive mp3 (the second starting when the first ends). Is it possible?

    At the moment I use a delay but this solution is far from being optimal. Thank you in advance

          act = this.getActivity();
          context = act.getApplicationContext();
    
          try {
            s1 = new MediaPlayer();
            s2 = new MediaPlayer();
    
            af1 = context.getAssets().openFd("sound1.mp3");
            af2 = context.getAssets().openFd("sound2.mp3");
    
            s1.setDataSource(af1.getFileDescriptor(), af1.getStartOffset(), af1.getLength());
            s2.setDataSource(af2.getFileDescriptor(), af2.getStartOffset(), af2.getLength());
    
            s1.prepare();// prepare the sound for playing/usage
            s2.prepare();
    
            s1.start();
            delay(800);  // <-------------- ugly!
            s1.release();
            s1 = null;
    
            s2.start();
            delay(600); // <---------------
            s2.release();
            s2 = null;
          }
          catch(IOException e) {
          }
    
  • Video in Android Mode

    Really good comments @hudson_m4000

    Please don't forget to cross link your posts.

    I haven't had the time to check your code @flashbacker. I have had some issues myself with accessing Android res from processing. However, I know it is possible as it has been done before. I think we just need better documentation.

    Related to the wrapper for MediaPlayer, I believe @codeanticode has suggested something like that before. There are no enough hands on deck to do this type of work. Nevertheless, I agree it is important thing to do as it would make things much easier to implement, at least for the demonstration stage.

    These two links are relevant for anybody else reading this post:

    https://github.com/processing/processing-library-template
    https://github.com/processing/processing-android-library-template

    Kf

  • Video in Android Mode

    @flashbacker

    No luck so far! Like you I am a Processing fan rather than AS or Eclipse as I'm not a professional programmer! It is clear that anything like this dealing with video, the Processing way of being simple for us artists and designers just doesn't apply sometimes, especially for Android .... we need to know the SDK (and then workarounds!) to get things going after that, so even by looking at tutorial code websites, it's all for AS so can't be used in Processing without a lot of knowledge ... which we don't have! It would be great to have something like the Image class so we can just load a play the audio/video, but we don't ... and we struggle to even get anything.

    Why we you need to have to get the activity/context and then use MediaPlayer with VideoView etc ... why not just a method that has something like:

    myVideo = loadVideo("myvideo.mp4")

    and then just

    myVideo.play().

    I bet hundreds of experienced coders could easily wrap all the hard stuff in a VideoClasss for users like us ... but no one has ... :( I hope and pray that @akenaton may one day share his full working code in Processing sketch. I understand his desire for us all to learn (and move to AS .. but let's face it most of us won't because we buy into and like Processing) and is always so helpful, but this has been going for 3 years now and we still can't do it. Please share with us .... if I can have some working code, I will put it it a class to wrap it all and then see if I can get the Android dev team to add it to the Android Mode .. not a library as they always go out of date ... if it's in the mode itself then it's going to be there forever!

    'For now though I carry on tinkering and sort of learning :)

    I am going to stop posting here and start a new thread at the new forum to see if we can get some new insights .... see you there under my new userID of sessionShed :)

  • Video in Android Mode

    @kfrajer

    I've actually managed to use hudson_m4000 code to confirm what the path of my video file should be. Problem even with this info, my processing sketch won't find my file. The following code (even if I know it can't show the video image) compile on my device, but return to the console "error loading transit.mp4":

    import android.media.MediaPlayer;
    import android.os.Environment;
    import android.app.Activity;
    import android.content.Context;
    MediaPlayer mp ;
    Context context ;
    Activity activity ;
    String statusText = "VIDEO INITIALIZING";
    String SDCard = new String(Environment.getExternalStorageDirectory().getAbsolutePath());
    //String SDCard = "/storage/3E07-120C/";
    
    void setup() {
      size(400,400);
      activity = this.getActivity();
      context = activity.getApplicationContext();
      mp = new MediaPlayer();
      println( SDCard);
      try {
        mp.setDataSource(SDCard + "machine.mp4");
        mp.prepare();
        mp.start();
        statusText = "VIDEO PLAYING";
      }
      catch (IOException e) {
        println("error loading transit.mp4");
      }
      fill(255); 
      textAlign(CENTER, CENTER);
    }
    

    This code can't compile:

    import android.media.MediaMetadataRetriever;
    import android.os.Handler;
    import android.os.HandlerThread;
    import android.os.Looper;
    import android.app.Activity;
    import android.view.ViewGroup;
    import android.view.View;
    import android.view.SurfaceHolder;
    import android.view.SurfaceView;
    import android.media.MediaMetadataRetriever;
    import android.media.MediaPlayer;
    import android.content.res.Resources;
    import android.content.res.AssetFileDescriptor;
    import android.content.res.AssetManager;
    import android.content.Context;
    
    AssetFileDescriptor afd;
    Context context;
    Activity act;
    SurfaceView mySurface;
    SurfaceHolder mSurfaceHolder;
    MediaMetadataRetriever metaRetriever;
    MediaPlayer mMediaPlayer;
    
    void setup() {
      size(400, 400, P2D);
      act = this.getActivity();
      context = act.getApplicationContext();
      Looper.prepare();
      mMediaPlayer = new MediaPlayer();
      try {
        afd = context.getAssets().openFd("machine_compr.mp4");
        MediaMetadataRetriever metaRetriever = new MediaMetadataRetriever();
        metaRetriever.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
        String height = metaRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT); 
        if (int(height) < 2) {
          throw new IOException();
        }
      }
      catch (IllegalArgumentException e) {
        e.printStackTrace();
      }
      catch (IllegalStateException e) {
        e.printStackTrace();
      } 
      catch (IOException e) {
        e.printStackTrace();
      }
    
      mySurface = new SurfaceView(act);
      mSurfaceHolder = mySurface.getHolder();
      mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
      mSurfaceHolder.addCallback(new SurfaceHolder.Callback() {
        @Override
          public void surfaceCreated(SurfaceHolder surfaceHolder) {
          mMediaPlayer.setDisplay(surfaceHolder);
        }
    
        @Override
          public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i2, int i3) {
          mMediaPlayer.setDisplay(surfaceHolder);
        }
    
        @Override
          public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
        }
      }
      );
      startVideo();
    }
    
    void startVideo() {
      act.runOnUiThread(new Runnable() {
        public void run() {
          try {
            mMediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
            mSurfaceHolder = mySurface.getHolder();
            mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
            mMediaPlayer.prepare();
    //        act.addContentView(mySurface, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT));
            act.addContentView(mySurface, new ViewGroup.LayoutParams(400,400));
            if (mMediaPlayer.isPlaying() == false) {
              mMediaPlayer.start();
            }
          }
          catch (IllegalArgumentException e) {
            e.printStackTrace();
          }
          catch (IllegalStateException e) {
            e.printStackTrace();
          } 
          catch (IOException e) {
            e.printStackTrace();
          }
        }
      }
      );
    };
    
    void draw() {
    }
    
    void onPause() {
      if (mMediaPlayer!=null) {
        mMediaPlayer.release();
        mMediaPlayer = null;
      }
      super.onPause() ;
    }
    
    void onStop() {
      if (mMediaPlayer!=null) {
        mMediaPlayer.release();
        mMediaPlayer = null;
      }
      super.onStop() ;
    }
    
    void onDestroy() {
      if (mMediaPlayer!=null) {
        mMediaPlayer.release();
        mMediaPlayer = null;
      }
      super.onDestroy() ;
    }
    
    void onResume() {
      super.onResume() ;
    }
    

    and the console says it's because file couldn't be loaded

    OpenGL error 1280 at bot beginDraw(): invalid enum
    java.io.FileNotFoundException: machine_compr.mp4
        at android.content.res.AssetManager.openAssetFd(Native Method)
        at android.content.res.AssetManager.openFd(AssetManager.java:334)
        at processing.test.android_nativeplayer.android_nativeplayer.setup(android_nativeplayer.java:66)
        at processing.core.PApplet.handleDraw(PApplet.java:1801)
        at processing.opengl.PSurfaceGLES$RendererGLES.onDrawFrame(PSurfaceGLES.java:264)
        at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1590)
        at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1286)
    FATAL EXCEPTION: main
    Process: processing.test.android_nativeplayer, PID: 12773
    java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.FileDescriptor android.content.res.AssetFileDescriptor.getFileDescriptor()' on a null object reference
        at processing.test.android_nativeplayer.android_nativeplayer$2.run(android_nativeplayer.java:110)
        at android.os.Handler.handleCallback(Handler.java:815)
        at android.os.Handler.dispatchMessage(Handler.java:104)
        at android.os.Looper.loop(Looper.java:207)
        at android.app.ActivityThread.main(ActivityThread.java:5765)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
    OpenGL error 1285 at bot endDraw(): out of memory
    

    I'll maybe try a post on the new forum, but there's so much ressource here, it felt more accurate to post here rather than create a new post which would point towards here...

  • Not able to run the code in android mode

    I don't think minim will work in Android. If you want to play a sound file, you can do it using the cassette library. Also check previous posts: https://forum.processing.org/two/search?Search=mediaplayer

    Also please check he sticky post in the forum to learn how to format your code in the forum: https://forum.processing.org/two/discussion/15473/readme-how-to-format-code-and-text#latest

    Kf

  • Video in Android Mode

    Thanks again @akenaton! It works now. A couple of things:

    • I was building a simple sketch inside processing editor, and not in Android Studio. I hadn't implemented a mainActivity. Should I for my final application?
    • the setType method for surfaceHolder is deprecated. Is there a better way of defining holder type?
    • I don't have the layoutParameters worked out yet so that they comply with the processing default layout. The Processing application runs centered based onto size() method in setup, but the holder surface view is still the fullscreen. Is there a means of getting the layout parameters so that they stay contained within the size() of the sketch?

    Here's my simple video player sketch, based on @akenaton 's excellent description. Thanks again! (Can't get the forum's preprocessor not to convert "@Override" into an HTML ref to a forum user, so I changed it to "@ Override")(Thanks! @GoToLoop)

    import android.media.MediaMetadataRetriever;
    import android.os.Handler;
    import android.os.HandlerThread;
    import android.os.Looper;
    import android.app.Activity;
    import android.view.ViewGroup;
    import android.view.View;
    import android.view.SurfaceHolder;
    import android.view.SurfaceView;
    import android.media.MediaMetadataRetriever;
    import android.media.MediaPlayer;
    import android.content.res.Resources;
    import android.content.res.AssetFileDescriptor;
    import android.content.res.AssetManager;
    import android.content.Context;
    
    AssetFileDescriptor afd;
    Context context;
    Activity act;
    SurfaceView mySurface;
    SurfaceHolder mSurfaceHolder;
    MediaMetadataRetriever metaRetriever;
    MediaPlayer mMediaPlayer;
    
    void setup() {
      size(400, 400, P2D);
      act = this.getActivity();
      context = act.getApplicationContext();
      Looper.prepare();
      mMediaPlayer = new MediaPlayer();
      try {
        afd = context.getAssets().openFd("Title.m4v");
        MediaMetadataRetriever metaRetriever = new MediaMetadataRetriever();
        metaRetriever.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
        String height = metaRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT); 
        if (int(height) < 2) {
          throw new IOException();
        }
      }
      catch (IllegalArgumentException e) {
        e.printStackTrace();
      }
      catch (IllegalStateException e) {
        e.printStackTrace();
      } 
      catch (IOException e) {
        e.printStackTrace();
      }
    
      mySurface = new SurfaceView(act);
      mSurfaceHolder = mySurface.getHolder();
      mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
      mSurfaceHolder.addCallback(new SurfaceHolder.Callback() {
        @Override
          public void surfaceCreated(SurfaceHolder surfaceHolder) {
          mMediaPlayer.setDisplay(surfaceHolder);
        }
    
        @Override
          public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i2, int i3) {
          mMediaPlayer.setDisplay(surfaceHolder);
        }
    
        @Override
          public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
        }
      }
      );
      startVideo();
    }
    
    void startVideo() {
      act.runOnUiThread(new Runnable() {
        public void run() {
          try {
            mMediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
            mSurfaceHolder = mySurface.getHolder();
            mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
            mMediaPlayer.prepare();
    //        act.addContentView(mySurface, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT));
            act.addContentView(mySurface, new ViewGroup.LayoutParams(400,400));
            if (mMediaPlayer.isPlaying() == false) {
              mMediaPlayer.start();
            }
          }
          catch (IllegalArgumentException e) {
            e.printStackTrace();
          }
          catch (IllegalStateException e) {
            e.printStackTrace();
          } 
          catch (IOException e) {
            e.printStackTrace();
          }
        }
      }
      );
    };
    
    void draw() {
    }
    
    void onPause() {
      if (mMediaPlayer!=null) {
        mMediaPlayer.release();
        mMediaPlayer = null;
      }
      super.onPause() ;
    }
    
    void onStop() {
      if (mMediaPlayer!=null) {
        mMediaPlayer.release();
        mMediaPlayer = null;
      }
      super.onStop() ;
    }
    
    void onDestroy() {
      if (mMediaPlayer!=null) {
        mMediaPlayer.release();
        mMediaPlayer = null;
      }
      super.onDestroy() ;
    }
    
    void onResume() {
      super.onResume() ;
    }
    
  • Video in Android Mode

    @steve_j, @Conrad

    you can show video with android mediaplayer or android videoView (which is a kind of wrapper for mediaPlayer) following these steps (i choose mediaPlayer)::

    imports:(some of them are useless)

        import android.media.MediaMetadataRetriever;
        import android.media.MediaPlayer;
        import android.os.Handler;
        import android.os.HandlerThread;
        import android.os.Looper;
    
        import android.app.Activity;
        import android.view.ViewGroup;
        import android.view.View;
        import android.view.SurfaceHolder;
        import android.view.SurfaceView;
    

    in setup():

    • Looper.prepare();

    • You find your video with fileDescriptor (like for the sound); your video is in your data folder; choose a standard format: mp4, 3Gp..., not mkv! - This is the first step and here you have to be sure that your video is found using MediaMetadataRetriever metaRetriever and getting height or width: if it returns 0 or -1, there is a problem!

    • you create a SurfaceView (mySurface) then you get the Holder from this surface; then you add callbacks to it (SurfaceCreated, surfaceChanged, surfaceDestroyed). In surfaceCreated you set your player.display to the surface: myPlayer.setDisplay(mySurface); the same for surfaceChanged (in case of config changes)

    • you create some method to add your video to surface view; it has to be a runnable (this.getactivity().runOnUiThread(new runnable)...In its run() you call another method which prepare the player in a standard way using the afd you have created at the beginning. At this moment your player is ok; you can add your surfaceview to the mainActivity; finally you start the player (if player().isPlaying == false){myPlayer.start();}

    • nothing to do in draw

    • in void onPause() or onDestroy() dont forget to release the player

    try to do that and you can seee your video... tested with processing 3XXX and android mode 3; not tested with am 4 SONYXPERIA Z running kitKat nexus 7 TABLET running mmallow (6.0)

  • Help Me!I use Processing Minim to play Map3 files in Android

    @zishu===

    minim does not work with android as it is based on javaFxSound; use the android mediaplayer API to play sound.

  • Google TTS, Help me
    import android.media.MediaPlayer;
    import  android.content.res.Resources;
    import android.content.res.AssetFileDescriptor;
    import android.content.res.AssetManager;
    import android.content.Context;
    import android.app.Activity;
    
    MediaPlayer snd = new MediaPlayer();
    //AssetManager assets = this.getAssets();
    AssetFileDescriptor fd;
    Context context;
    Button bPlay;
    Activity act;
    
    void setup()
    {
      colorMode(HSB);
      size(800, 800);
      act = this.getActivity();
      context = act.getApplicationContext();
      try {
    
        fd = context.getAssets().openFd("https:"
            + "//translate.google.com/translate_tts"
            + "?ie=UTF-8&tl=ko-KR&client=tw-ob&q=Hello");
        snd.setDataSource(fd.getFileDescriptor(),fd.getStartOffset(), fd.getLength());
      }
      catch (IllegalArgumentException e) {
        e.printStackTrace();
      }
      catch (IllegalStateException e) {
        e.printStackTrace();
      } 
      catch (IOException e) {
        e.printStackTrace();
      }
     }
    

    Dear akenaton,

    1. Is the url wrong?

    2. Or is it(source code) wrong?

  • Google TTS, Help me

    @Gwak===

    minim does not work with android so you get a NPE: use mediaPlayer + setDataSource(yoururl)

  • Unable to load sound in Android

    The problem is not with the sound file. I tried it with many files from different sources.

    This is the code that worked for me with SoundPool (Do you think it's reliable?):

    import android.media.AudioManager;
    import android.media.SoundPool;
    import android.content.res.AssetFileDescriptor;
    import android.content.res.AssetManager;
    import android.content.Context;
    import android.app.Activity;
    SoundPool SP;
    Context context;
    AssetFileDescriptor descriptor;
    Activity act;
    AssetManager am;
    int sound1, sound2;
    
    void setup() {
      fullScreen();
      SP = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
      try {
        am = getAssets();
        sound1 = SP.load(this.getActivity().getApplicationContext().getAssets().openFd("MYSOUND2.mp3"), 1);
        sound2 = SP.load(this.getActivity().getApplicationContext().getAssets().openFd("MYSOUND1.mp3"), 1);
      } 
      catch (IOException e) {
      }
    }
    AssetManager getAssets() {
      return null;
    }
    void draw() {
    }
    void mousePressed() {
      if (mouseY<height/2) {
        SP.play(sound1, 0.9f, 0.9f, 2, 0, 1.0);
      } else if (mouseY>=height/2) {
        SP.play(sound2, 0.9f, 0.9f, 2, 0, 1.0);
      }
    }
    

    I also tried the MediaPlayer code in a similarly simplified version:

    import android.media.MediaPlayer;
    import android.content.res.AssetFileDescriptor;
    import android.content.Context;
    import android.app.Activity;
    
    /////////////////////////////////////////////////////////
    
    MediaPlayer mp;
    Context context; 
    Activity act;
    AssetFileDescriptor afd;
    
    void setup() {
      context = this.getActivity().getApplicationContext();
      try {
        mp = new MediaPlayer();
        mp.setDataSource(context.getAssets().openFd("MYSOUND.mp3").getFileDescriptor());
        mp.prepare();
      } 
      catch(IOException e) {
        println("file did not load");
      }
      mp.start();
    };
    
    void draw() {
    };
    

    But file still does not load (naturally, because that's practically the same code..) and then I tried this:

    MediaPlayer mp;
    Context context; 
    Activity act;
    AssetFileDescriptor afd;
    AssetManager am;
    
    void setup() {
      context = this.getActivity().getApplicationContext();
      try {
        mp = new MediaPlayer();
        am = getAssets();
        mp.setDataSource(context.getAssets().openFd("ALL THE RAGE BACK HOME.mp3").getFileDescriptor());
        mp.prepare();
      } 
      catch(IOException e) {
        println("file did not load");
      }
      mp.start();
    }
    AssetManager getAssets() {
      return null;
    }
    
    void draw() {
    };
    

    And it - also - did not work.

  • Unable to load sound in Android

    @akenaton

    Then I am completely at a loss... I tried so many things with the MediaPlayer.

    Hmmm, I'm not sure if I'm getting SoundPool right. Whenever possible, can you give a short code snippet only showing how to use SoundPool? I don't think there's one anywhere in the forum.

    Edit: I HAVE SUCCESSFULLY PLAYED SOUND WITH SOUNDPOOL. However, I do not fully understand my code (I just tried a lot of different things and one just so happened to work.) I will post it when I can.

    However, I still can’t play music. MediaPlayer still refuses to work.

  • Unable to load sound in Android

    If this is of any help, I tried it this way:

    import android.media.MediaPlayer;
    import android.content.res.AssetFileDescriptor;
    import android.content.Context;
    import android.app.Activity;
    
    /////////////////////////////////////////////////////////
    
    MediaPlayer mp = new MediaPlayer();
    Context context; 
    AssetFileDescriptor afd;
    
    void setup() {
      try {
      afd = context.getAssets().openFd("MYSOUND.mp3");
      mp.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength());
      mp.prepare();
      mp.start();
    
      } 
      catch(IllegalArgumentException e) {
        e.printStackTrace();
      }
      catch(IllegalStateException e) {
        e.printStackTrace();
      }
      catch(IOException e) {
        e.printStackTrace();
       }
    };
    
    void draw() {
    };
    

    And got this in console:

    BUILD SUCCESSFUL in 7s 35 actionable tasks: 35 executed FATAL EXCEPTION: Animation Thread Process: processing.test.sketch_171009c, PID: 15932 java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.AssetManager android.content.Context.getAssets()' on a null object reference at processing.test.sketch_171009c.sketch_171009c.setup(sketch_171009c.java:37) at processing.core.PApplet.handleDraw(Unknown Source) at processing.core.PSurfaceNone.callDraw(Unknown Source) at processing.core.PSurfaceNone$AnimationThread.run(Unknown Source)

  • Unable to load sound in Android

    The cassette should work. I remember there was some issues when the app was being sent to the background (back button) then one was not able to free the resources properly. At that time, the OP solved his issues by moving to MediaPlayer as advised by akenaton. If cassette work for your project, you should go ahead an use it. If you get into any trouble, you could continue pursuing any of the alternatives above.

    Please keep in mind that when I tested the cassette lib b4, I was using an older Android Mode, not the latest 4.0

    Kf

  • Unable to load sound in Android

    @randomdude===

    the error message is mainly about ndk that this code does not use, it is probably a bug, yet without consequence in your case (if you really want to get rid of it you can install the ndk platform yourself but why???) - the real error is a JIO exception which seems to mean that the file is found but cannot be loaded by mediaplayer; to be sure or trying to be, you can add a setOnPreparedListener() to your player and start the sound when the callback is received; perhaps also you can try to create a runnable or use prepareAsync() instead of prepare. It is difficult to answer more without being sure that your sound is a good one (and knowing is weight && length): in some cases .mp3 do not work. Try with another one...

    As for sound effects (sounds <5") eg. for a game Android has the soundPool class. As for more complex things about sound you can also use AudioTrackManager.With these 3 solutions already present Cassette lib is useless.

  • Unable to load sound in Android

    Cassette should work and it has limited features, but it is good for building small programs. However, when it comes to managing the app's life cycle, it seems the library needs some changes. MediaPlayer should work in your case although i am not able to assist any further atm.

    Kf

  • Unable to load sound in Android

    Hey all!

    I have gone through every topic on the subject and tried every example, but I always get the exception message and sound fails to load. I have tried the go-to example:

    import android.media.MediaPlayer;
    import android.content.res.AssetFileDescriptor;
    import android.content.Context;
    import android.app.Activity;
    
    /////////////////////////////////////////////////////////
    
    MediaPlayer mp;
    Context context; 
    Activity act;
    AssetFileDescriptor afd;
    
    void setup() {
      act = this.getActivity();
      context = act.getApplicationContext();
      try {
        mp = new MediaPlayer();
        afd = context.getAssets().openFd("MYSOUND.mp3");//which is in the data folder
        mp.setDataSource(afd.getFileDescriptor());
        mp.prepare();
      } 
      catch(IOException e) {
        println("file did not load");
      }
      mp.start();
    };
    
    void draw() {
    };
    

    As well as the more complex examples:

    import java.io.File;
                    import java.io.IOException;
                    import android.media.MediaPlayer;
                    import android.media.MediaPlayer.OnPreparedListener;
                    import android.content.res.AssetFileDescriptor;
                    import  android.content.res.Resources;
                   import android.content.res.AssetManager;
                     import android.content.Context;
            import android.app.Activity;
            import android.os.Bundle;
            import android.app.Application;
    
            /////////////////////////////////////////////////////////
    
            MediaPlayer mp;
            public boolean loop = true;
            AssetManager am;
         Context context; 
         boolean premiere = false;
    
         Activity act;
         AssetFileDescriptor afd;
         boolean AppliAndroid = false;
    
    
         public   void setup(){
              orientation(PORTRAIT);
              background(255,0,0);
              fill(255);
              textSize(24);
              text("SONG IS PLAYING", 200,300);
              act = this.getActivity();
           context = act.getApplicationContext();
          // testClass();
    
    
    
    
           try {
              mp = new MediaPlayer();
    
            afd = context.getAssets().openFd("MYSOUND.mp3");//which is in the data folder
          mp.setDataSource(afd.getFileDescriptor());
          mp.prepare();
          } catch(IOException e) {
            println("j'arrive pas à préparer");
            }
             mp.setLooping(loop);
              mp.start();
    
    
           };
    
        public    void draw(){
    
            };
    

    Sound just refuses to load. I get the exception message every time.

  • Midi sequencer android porting issue

    @chanof===

    i suppose that your lib (themidibus) uses javaFXsound which is not supported by android. As for android, mediaplayer can read .mid files. For something like your code is doing you need another lib: i have used libpd for that with some results. This lib supposes that you install pd (vanilla).