我该怎么玩MP3文件? [英] How can I play an MP3 file?

查看:120
本文介绍了我该怎么玩MP3文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试访问保存在源文件夹中的 MP3 音频文件,以便我可以在Java程序(应用程序,而不是applet)中播放。

I am trying to access an MP3 audio file saved in my source folder, so that I can play it in a Java program (application, not applet).

我似乎遇到的问题是我无法导入 javax.media 。我一直在尝试安装Java媒体框架,但它给了我一个错误报告:

The problem I seem to be having is that I cannot import javax.media. I have been trying to install the Java media framework, but it gives me an error report:


无法执行......。

"unable to execute...".

我使用JDK v7 u45并在JDK v7 u40上试过它,但我似乎无法弄清楚要做什么。
我试过调用

I use JDK v7 u45 and had tried it on JDK v7 u40, but I can't seem to figure out what to do. I tried calling

import javax.media.*;

并且它在媒体上返回语法错误,声称导入不可用。 jmf在Java 7中不起作用吗?

and it returns a syntax error on the "media", claiming import is not available. Does jmf not work in Java 7?

推荐答案

请尝试以下方法。它正在运行并导入以下类。

Try the following way. It's working and also import the following classes.

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;

try{
    AudioInputStream audioInputStream =
        AudioSystem.getAudioInputStream(
            this.getClass().getResource("<Path of relative sound file in src folder>"));
    Clip clip = AudioSystem.getClip();
    clip.open(audioInputStream);
    clip.start();
}
catch(Exception ex)
{
}

这篇关于我该怎么玩MP3文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆