英文:
Codename One: Issue with .createMedia method and audio files
问题
我正在尝试使用Codename One制作游戏的音频播放器,但在使用.createMedia方法时遇到了问题。
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();
}
}
}
playAudio和AudioPlayer被实例化的类:
public AudioPlayer audioPlayer = new AudioPlayer(); //音频播放器
public VolumeButton volumeButton = new VolumeButton();
public Game() {
changeState(sceneNum);
audioPlayer.playAudio("/fluffingADuck.wav");
}
我几乎可以确定这不是文件路径的问题,因为当我打印'is'时会得到一个地址,我还使用is.available()来检查'is'中是否有字节。我还尝试使用.mp3文件而不是.wav文件,但没有成功。
这是我得到的堆栈跟踪;尽管出现错误,但程序仍然能够运行:
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)
任何帮助对我来说都很重要,我一直在努力制作这个音频播放器。
英文:
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.
答案1
得分: 1
异常的打印代码似乎不正确,如原答案中所述,但问题并非如此。
Game
类可能是您的主类(其中 start()
方法所在的类,或者是 Lifecycle
的子类)。您将代码编写在构造函数中,或者在构造函数链中的其他位置。
在主类的 init(Object)
方法被调用之前,如果在构造函数或静态初始化程序中初始化了某些内容,那么 Codename One 本身尚未初始化,会导致问题失败。
要修复堆栈跟踪,请更改此代码:
catch (IOException ioe) { ioe.printStackTrace(); }
为:
catch (IOException ioe) {
if(ios.getCause() != null) {
ios.getCause().printStackTrace();
}
ioe.printStackTrace();
}
这应该可以回答问题或提供适用的附加详细信息。如果是后者,请编辑问题并我会改进这个答案。
英文:
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.
答案2
得分: 1
要播放mp3,请尝试以下代码:
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("播放错误:" + e);
}
}
如前所述,mp3文件应该位于\common\src\main\resources
目录中。
请注意,mimeType应为audio/mpeg
而不是audio/mp3
。
英文:
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 \common\src\main\resources
directory.
Note mimeType should be audio/mpeg
not audio/mp3
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论