Home > Java > javaTutorial > How to Play .WAV Files in Java with Ease?

How to Play .WAV Files in Java with Ease?

Mary-Kate Olsen
Release: 2024-11-13 01:54:02
Original
829 people have browsed it

How to Play .WAV Files in Java with Ease?

Play .WAV Files with Ease in Java

Playing audio files in Java can be a breeze with the right tools and techniques. Let's tackle this challenge by playing a simple beep sound when a button is pressed.

Code Snippet for Playing .WAV Files

To achieve this, the following Java code snippet leverages advanced audio capabilities:

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;

public class PlaySound {

    private final int BUFFER_SIZE = 128000;
    private File soundFile;
    private AudioInputStream audioStream;
    private AudioFormat audioFormat;
    private SourceDataLine sourceLine;

    public void playSound(String filename){

        String strFilename = filename;

        try {
            soundFile = new File(strFilename);
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(1);
        }

        try {
            audioStream = AudioSystem.getAudioInputStream(soundFile);
        } catch (Exception e){
            e.printStackTrace();
            System.exit(1);
        }

        audioFormat = audioStream.getFormat();

        DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);
        try {
            sourceLine = (SourceDataLine) AudioSystem.getLine(info);
            sourceLine.open(audioFormat);
        } catch (LineUnavailableException e) {
            e.printStackTrace();
            System.exit(1);
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(1);
        }

        sourceLine.start();

        int nBytesRead = 0;
        byte[] abData = new byte[BUFFER_SIZE];
        while (nBytesRead != -1) {
            try {
                nBytesRead = audioStream.read(abData, 0, abData.length);
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (nBytesRead >= 0) {
                @SuppressWarnings("unused")
                int nBytesWritten = sourceLine.write(abData, 0, nBytesRead);
            }
        }

        sourceLine.drain();
        sourceLine.close();
    }
}
Copy after login

Step-by-Step Explanation

  • The playSound() method takes the filename of the WAV file as an input parameter.
  • It initializes various audio-related objects, including file, stream, format, and sound line.
  • The code reads the WAV file and opens the source line to prepare for playback.
  • A while loop reads the file in chunks and writes it to the source line.
  • Once the file is fully read, the source line is drained and closed, ensuring clean audio playback.

The above is the detailed content of How to Play .WAV Files in Java with Ease?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template