简体   繁体   English

如何在 CodeNameOne 中播放 mp3 目录文件?

[英]How to play mp3 directory files in CodeNameOne?

I'm recently starting with codenameone and I'm working on a music player app.我最近从 codenameone 开始,我正在开发一个音乐播放器应用程序。 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/" .音频位于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.将音频捆绑到 jar 将增加您的 jar 大小和最终应用程序大小,这将超过免费配额,因此我们通常不建议这样做。

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") .如果您仍然选择 go 与该路由,您可以将文件存储在 src 文件夹的根目录中并使用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. 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.您无法动态列出文件,因为 HTTP 或 jar 的内容没有列出选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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