James Dark

Greenhorn
+ Follow
since Mar 06, 2004
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by James Dark

Hi. The code below is to play a song but needs to stop when the user types the number "1." How do you make it so the public Reminder(int seconds) will stop which will then make the song stop? After it stops, I want to be able to do other things so I don't want the program to end.


Thanks for any help.


import apcslib.*;
import chn.util.*;
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;

import java.util.Timer;
import java.util.TimerTask;

public class Reminder {
Timer timer;
private static final intEXTERNAL_BUFFER_SIZE = 128000;
ConsoleIO console = new ConsoleIO();

public Reminder(int seconds) {
timer = new Timer(true);
timer.schedule(new RemindTask(), seconds*1000);

File soundFile = new File("C:\\Program Files\\Counter-Strike\\cstrike\\sound\\training\\cstrain6.wav");
AudioInputStreamaudioInputStream = null;
try
{
audioInputStream = AudioSystem.getAudioInputStream(soundFile);
}
catch (Exception e)
{
e.printStackTrace();
System.exit(1);
}
AudioFormataudioFormat = audioInputStream.getFormat();
SourceDataLineline = null;
DataLine.Infoinfo = new DataLine.Info(SourceDataLine.class, audioFormat);
try
{
line = (SourceDataLine) AudioSystem.getLine(info);
line.open(audioFormat);
}
catch (LineUnavailableException e)
{
e.printStackTrace();
System.exit(1);
}
catch (Exception e)
{
e.printStackTrace();
System.exit(1);
}
line.start();
intnBytesRead = 0;
byte[]abData = new byte[EXTERNAL_BUFFER_SIZE];
while (nBytesRead != -1)
{
try
{
nBytesRead = audioInputStream.read(abData, 0, abData.length);
}
catch (IOException e)
{
e.printStackTrace();
}
if (nBytesRead >= 0)
{
intnBytesWritten = line.write(abData, 0, nBytesRead);
}
}
line.drain();
line.close();

}

class RemindTask extends TimerTask {
public void run() {
System.out.println("Time's up!");
timer.cancel(); //Terminate the timer thread
int a;
ConsoleIO console = new ConsoleIO();

System.out.print("Would you choose 1 or 2: ");
a = console.readInt();
if (a == 1)
Thread.currentThread().interrupt();//Need help here...

}
}

public static void main(String args[]) {


System.out.println("About to schedule task.");
new Reminder(0);
System.out.println("Task scheduled.");

}
}
Hi. I've nearly completed my computer science project but have one major problem. When I play my music files, it makes it so nothing else can occur at the same time. Is there anyway to play a music file and have my program run simultaneously?

Thanks for your help.
At the moment I'm trying to work on this but I don't know how to do so. The farthest I get is this:

I removed the comments to decrease the length...

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 Sound
{
private static final int EXTERNAL_BUFFER_SIZE = 128000;

public void song()
{

File soundFile = new File("C:\\Documents and Settings\\James Bond\\Desktop\\Junk Stuff\\hlss300\\Sounds\\File.wav");

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

AudioFormat audioFormat = audioInputStream.getFormat();

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

int nBytesRead = 0;
byte[] abData = new byte[EXTERNAL_BUFFER_SIZE];
while (nBytesRead != -1)
{
try
{
nBytesRead = audioInputStream.read(abData, 0, abData.length);
}
catch (IOException e)
{
e.printStackTrace();
}
if (nBytesRead >= 0)
{
int nBytesWritten = line.write(abData, 0, nBytesRead);
}
}

line.drain();

line.close();

System.exit(0);
}

private static void printUsageAndExit()
{
out("SimpleAudioPlayer: usage:");
out("\tjava SimpleAudioPlayer <soundfile>");
System.exit(1);
}

private static void out(String strMessage)
{
System.out.println(strMessage);
}

}

Please help me out. My teacher never instructed us and made us learn java on our own.
19 years ago
Thanks again! You helped me save so many lines of code!
19 years ago
Thanks man. Was looking for the API file.
19 years ago
Hi. I wanted to know how to be able to place a statement that will check if either letters are equal to the letter stored in a[0]. I know I could break it up but would like to know how to do it in one line. Do you have to do ASCII keys or something?

//a[0] is a String array that contains either "C" or "c"
//e[0] is a String array that holds words
if (a[0] == ("C" || "c"))
e[0] = "Correct";

Any help is appreciated. Thanks.
19 years ago
Hi. At the moment, I created an ASCII picture with many system.outs but am wondering if you can make a small millisecond pause between each line being printed. I'm not sure if this is the sleep method. If you can show me how to do this, it be great.

Thank you.
19 years ago
Hi! The following code I have is a sound file. At the moment, I'm creating a final project for my computer science class. I wanted to be creative and add sound to my project. The following code allows you to play songs but you can only play one. I want to make it so I can call the song from another class (from my other main, which holds the acual program) in which it is automatically played when placed inside the main (it will start when called). Is this possible?


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 Sound
{
private static final intEXTERNAL_BUFFER_SIZE = 128000;

public static void main(String[] args)
{
/*
Now, that we're shure there is an argument, we
take it as the filename of the soundfile
we want to play.
*/
//StringstrFilename = args[0];
FilesoundFile = new File("C:\\Documents and Settings\\James Bond\\Desktop\\Junk Stuff\\hlss300\\Sounds\\File.wav");

/*
We have to read in the sound file.
*/
AudioInputStreamaudioInputStream = null;
try
{
audioInputStream = AudioSystem.getAudioInputStream(soundFile);
}
catch (Exception e)
{
/*
In case of an exception, we dump the exception
including the stack trace to the console output.
Then, we exit the program.
*/
e.printStackTrace();
System.exit(1);
}

/*
From the AudioInputStream, i.e. from the sound file,
we fetch information about the format of the
audio data.
These information include the sampling frequency,
the number of
channels and the size of the samples.
These information
are needed to ask Java Sound for a suitable output line
for this audio file.
*/
AudioFormataudioFormat = audioInputStream.getFormat();

/*
Asking for a line is a rather tricky thing.
We have to construct an Info object that specifies
the desired properties for the line.
First, we have to say which kind of line we want. The
possibilities are: SourceDataLine (for playback), Clip
(for repeated playback)and TargetDataLine (for
recording).
Here, we want to do normal playback, so we ask for
a SourceDataLine.
Then, we have to pass an AudioFormat object, so that
the Line knows which format the data passed to it
will have.
Furthermore, we can give Java Sound a hint about how
big the internal buffer for the line should be. This
isn't used here, signaling that we
don't care about the exact size. Java Sound will use
some default value for the buffer size.
*/
SourceDataLineline = null;
DataLine.Infoinfo = new DataLine.Info(SourceDataLine.class,
audioFormat);
try
{
line = (SourceDataLine) AudioSystem.getLine(info);

/*
The line is there, but it is not yet ready to
receive audio data. We have to open the line.
*/
line.open(audioFormat);
}
catch (LineUnavailableException e)
{
e.printStackTrace();
System.exit(1);
}
catch (Exception e)
{
e.printStackTrace();
System.exit(1);
}

/*
Still not enough. The line now can receive data,
but will not pass them on to the audio output device
(which means to your sound card). This has to be
activated.
*/
line.start();

/*
Ok, finally the line is prepared. Now comes the real
job: we have to write data to the line. We do this
in a loop. First, we read data from the
AudioInputStream to a buffer. Then, we write from
this buffer to the Line. This is done until the end
of the file is reached, which is detected by a
return value of -1 from the read method of the
AudioInputStream.
*/
intnBytesRead = 0;
byte[]abData = new byte[EXTERNAL_BUFFER_SIZE];
while (nBytesRead != -1)
{
try
{
nBytesRead = audioInputStream.read(abData, 0, abData.length);
}
catch (IOException e)
{
e.printStackTrace();
}
if (nBytesRead >= 0)
{
intnBytesWritten = line.write(abData, 0, nBytesRead);
}
}

/*
Wait until all data are played.
This is only necessary because of the bug noted below.
(If we do not wait, we would interrupt the playback by
prematurely closing the line and exiting the VM.)

Thanks to Margie Fitch for bringing me on the right
path to this solution.
*/
line.drain();

/*
All data are played. We can close the shop.
*/
line.close();

/*
There is a bug in the jdk1.3/1.4.
It prevents correct termination of the VM.
So we have to exit ourselves.
*/
System.exit(0);
}

private static void printUsageAndExit()
{
out("SimpleAudioPlayer: usage:");
out("\tjava SimpleAudioPlayer <soundfile>");
System.exit(1);
}

private static void out(String strMessage)
{
System.out.println(strMessage);
}

}
[ June 03, 2004: Message edited by: James Dark ]
19 years ago
Hi. I'm currently working on a final project for AP Computer Science 1 and have question about inheritance. If the superclass were to have try and catches, will the subclass inherit those characteristics? (Code provided below). If this is possible, could someone show me how to make

FilesoundFile = new File("C:\\WINNT\\Media\\notify.wav");

into a method or somehow other classes able to change this file root so different songs can be played through different classes. I could copy and paste this code to each class but I don't want to increase the size of the code.
Thank for helping me. I will provide you my final project in return for your help.


public class Sound
{
private static final intEXTERNAL_BUFFER_SIZE = 128000;



public static void main(String[] args)
{
/*
We check that there is exactely one command-line
argument.
If not, we display the usage message and exit.
*/

/*
Now, that we're shure there is an argument, we
take it as the filename of the soundfile
we want to play.
*/


FilesoundFile = new File("C:\\WINNT\\Media\\notify.wav");


/*
We have to read in the sound file.
*/
AudioInputStreamaudioInputStream = null;
try
{
audioInputStream = AudioSystem.getAudioInputStream(soundFile);
}
catch (Exception e)
{
/*
In case of an exception, we dump the exception
including the stack trace to the console output.
Then, we exit the program.
*/
e.printStackTrace();
System.exit(1);
}

/*
From the AudioInputStream, i.e. from the sound file,
we fetch information about the format of the
audio data.
These information include the sampling frequency,
the number of
channels and the size of the samples.
These information
are needed to ask Java Sound for a suitable output line
for this audio file.
*/
AudioFormataudioFormat = audioInputStream.getFormat();

/*
Asking for a line is a rather tricky thing.
We have to construct an Info object that specifies
the desired properties for the line.
First, we have to say which kind of line we want. The
possibilities are: SourceDataLine (for playback), Clip
(for repeated playback)and TargetDataLine (for
recording).
Here, we want to do normal playback, so we ask for
a SourceDataLine.
Then, we have to pass an AudioFormat object, so that
the Line knows which format the data passed to it
will have.
Furthermore, we can give Java Sound a hint about how
big the internal buffer for the line should be. This
isn't used here, signaling that we
don't care about the exact size. Java Sound will use
some default value for the buffer size.
*/
SourceDataLineline = null;
DataLine.Infoinfo = new DataLine.Info(SourceDataLine.class,
audioFormat);
try
{
line = (SourceDataLine) AudioSystem.getLine(info);

/*
The line is there, but it is not yet ready to
receive audio data. We have to open the line.
*/
line.open(audioFormat);
}
catch (LineUnavailableException e)
{
e.printStackTrace();
System.exit(1);
}
catch (Exception e)
{
e.printStackTrace();
System.exit(1);
}

/*
Still not enough. The line now can receive data,
but will not pass them on to the audio output device
(which means to your sound card). This has to be
activated.
*/
line.start();

/*
Ok, finally the line is prepared. Now comes the real
job: we have to write data to the line. We do this
in a loop. First, we read data from the
AudioInputStream to a buffer. Then, we write from
this buffer to the Line. This is done until the end
of the file is reached, which is detected by a
return value of -1 from the read method of the
AudioInputStream.
*/
intnBytesRead = 0;
byte[]abData = new byte[EXTERNAL_BUFFER_SIZE];
while (nBytesRead != -1)
{
try
{
nBytesRead = audioInputStream.read(abData, 0, abData.length);
}
catch (IOException e)
{
e.printStackTrace();
}
if (nBytesRead >= 0)
{
intnBytesWritten = line.write(abData, 0, nBytesRead);
}
}

/*
Wait until all data are played.
This is only necessary because of the bug noted below.
(If we do not wait, we would interrupt the playback by
prematurely closing the line and exiting the VM.)

Thanks to Margie Fitch for bringing me on the right
path to this solution.
*/
line.drain();

/*
All data are played. We can close the shop.
*/
line.close();

/*
There is a bug in the jdk1.3/1.4.
It prevents correct termination of the VM.
So we have to exit ourselves.
*/
System.exit(0);
}


private static void printUsageAndExit()
{
out("SimpleAudioPlayer: usage:");
out("\tjava SimpleAudioPlayer <soundfile>");
System.exit(1);
}


private static void out(String strMessage)
{
System.out.println(strMessage);
}
}
19 years ago
Thanks for you help. I will try to find out how to include sound.
19 years ago
Hi. I'm currently in AP Computer Science 1 and am working on a final project. Within it, I was doing a multi-program and wanted to include sound to make the project more impressive. At the moment, I'm using JCreator LE and wanted to know how to include sound in the project. If you know how, could you please help me work through it, by telling me the steps? Your help is greatly appreciated. If you have any other suggestion of improving the project, please tell me (anything that does not involve java applets). My program will contain: (1) Multiple Choice Trivia with 4 different difficulties, (2) Able to convert different measurements such as meter to millimeter, (3) Able to find the number has a prime, (4) A game of High Low. Any help will be totally appreciated.
Thank you.
19 years ago