English 中文(简体)
Codename One: Issue with .createMedia method and audio files
原标题:

I m trying to make an audio player for a game using Codename One but I ve been running into issues with the .createMedia method.

public class AudioPlayer {
private Media MEDIA = null;
public Media media;

public void playAudio(String fileName) {
    try {
        if (MEDIA == null) {
            InputStream is = CN.getResourceAsStream(fileName);
            System.out.println(is);
            MEDIA = MediaManager.createMedia(is, "audio/wav", new Runnable() {
                @Override
                public void run() {
                    MEDIA = null;
                }
            });
        if (MEDIA != null && MEDIA.isPlaying() == false) {
            MEDIA.setVolume(100);
            MEDIA.play();
        } }
    } catch (IOException ioe) { ioe.printStackTrace(); }
} }

Class where playAudio and AudioPlayer are instantiated:

public AudioPlayer audioPlayer = new AudioPlayer(); //audio player 
public VolumeButton volumeButton = new VolumeButton();

public Game() {
    changeState(sceneNum);
    audioPlayer.playAudio("/fluffingADuck.wav");

I m almost certain it is not an issue with the file pathing as I get an address when I print out is and I ve used is.available() to check if there are bytes in is . I have also tried using a .mp3 instead of .wav file to no avail. This is the stack trace that I get; the program is still able to run despite the error

java.io.IOException
    at com.codename1.impl.javase.JavaSEPort.createMedia(JavaSEPort.java:9535)
    at com.codename1.ui.Display.createMedia(Display.java:3705)
    at com.codename1.media.MediaManager.createMedia(MediaManager.java:306)
    at com.example.audio.AudioPlayer.playAudio(AudioPlayer.java:31)
    at com.example.myapp.Game.<init>(Game.java:30)
    at com.example.myapp.Game.getGame(Game.java:39)
    at com.example.myapp.MyApp.runApp(MyApp.java:14)
    at com.codename1.system.Lifecycle.start(Lifecycle.java:129)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:568)
    at com.codename1.impl.javase.Executor$3$2.run(Executor.java:340)
    at com.codename1.ui.Display.executeSerialCall(Display.java:1395)
    at com.codename1.ui.Display.processSerialCalls(Display.java:1379)
    at com.codename1.ui.Display.mainEDTLoop(Display.java:1166)
    at com.codename1.ui.RunnableWrapper.run(RunnableWrapper.java:120)
    at com.codename1.impl.CodenameOneThread.run(CodenameOneThread.java:176)

Any help would mean a lot I ve been stuck on making this audio player.

问题回答

To play mp3 try this

audioPlayer.playAudio("/test.mp3");

public void playAudio(String mp3FileName) {
    try {
        Media media = MediaManager.createMedia((Display.getInstance()
                .getResourceAsStream(getClass(), mp3FileName)), "audio/mpeg");

        media.play();
    } catch (IOException e) {
        System.out.println("Play Error: " + e);
    }
}

As instructed before mp3 file should be in commonsrcmain esources directory.

Note mimeType should be audio/mpeg not audio/mp3

The print code for the exception seems incorrect as mentioned below in the original answer but the problem isn t that.

The class Game is probably your main class (the one where the start() method is in or subclass of Lifecycle). You wrote the code in the constructor or alternatively within that chain.

Until the init(Object) method of the main class is invoked. If you initialize stuff in the constructor or static initializer then Codename One itself isn t initialized and things will fail.

To fix the stack trace fix this code:

catch (IOException ioe) { ioe.printStackTrace(); }

To this:

catch (IOException ioe) { 
   if(ios.getCause() != null) {
       ios.getCause().printStackTrace();
   }
   ioe.printStackTrace(); 
}

This should either answer the question or provide applicable additional details. Assuming the latter, edit the question with the additional details and I ll improve this answer.





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签