AudioStream cannot find file Java - java

I am trying to make background music for my game, but when I try to get the file, it brings up the error File Not Found. Here is my code:
package TBG;
import sun.audio.*;
import java.io.*;
public class Sound {
public static void main(String[] args) {
//File here = new File("Slow.wav");
//System.out.println(here.getAbsolutePath());
music();
}
public static void music(){
AudioPlayer MGP = AudioPlayer.player;
AudioData MD;
ContinuousAudioDataStream loop = null;
try{
AudioStream BGM = new AudioStream(new FileInputStream("C:/Users/GrffinStout/Documents/Eclipse Stuff/G Text RPG/Slow.wav"));
MD = BGM.getData();
loop = new ContinuousAudioDataStream(MD);
}catch(IOException error){}
MGP.start(loop);
}
}

Mac file paths don't use C: - that is only for Windows.
On Macs (or any Unix or Linux system) absolute file paths start with / so you just want /Users/GrffinStout/Documents/Eclipse Stuff/G Text RPG/Slow.wav.

Related

Java can't read a text file

I am trying to read a .txt file and search for a word, but the program just closes with Process finished with exit code 0.
import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;
public class LogParser {
static Scanner file;
static ArrayList text = new ArrayList();
static String path = new String();
static String check = new String();
private static int a = 0;
static Scanner inpunt = new Scanner(System.in);
public static void main (String[] args) {
System.out.println("Input path to file");
path = inpunt.nextLine();
File texts = new File(path);
try {
file = new Scanner(new File(path));
} catch (Exception e) {
System.out.println("Can't open file");
}
try {
while (file.hasNext()) {
text.add(a, file.nextLine());
check = text.get(a).toString();
if (check.contains("cap"))
System.out.println("Allert!!!!!!!!!!!!!!!!!!!!!!!!!!!" + text.get(a));
a = a + 1;
}
} catch (Exception e) {
// System.out.println("Can't open file");
if (file.toString().contains("cap"))
System.out.println("cap" + "Path to file: " + path);
System.out.println(text.size());
}
}
}
The text in the .txt file is:
let's try read this cap
If I try to open an xml file, everything is ok. My problem is only in txt files.
As mentioned in the comments, your path variable isn't set. You're trying to create a new file and passing in a path that hasn't been instantiated.

Playing music continuously in the loop in java

I have written this code for playing music in my java program, but I want to play the music continuously in the loop, for that I have tried an infinite loop as well but it is not working. Please tell how it possible to play the music continuously?
import java.io.FileInputStream;
import sun.audio.*;
public class A {
public static void main(String arg[]) throws Exception {
AudioPlayer MGP = AudioPlayer.player;
AudioStream BGM = new AudioStream(new FileInputStream("sounds.wav"));
AudioPlayer.player.start(BGM);
}
}
From the JavaDoc:
To play a continuous sound you first have to create an AudioData instance and use it to construct a ContinuousAudioDataStream. For example:
AudioData data = new AudioStream(url.openStream()).getData();
ContinuousAudioDataStream audiostream = new ContinuousAudioDataStream(data);
AudioPlayer.player.start(audiostream);
I really didn't think it would be that difficult to adapt the docs.
import java.io.FileInputStream;
import sun.audio.*;
public class A {
public static void main(String arg[]) throws Exception {
AudioData data = new AudioStream(new FileInputStream("yourfile.wav")).getData();
ContinuousAudioDataStream BGM = new ContinuousAudioDataStream(data);
AudioPlayer.player.start(BGM);
}
}

playing music continuously in the loop

I have written this code for playing music, this code plays music once but I want to play it continuously in the loop please tell how I can do that?
import java.io.FileInputStream;
import sun.audio.*;
public class A {
public static void main(String arg[]) throws Exception {
AudioPlayer MGP = AudioPlayer.player;
AudioStream BGM = new AudioStream(new FileInputStream("sounds.wav"));
AudioPlayer.player.start(BGM);
}
}
According to the documentation, start will cause the player to:
Start playing a stream. The stream will continue to play until the stream runs out of data, or it is stopped.
Hence, you could just wrap it in an endless loop:
public static void main(String arg[]) throws Exception {
AudioPlayer MGP = AudioPlayer.player;
while(true) {
AudioStream BGM = new AudioStream(new FileInputStream("sounds.wav"));
AudioPlayer.player.start(BGM);
BGM.close();
}
}
}

copying a serialized file from one folder to other folder in every 2 minutes

Please do not consider this question as repeated one ,I have few files in a folder ter , that is ter folder is in c : drive and it contain a seriliazed file named gfr.ser so the complete path is (C:\ter\gfr.ser) , Now i want this gfr.ser file to be copied in another folder inside C: itself named bvg so I want file to copied to path (C:\bvg\gfr.ser) below is the java class , please advise can I achieve the same ,please advise
import java.util.TimerTask;
import java.util.Date;
import java.util.Timer;
// Create a class extends with TimerTask
public class ScheduledTask extends TimerTask {
// Add your task here
public void run() {
Runtime.getRuntime().exec("cmd.exe /c start c:\\ter\\gfr.ser");
}
}
//Main class
public class SchedulerMain {
public static void main(String args[]) throws InterruptedException {
Timer time = new Timer(); // Instantiate Timer Object
ScheduledTask st = new ScheduledTask(); // Instantiate SheduledTask class
time.schedule(task, now ,TimeUnit.SECONDS.toMillis(2));
}
}
I would say that easier option for you is probably first option from my link in comment (you wouldn't have to download additional libraries to your project). Here is simple code that you can use in your application
Path source = new File("C:\\ter\\gfr.ser").toPath();
Path target = new File("C:\\bvg\\gfr.ser").toPath();
Files.copy(source, target, StandardCopyOption.REPLACE_EXISTING);
But make sure that C:\bvg folder exists before you start copping.
Since you are using JDK 1.6 this example is better. I assume that you are trying to replace old file with new one. Here is how you can do it:
import java.io.*;
import java.util.*;
import java.util.concurrent.TimeUnit;
class ScheduledTask extends TimerTask {
public void run() {
InputStream inStream = null;
OutputStream outStream = null;
try {
File targetDirectory = new File("C:\\bvg");
if (!targetDirectory.exists()) targetDirectory.mkdirs();
File source = new File("C:\\ter\\gfr.ser");
File target = new File("C:\\bvg\\gfr.ser");
inStream = new FileInputStream(source);
outStream = new FileOutputStream(target);
byte[] buffer = new byte[1024];
int length;
// copy the file content in bytes
while ((length = inStream.read(buffer)) > 0) {
outStream.write(buffer, 0, length);
}
inStream.close();
outStream.close();
System.out.println("File is copied successful!");
} catch (IOException e) {
e.printStackTrace();
}
}
}
// Main class
public class SchedulerMain {
public static void main(String args[]) throws InterruptedException {
Timer time = new Timer();
ScheduledTask task = new ScheduledTask();
time.schedule(task, new Date(), TimeUnit.MINUTES.toMillis(2));
}
}

how to play wav file in java 1.4

as title
How can i play a sound file repeatedly in java v1.4?
If you just want to play the wav file then 'org.life.java''s answer is correct. For other format types you can use JMF( http://www.oracle.com/technetwork/java/javase/tech/index-jsp-140239.html ).
Note: JMF is obsolete now... But it will work with jdk 1.4
import java.net.URL;
import javax.sound.sampled.*;
public class LoopSound {
public static void main(String[] args) throws Exception {
URL url = new URL(
"http://pscode.org/media/leftright.wav");
Clip clip = AudioSystem.getClip();
AudioInputStream ais = AudioSystem.
getAudioInputStream( url );
clip.open(ais);
clip.loop(0);
javax.swing.JOptionPane.
showMessageDialog(null, "Close to exit!");
}
}
This will work in JDK 1.4 (tested in Windows XP and JDK 1.4.2_06).
The other answer fails because as correctly stated in the comments, AudioSystem.getClip() does not exist on JDK 1.4. Below is a complete source (in the form of a main function, but it's adaptable to anything else) that uses DataLine and plays in a separate Thread for better overall performance as well:
import java.io.File;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.SourceDataLine;
public class AudioTest {
public static void main(String[] args) throws Exception {
AudioInputStream ais = AudioSystem.getAudioInputStream(new File("C:/sound1.wav"));
AudioFormat format = ais.getFormat();
DataLine.Info dataLineInfo = new DataLine.Info(SourceDataLine.class, format);
SourceDataLine sourceDataLine = (SourceDataLine) AudioSystem.getLine(dataLineInfo);
class PlayThread extends Thread {
private AudioInputStream ais;
private AudioFormat format;
private SourceDataLine sourceDataLine;
byte tempBuffer[] = new byte[10000];
public PlayThread(AudioInputStream ais, SourceDataLine sourceDataLine, AudioFormat format) {
this.ais = ais;
this.sourceDataLine = sourceDataLine;
this.format = format;
}
public void run() {
try {
sourceDataLine.open(this.format);
sourceDataLine.start();
int cnt;
while ((cnt = this.ais.read(tempBuffer, 0, tempBuffer.length)) != -1) {
if (cnt > 0) {
sourceDataLine.write(tempBuffer, 0, cnt);
}
}
sourceDataLine.drain();
sourceDataLine.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
new PlayThread(ais, sourceDataLine, format).start();
}
}
Both question and answers are really old, but I just had to make this work on a fanless mini PC that only run windows XP so... ¯\_(ツ)_/¯

Categories