1

I'm recently starting with codenameone and I'm working on a music player app. I want to get the musics from a folder. I found out in the documentation a code about "Audio Capture & Recording".

But what I'm looking for is to just play audios from a specific folder.How can I adjust this code? I'm also not finding where these audios are being saved to.

Here's the code+ a footage of the Recording code.

enter image description here

Button button = new Button("musiques!");
         Form hi = new Form("musiques", new BorderLayout(BorderLayout.CENTER_BEHAVIOR_CENTER_ABSOLUTE));
        hi.addComponent(BorderLayout.CENTER, LayeredLayout.encloseIn(BoxLayout.encloseY(button)));
        
        button.addActionListener((e) -> {
                 InputStream streamFromResource = CN.getResourceAsStream("/filename.mp3"); 
        try { 
            MediaManager.createMedia(streamFromResource, "audio/mp3");
        } catch (IOException ex) {
            System.out.print("non");
        }
        });
Compte Gmail
  • 137
  • 3
  • 8

1 Answers1

1

The audio is in fs.getAppHomePath() + "recordings/". Bundling the audios into the jar will increase your jar size and final app size which will exceed the free quota so we don't normally recommend that.

If you still choose to go with that route you can store the files in the root of the src folder and access them using CN.getResourceAsStream("/filename.mp3").

Then you can just use:

Media m = MediaManager.createMedia(streamFromResource, "audio/mp3");

Alternatively you can store the files on the web and use an HTTPS URL instead of a file URL to play them.

You can't dynamically list the files since there's no listing option for HTTP or for the contents of the jar.

Shai Almog
  • 51,749
  • 5
  • 35
  • 65
  • Hey shai! I'm still having a problem , here's what I wrote in the start function. InputStream streamFromResource = CN.getResourceAsStream("/filename.mp3"); MediaManager.createMedia(streamFromResource, "audio/mp3", false); I have an error in the 2nd line (I don't know what should I put in the 1st parameter) – Compte Gmail May 13 '21 at 20:25
  • I remembered incorrectly it doesn't include the boolean argument so `MediaManager.createMedia(streamFromResource, "audio/mp3");` would work. – Shai Almog May 14 '21 at 03:30
  • Still not working! The mp3 file is in the default package btw. here's the function start: https://pastebin.com/LRTKGzDL – Compte Gmail May 14 '21 at 18:05
  • What's the error? Is the input stream null or is there something printed in the console? – Shai Almog May 15 '21 at 05:32
  • Here's the output Shai. https://i.ibb.co/p1wYqnQ/noa.png the "filename.mp3" is on the default package as you can see what I get is a black screen. I don't know if you have an old project that illustrates the example maybe I'm missing something? – Compte Gmail May 15 '21 at 11:42
  • Usually that's what happens when form.show() wasn't reached. Did you step over the code with the debugger? Can you update the question with the current code that you have? Maybe one of the changes broke the behavior. – Shai Almog May 16 '21 at 01:46