如何让用户选择一个音频文件,并在Java中播放 [英] How to have the user choose an audio file and play it in Java

查看:283
本文介绍了如何让用户选择一个音频文件,并在Java中播放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够使无论是在用户点击一个按钮,选择从他们的计算机的音频文件(兼容格式的)GUI或控制台应用程序,它扮演,而作为我完全没有经验的GUI中,如果我能得到一个提示,如何实现暂停和播放按钮,以及一个音量电位器/拨号和停止按钮这将是很好。我只知道我会不得不进口 java.io。* sun.audio。*

修改
我现在的code是这样的:

 进口sun.audio *。 //导入sun.audio包
进口java.awt中的*。
进口java.io. *;
公共类内置扬声器扩展框架实现了FilenameFilter {/ **
 *
 * /
私有静态最后的serialVersionUID长= 4914433234899026080L;
按钮openButton =新按钮(打开);
按钮为playButton =新按钮(播放);
按钮loopButton =新按钮(循环);
按钮STOPBUTTON =新按钮(停止);
标签名=新标签();
文件theFile = NULL;
@燮pressWarnings({限购})
AudioData海图= NULL;
InputStream的nowPlaying = NULL;@燮pressWarnings({德precation})
公共内置扬声器(){
    超级(内置扬声器);
    调整(300,200);
    面板北=新面板();
    north.setLayout(新的FlowLayout(FlowLayout.LEFT));
    north.add(新标签(文件));
    north.add(北,文件名);
    加(北,北);
    面板南方=新面板();
    south.add(openButton);
    south.add(为playButton);
    south.add(loopButton);
    south.add(STOPBUTTON);
    加(南方,南);
}@燮pressWarnings(德precation)
公共静态无效的主要(字串[] args){
    内置扬声器SP =新的内置扬声器();
    sp.show();
}@燮pressWarnings({德precation,限购})
公共无效的open(){
    的FileDialog的fd =新的FileDialog(这一点,请选择一个.AU文件:);
    fd.setFilenameFilter(本);
    fd.show();
    尝试{
        theFile =新的文件(fd.getDirectory()+/+ fd.getFile());
        如果(theFile!= NULL){
            filename.setText(theFile.getName());
            FIS的FileInputStream =新的FileInputStream(theFile);
            语音串流为=新语音串流(FIS);
            海图= as.getData();
        }
    }
    赶上(IOException异常五){
        通信System.err.println(E);
    }
}@燮pressWarnings(限购)
公共无效播放(){
    停止();
    如果(海图== NULL)开();
    如果(海图!= NULL){
        AudioDataStream广告=新AudioDataStream(海图);
        AudioPlayer.player.start(广告);
        nowPlaying =广告;
    }
}@燮pressWarnings(限购)
公共无效停止(){
    如果(nowPlaying!= NULL){
        AudioPlayer.player.stop(nowPlaying);
        nowPlaying = NULL;
    }
}@燮pressWarnings(限购)
公共无效循环(){
    停止();
    如果(海图== NULL)开();
    如果(海图!= NULL){
        ContinuousAudioDataStream CADS =新ContinuousAudioDataStream(海图);
        AudioPlayer.player.start(CADS);
        nowPlaying = CADS;
    }
}公共布尔行动(事件E,对象怎么办){    如果(e.target ==为playButton){
        玩();
        返回true;
    }
    否则,如果(e.target == openButton){
        打开();
        返回true;
    }
    否则,如果(e.target == loopButton){
        循环();
        返回true;
    }
    否则,如果(e.target == STOPBUTTON){
        停止();
        返回true;
    }    返回false;}公共布尔接受(文件目录,字符串名称){    名称= name.toLowerCase();
    如果(name.endsWith()非盟)返回true;
    如果(name.endsWith(WAV))返回true;
    返回false;}}


解决方案

下面是扮演一个短片的简单方法。

 进口javax.sound.sampled中*。
进口的java.net.URL;
进口javax.swing.JOptionPane中;类ClipTest {  公共静态无效的主要(字串[] args)抛出异常{
    字符串clipName = NULL;
    如果(args.length == 1){
      clipName = ARGS [0];
    }其他{
      clipName =HTTP://ps$c$c.org/media/leftright.wav
    }
    的System.out.println(循环'+ clipName +');
    网址URL =新的URL(clipName);
    AIS的AudioInputStream = AudioSystem.getAudioInputStream(URL);
    夹夹= AudioSystem.getClip();
    clip.open(AIS);
    clip.loop(2);
    clip.start();
    JOptionPane.showMessageDialog(NULL,关闭结束..);
  }
}

样品输入/输出。

  F:\\凸出>的java ClipTest HTTP://ps$c$c.org/media/100_2817-linear.wav
循环的http://ps$c$c.org/media/100_2817-linear.wav。F:\\凸出>的java ClipTest
循环的http://ps$c$c.org/media/leftright.wav。F:\\凸出>

I want to be able to make either a GUI or console application where the user clicks a button to select an audio file from their computer (of a compatible format) and it plays, and as I'm completely inexperienced in GUIs, it would be nice if I could be given a hint as to how to implement a pause and play button, as well as a volume slide/dial and a stop button. All I know is I'm gonna have to import java.io.* and sun.audio.*.

EDIT My current code is thus:

import sun.audio.*; //import the sun.audio package
import java.awt.*;
import java.io.*;


public class Boombox extends Frame implements FilenameFilter {

/**
 * 
 */
private static final long serialVersionUID = 4914433234899026080L;
Button openButton = new Button("Open");  
Button playButton = new Button("Play");
Button loopButton = new Button("Loop");
Button stopButton = new Button("Stop");
Label filename = new Label("                   ");
File theFile = null;
@SuppressWarnings({ "restriction" })
AudioData theData = null;
InputStream nowPlaying = null;

@SuppressWarnings({ "deprecation" })
public Boombox() {
    super("Boombox");
    resize(300, 200);
    Panel north = new Panel();
    north.setLayout(new FlowLayout(FlowLayout.LEFT));
    north.add(new Label("File: "));
    north.add("North", filename);
    add("North", north);
    Panel south = new Panel();
    south.add(openButton);
    south.add(playButton);
    south.add(loopButton);
    south.add(stopButton);
    add("South", south);
}

@SuppressWarnings("deprecation")
public static void main(String[] args) {
    Boombox sp = new Boombox();
    sp.show();
}

@SuppressWarnings({ "deprecation", "restriction" })
public void open() {
    FileDialog fd = new FileDialog(this, "Please select a .au file:");
    fd.setFilenameFilter(this);
    fd.show();
    try {
        theFile = new File(fd.getDirectory() + "/" + fd.getFile());
        if (theFile != null) {
            filename.setText(theFile.getName());
            FileInputStream fis = new FileInputStream(theFile);
            AudioStream as = new AudioStream(fis);
            theData = as.getData();
        }
    }
    catch (IOException e) {
        System.err.println(e);
    }
}

@SuppressWarnings("restriction")
public void play() {
    stop();    
    if (theData == null) open();
    if (theData != null) {
        AudioDataStream ads = new AudioDataStream(theData);
        AudioPlayer.player.start(ads);
        nowPlaying = ads;
    }
}

@SuppressWarnings("restriction")
public void stop() {
    if (nowPlaying != null) {
        AudioPlayer.player.stop(nowPlaying);
        nowPlaying = null;
    }
}

@SuppressWarnings("restriction")
public void loop() {
    stop();
    if (theData == null) open();
    if (theData != null) {
        ContinuousAudioDataStream cads = new ContinuousAudioDataStream(theData);
        AudioPlayer.player.start(cads);
        nowPlaying = cads;
    }
}

public boolean action(Event e, Object what) {

    if (e.target == playButton) {
        play();
        return true;
    }
    else if (e.target == openButton) {
        open();
        return true;
    }
    else if (e.target == loopButton) {
        loop();
        return true;
    }
    else if (e.target == stopButton) {
        stop();
        return true;
    }

    return false;

}

public boolean accept(File dir, String name) {

    name = name.toLowerCase();
    if (name.endsWith(".au")) return true;
    if (name.endsWith(".wav")) return true;
    return false;

}

}

解决方案

Here is a simple way to play a short clip.

import javax.sound.sampled.*;
import java.net.URL;
import javax.swing.JOptionPane;

class ClipTest {

  public static void main(String[] args) throws Exception {
    String clipName = null;
    if (args.length==1) {
      clipName = args[0];
    } else {
      clipName = "http://pscode.org/media/leftright.wav";
    }
    System.out.println("Looping '" + clipName + "'.");
    URL url = new URL(clipName);
    AudioInputStream ais = AudioSystem.getAudioInputStream(url);
    Clip clip = AudioSystem.getClip();
    clip.open( ais );
    clip.loop(2);
    clip.start();
    JOptionPane.showMessageDialog(null, "Close to end..");
  }
}

Sample input/output.

F:\proj>java ClipTest http://pscode.org/media/100_2817-linear.wav
Looping 'http://pscode.org/media/100_2817-linear.wav'.

F:\proj>java ClipTest
Looping 'http://pscode.org/media/leftright.wav'.

F:\proj>

这篇关于如何让用户选择一个音频文件,并在Java中播放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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