J2me播放器,逐个播放MP3文件
我无法依次播放 MP3 文件,当 1 个文件播放完毕后,另一个文件需要开始播放。我只能开始播放 1 个文件,当我输入代码来启动下一个文件时,它什么也不做。由于某些原因,我无法在文件持续时间内使用 thread.sleep()
。还有其他方法可以做到这一点吗?
//this is the basic code..
playq(String fname){
pl = Manager.createPlayer(stream,"audio/mpeg");
pl.realize();
pl.prefetch();
pl.start();
// what should i use here?? pls i don't want to use thread.sleep..
playagain(fname);
}
void playagain(String fname){
try {
pl.stop();
pl.deallocate();
pl.close();
} catch (Exception ex) {}
//change file name and stream and..
playq(mp3f);
}
I am unable to play an MP3 file one after the other, when 1 file finishes playing the other file needs to start. I am only able to start playing 1 file, when I put code to start the next file it just does nothing. For some reasons I cannot use thread.sleep()
for the duration of file. Is there any other way to do this?
//this is the basic code..
playq(String fname){
pl = Manager.createPlayer(stream,"audio/mpeg");
pl.realize();
pl.prefetch();
pl.start();
// what should i use here?? pls i don't want to use thread.sleep..
playagain(fname);
}
void playagain(String fname){
try {
pl.stop();
pl.deallocate();
pl.close();
} catch (Exception ex) {}
//change file name and stream and..
playq(mp3f);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的代码不应该尝试从
catch
块中播放代码 - 一方面,只有在以下情况下才会调用它:不生成了异常(您通常也不应该一揽子捕获异常 - 使用更具体的内容)。您确定您不能使用
Thread.sleep()
吗?当然,无论如何你都不会真正想要这样做(例如,如果用户可以暂停剪辑......)。相反,请考虑使用
PlayerListener
接口,并侦听END_OF_MEDIA
事件。一个非常基本的(例如,这没有经过测试,并且还需要更多工作)示例:
请注意,我没有完全正确地执行此操作 - 例如,我没有进行异常处理。另外,在不了解您的情况的情况下,我不知道还有什么值得担心的。这应该只是一个简单的答案,让您开始考虑应该如何处理这个问题。
(另外,我从未使用过 JME 的媒体播放器,所以我不知道有关 GC 的任何警告等)。
Your code should NOT attempt to play the code from a
catch
block - for one thing, it's only going to be called if an exception was generated (You also generally shouldn't be blanket-catchingException
- use something more specific).Are you sure you can't use
Thread.sleep()
? Granted you're not really going to want to anyways (for instance, if the user can pause the clip...).Instead, look into using the
PlayerListener
interface, and listen forEND_OF_MEDIA
events.A really basic (as in, this isn't tested, and needs more work besides) example:
Please note that I'm not doing this completely correctly - I'm not doing exception handling, for example. Also, without knowing more about your situation, I don't know what other things to worry about. This should just be a simple answer to get you started looking at where you should be going with this.
(Also, I've never worked with the media player of JME, so I don't know any caveats about the GC there, etc).