【问题标题】:Play a sound while using try and catch使用 try and catch 时播放声音
【发布时间】:2019-02-20 12:24:30
【问题描述】:

无法编译。我正在尝试在使用 try and catch 时播放声音。 wav 文件位于我的桌面上。 我收到错误,例如: AudioStream 无法解析为类型 AudioStream 无法解析为类型 无法解析 AudioPlayer。

我不确定如何解决这个问题,我知道这很简单,

import java.io.FileInputStream;
import java.io.InputStream;

public class PlayMySoundApplication
{
public static void main(String[] args) 
 throws Exception
{
// open the sound file as a Java input stream
String applause2x = "/Users/pc/Desktop/applause2x.wav";
InputStream in = new FileInputStream(applause2x);

// create an audiostream from the inputstream
AudioStream audioStream = new AudioStream(in);

// play the audio clip with the audioplayer class
AudioPlayer.player.start(audioStream);

【问题讨论】:

  • 1. try & catch 用于处理异常。 2. 我在你发布的代码中看不到你的 try & catch
  • 3.您尚未导入 AudioStreamAudioPlayer
  • 在上面的代码中,throws Exception 语句应该作为一个动作监听器。
  • @JonathanArendt throws Exception 应该替换为 try-catch
  • 同意,但是导致问题的原因是什么?缺少导入 AudioPlayerAudioStream 类?

标签: java file-io try-catch fileinputstream


【解决方案1】:

我不知道您要使用什么声音库。

如果您使用的是不错的 JDK 版本,那应该可以解决问题:

import java.io.File;
import java.io.IOException;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.UnsupportedAudioFileException;

public class MakeSound {

    public static void main(String[] args) throws LineUnavailableException, UnsupportedAudioFileException, IOException {
        playSound("test.wav");
    }

    public static void playSound(String strFilename)
            throws LineUnavailableException, UnsupportedAudioFileException, IOException {

        File soundFile = new File(strFilename);

        AudioInputStream audioStream = AudioSystem.getAudioInputStream(soundFile);

        AudioFormat audioFormat = audioStream.getFormat();

        DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);
        try (SourceDataLine sourceLine = (SourceDataLine) AudioSystem.getLine(info);) {
            sourceLine.open(audioFormat);
            sourceLine.start();

            int nBytesRead = 0;
            byte[] abData = new byte[128000];
            while (nBytesRead != -1) {
                nBytesRead = audioStream.read(abData, 0, abData.length);
                if (nBytesRead > 0) {
                    sourceLine.write(abData, 0, nBytesRead);
                }
            }

            sourceLine.drain();
        }
    }
}

HTH!

【讨论】:

    【解决方案2】:

    为音频添加导入,

    import java.io.*;
    import sun.audio.*;
    
    
    public class PlayMySoundApplication
    {
      public static void main(String[] args) 
      throws Exception
      {
        // open the sound file as a Java input stream
        String applause2x = "/Users/pc/Desktop/applause2x.wav";
    InputStream in = new FileInputStream(applause2x);
    
    
        // create an audiostream from the inputstream
        AudioStream audioStream = new AudioStream(in);
    
        // play the audio clip with the audioplayer class
        AudioPlayer.player.start(audioStream);
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多