GPWiki.org
GPWiki.org
It is currently Wed Jun 19, 2013 7:02 am

All times are UTC




Post new topic Reply to topic  [ 2 posts ] 
Author Message
PostPosted: Thu Jun 09, 2011 5:28 am 
Rookie
User avatar

Joined: Wed Jun 08, 2011 1:11 am
Posts: 3
Location: Australia
What do you use for audio with Java? OpenAl, OpenAl Soft, SDL, FMOD?


Top
 Profile  
 
PostPosted: Fri Aug 19, 2011 3:46 am 
Octogenarian

Joined: Thu Aug 04, 2011 12:00 am
Posts: 87
Location: Arizona
Here is an example of how to load and play a wave file in Java with a JFrame for printing out the audio file's properties. Just replace "gong.wav" with whatever file you want to try out. Java also supports aiff and... hmm... one other odd format I can't recall.

Code:
import java.awt.*;
import javax.swing.*;
import java.io.*;
import java.net.*;
import javax.sound.sampled.*;

public class PlaySound extends JFrame {

    String filename = "gong.wav";
    AudioInputStream sample;

    public static void main(String[] args) {
        new PlaySound();
    }
   
    public PlaySound() {
        super("PlaySound");
        setSize(500,400);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       
        try {
            sample = AudioSystem.getAudioInputStream(getURL(filename));
           
            //create a sound buffer
            Clip clip = AudioSystem.getClip();
           
            //load the audio file
            clip.open(sample);
           
            //play sample
            clip.start();
           
        } catch (MalformedURLException e) {
        } catch (IOException e) {
        } catch (LineUnavailableException e) {
        } catch (UnsupportedAudioFileException e) {
        } catch (Exception e) { }

        repaint();
    }

    public void paint(Graphics g) {
        g.drawString("Sample file: " + filename, 10, 40);
        g.drawString(sample.getFormat().toString(), 10, 55);
        g.drawString("Sampling rate: " + (int)sample.getFormat().
            getSampleRate(), 10, 70);
        g.drawString("Sample channels: " + sample.getFormat().getChannels() , 10, 85);
        g.drawString("Encoded format: " + sample.getFormat().getEncoding().toString(), 10, 100);
        g.drawString("Sample size: " + sample.getFormat().getSampleSizeInBits() + "-bit", 10, 115);
        g.drawString("Frame size: " + sample.getFormat().getFrameSize(), 10, 130);
    }

    private URL getURL(String filename) {
        URL url = null;
        try {
            url = this.getClass().getResource(filename);
        }
        catch (Exception e) { e.printStackTrace(); }
        return url;
    }
}

_________________
Amazon Author Page
Game Dev Forum
Facebook Page


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group