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.