繁体   English   中英

如何使一个MP3文件等待播放直到另一个MP3

[英]How to make an MP3 file wait to play until after another MP3

我正在开发一个程序,将播放包含MP3的播放列表。

我正在使用以下MP3类

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.sound.sampled.AudioInputStream;
import javazoom.jl.player.Player;
public class MP3 {
private Player player;
public BufferedInputStream bis;
// constructor 
public MP3() {
}
public void close() throws IOException {
    if (player != null)
        player.close();
}
// play the MP3 file to the sound card
public void play(String filename) {
    try {
        InputStream stream = MP3.class.getClassLoader()
                .getResourceAsStream(filename);

        bis = new BufferedInputStream(stream);
        player = new Player(bis);

    } catch (Exception e) {
        System.out.println("Problem playing file " + filename);
        System.out.println(e);
    }

    // run in new thread to play in background
    new Thread() {
        public void run() {
            try {
                player.play();
            } catch (Exception e) {
                System.out.println(e);
            }
        }
    }.start();

}
}

我已经能够基于此代码播放MP3文件,但无法播放多个文件。 当我第一次尝试播放MP3的ArrayList时,所有歌曲都是彼此直接播放的。 然后,我尝试使用.wait方法使第二首歌曲等待,直到完成播放第一首歌曲。 但是,这样做有效,这也使程序中的所有内容都等到歌曲播放完毕后再执行。 我希望我的程序在播放歌曲时能够执行多项操作,以便该方法不起作用。

这是我目前使用.wait的代码

} else if (e.getSource() == btn2) {
            ArrayList<Integer> songsToPlay = new ArrayList<Integer>();
            songsToPlay.add(1);
            songsToPlay.add(2);
            if (b2 == false) {
                b2 = true;
                mp3 = new MP3();
            //loop through the ArrayList of songs 
            for (int i = 0; i < songsToPlay.size(); i++) {
                mp3.play("music/" + songsToPlay.get(i) + ".mp3");
                synchronized (mp3) {
                    try {
                        mp3.wait(1000);
                    } catch (InterruptedException e2) {
                    // TODO Auto-generated catch block
                   // e2.printStackTrace();
            }
            }
            }
            } else {
                  b2 = false;
                  try {
                      mp3.close();
                  } catch (IOException e1) {
                  // TODO Auto-generated catch block
                 // e1.printStackTrace();
              }
          }

        }

任何建议都会很棒,因为我已经坚持了一段时间。 同样,如果我想找到一种使用.wait的方法,我还需要一种方法来计算当前MP3的长度。 现在,我只是对值进行硬编码。

您为什么要尝试手动完成所有操作? 为什么不使用Java VLC库作为播放器呢?

在该主题上,您似乎缺少一些有价值的信息来构建便捷的播放器。 当前曲目的长度是多少? 用户将如何搜寻一首歌曲? 您是否阅读了mp3文件的ID3标签以显示标题,艺术家,专辑等信息?

我知道这不是解决问题的直接方法,但可以帮助您自己找到问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM