• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Understanding how this code works

 
jorge Garcia
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I built a class to play a sound wav file and I had some trouble getting it to work from a jar file.

I found this code online and it solved my problem but I just don't get what it's doing.

This is the: in= this.getClass().getResourceAsStream("/audio/Bell.wav");

I don't see what 'this.getClass() is doing exactly? 'in' is an InputStream object so is 'this.getClass()' getting an instance of the class vs. getting an object?

Here is the full class code:

import javax.sound.sampled.*;
import java.io.*;
import java.net.*;




public class SoundTest {

//variable declaration

AudioInputStream stream;
AudioFormat format;
Clip clip;
DataLine.Info info;
InputStream in;


public static void main(String[] args) {

SoundTest soundtest = new SoundTest();
soundtest.go();

}

public void go() {

try{
in= this.getClass().getResourceAsStream("/audio/Bell.wav");
}catch(Exception e) {}

try {
stream = AudioSystem.getAudioInputStream(in);
System.out.println("stream");
} catch (Exception e) {}


format = stream.getFormat();
System.out.println("format");
info = new DataLine.Info(Clip.class,format);

try{
clip = (Clip) AudioSystem.getLine(info);
clip.open(stream);
System.out.println("Start is next");
System.out.println(Thread.currentThread());
clip.start();
System.out.println("Line after Start");
System.out.println(Thread.currentThread());
Thread.sleep(10000);
System.out.println(Thread.currentThread());
System.out.println("Line after Thread-Sleep");
} catch (Exception a) {}

}//close go

}//close class
 
Bill Shirley
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is a reference to the current object you are executing code within,

getClass() is the class object it is an instance of

MyClass.class.getResourceAsStream("..."); // just about the same
 
jorge Garcia
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Bill. I got it now, the object is a class.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic