我想要做2任务在同一时间:J2ME上的音频,我不知道哪里错了?
播放音频文件。
阅读它的原始数据做一些事情
这里是我的代码:
String wavPath = "file:///" + currentPath + fileName;
FileConnection fc;
try {
fc = (FileConnection) Connector.open(wavPath);
if (!fc.exists()) {
throw new IOException("File does not exists.");
}
InputStream is = fc.openInputStream();
// to do something with raw data as print samples of it
Player player = Manager.createPlayer(wavPath);
player.realize();
player.prefetch();
player.start();
} catch (IOException e1) {
e1.printStackTrace();
}
但没有运行,音频文件不无法运行。如果我删除线:
InputStream is = fc.openInputStream();
音频文件运行得很好。但我想同时做2件任务,我不知道该怎么做。任何人都可以帮助我?
我曾尝试使用2线程,但它仍然无法正常工作,音频文件运行(线程1),但是线程2不运行:
new Thread(new Runnable() {
public void run() {
try {
Manager.createPlayer("file:///E:/" + fileName).start();
} catch (MediaException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
new Thread(new Runnable() {
public void run() {
FileConnection fc;
try {
fc = (FileConnection) Connector.open("file:///E:/" + fileName);
InputStream is = fc.openInputStream();
byte[] b = new byte[10];
int length = is.read(b, 0, 10);
for (int i = 0; i < length; i++) {
form.append(b[i] + "");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
我要玩波形文件(.WAV) – itakanzebo
,我可以看到,当你删除了这一行是“主”线程被阻塞在该呼叫尝试打开输入流,将工作的唯一原因。一段时间后你会得到例外吗? – Joeblackdev
我试图模拟你使用两个文件副本描述的行为。播放第一副本的音频并阅读第二副本。只要用户没有注意到你以这种方式欺骗,谁在乎? – gnat