• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Understanding how this code works

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, Bill. I got it now, the object is a class.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic