Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

sound file java

import javax.sound.sampled.*;
import java.io.IOException;
import java.io.File;//what we need to import

public class Main{//what we need to put in the "Main class"
	public static void main(String[] args) throws InterruptedException, LineUnavailableException, IOException, UnsupportedAudioFileException { //mainmethod with exeptions
  		String thePath = "*Path*"; //insert the audiofile here
  		SoundPlayer player = new SoundPlayer();
  		player.play(thePath);
	}
}

public class SoundPlayer {//new class where the sounfile will be started and ended
    private static final int BUFFER = 4096;

    void play(String filePath) throws UnsupportedAudioFileException, IOException, LineUnavailableException {
        File soundFile = new File(filePath);
      	AudioInputStream Stream = AudioSystem.getAudioInputStream(soundFile);
      	AudioFormat formatAudio = Stream.getFormat();
      	DataLine.Info info = new DataLine.Info(SourceDataLine.class, formatAudio);
      	SourceDataLine Audio = (SourceDataLine) AudioSystem.getLine(info);
      	Audio.open(formatAudio);
      	Audio.start();
      	byte[] buffer = new byte[BUFFER];
      	int readBytes = -1;

      	while ((readBytes = Stream.read(buffer)) != -1) {
          	Audio.write(buffer, 0, readBytes);
        }

      	Audio.drain();
      	Audio.close();
        Stream.close();
    }
}
//If I was able to help you I would be happy about a Donation
Comment

PREVIOUS NEXT
Code Example
Java :: how to check if a person pressed a buuton in jframe 
Java :: Divide two integers without using multiplication, division and mod operator 
Java :: java system.out.println not working 
Java :: java list 
Java :: java super keyword 
Java :: java throws keyword 
Java :: android studio remove button onclick 
Java :: java compute sum and average of array elements 
Java :: class, interface, or enum expected java 
Java :: calling this in constructor java 
Java :: how to make popupwindow background blur in android 
Java :: java multithreading 
Java :: convert object array to int array java 
Java :: main method in java 
Java :: how to draw a triangle in java 
Java :: deserialize list jackson 
Java :: java bfs 
Java :: maximum arrays size in java 
Java :: return two values in java 
Java :: Java Access PriorityQueue Elements 
Java :: How to merge two sorted arrays into a single sorted bigger one? 
Java :: max int array java 
Java :: java android build secret keys 
Java :: android get user defined device name programmatically 
Java :: java how to make a parameter optional 
Java :: make a textarea not editable javafx 
Java :: selenium drag slider 
Java :: for loop condition java 
Java :: getresources in adapter android 
Java :: java string reduce 
ADD CONTENT
Topic
Content
Source link
Name
2+6 =