weixin_42993516
2021-12-22 16:41
采纳率: 100%
浏览 26

在java项目里能播放mp3音乐,并且打war包上传到服务器

想在java项目里添加音乐播放mp3

用的player方法做的,通过file方法读取路径,本地没问题读取项目中的WebContent/music/cc.mp3就不行
@RequestMapping("cc2")
public String toaccb()throws FileNotFoundException, JavaLayerException{

    File file = new File("/music/cc.mp3");
    FileInputStream fis = new FileInputStream(file);
    BufferedInputStream stream = new BufferedInputStream(fis);
    Player player = new Player(stream);
    
    player.play();
    return "cc2";
    
}

public void  play() {
    new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                File file = new File("/music/cc.mp3");
                FileInputStream fis = new FileInputStream(file);
                BufferedInputStream stream = new BufferedInputStream(fis);
                player = new Player(stream);
                player.play();
            } catch (Exception e) {

                // TODO: handle exception
            }
        }
    }).start(); 
    try {
        
        Thread.sleep(200000);
        
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    player.close();

} 

运行结果报错找不到路径

我查询了很多file都是读取的本机路径 ,也试了在服务器读取/usr/local路径也不行

我主要是想要把项目放在服务器上面,能播放音乐

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

相关推荐 更多相似问题