简体   繁体   中英

How to play mp3 directory files in CodeNameOne?

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.

在此处输入图像描述

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");
        }
        });

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM