Reading Audio file in java - java

I have written this code for playing audio file,and I want to get indication when my audio file ends after playing. I have tried AS.getMicrosecondLength() == AS.getMicrosecondPosition() but these methods are undefined for the AudioStream.
My Code:
import java.io.FileInputStream;
import sun.audio.AudioPlayer;
import sun.audio.AudioStream;
public class A {
public static void main(String arg[]) throws Exception {
AudioStream AS = new AudioStream(new FileInputStream("sounds.wav"));
AudioPlayer.player.start(AS);
}
}
Please tell how I can solve my problem

Related

I want to play a sound in Java and it doesn`t work

Hello I want to make an alarm clock and now I`m at the part at makeing the sound play....I wrote this
package audio;
import sun.audio.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import sun.audio.AudioPlayer;
import sun.audio.AudioStream;
public class Audio {
public static void main(String[] args) {
InputStream in;
try{
in = new FileInputStream(new File("‪sw.wav"));
AudioStream audio = new AudioStream(in);
AudioPlayer.player.start(audio);
}
catch(Exception e){}
}
}
And it doesn`t play the sound It doesnt give me an error it doesnt do nothing
I also put the direct pathway in a folder C:\...etc
try this: move the audio file to the source folder, then modify the path of the File in the form: new File("‪./sw.wav") and then do something helpful with the exception...
public static void main(String[] args) {
InputStream in;
try{
in = new FileInputStream(new File("‪./sw.wav"));
AudioStream audio = new AudioStream(in);
AudioPlayer.player.start(audio);
}
catch(Exception e){
//print some helpfull info from the stack trace
}
}
The problem in defining the path of FileInputStream you should provide the full path of your audio file and seperate it with double slashes \\:
in = new FileInputStream("C:\\Users\\serban\\Documents\\wav sound\\sw.wav");
Or instead you can move the audio file to the source folder of your java program and then put:
in = new FileInputStream("sw.wav");

"Attributes and objects cannot be resolved" - error

The following code is for reading or writing files with java, but:
Eclipse prints these errors:
buffer_1 cannot be resolved to a variable
file_reader cannot be resolved
also other attributes...
what is wrong in this code here:
//Class File_RW
package R_2;
import java.io.File;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.lang.NullPointerException;
public class File_RW {
public File_RW() throws FileNotFoundException, NullPointerException {
File file_to_read = new File("C:/myfiletoread.txt");
FileReader file_reader = new FileReader(file_to_read);
int nr_letters = (int)file_to_read.length()/Character.BYTES;
char buffer_1[] = new char[nr_letters];
}
public void read() {
file_reader.read(buffer_1, 0, nr_letters);
}
public void print() {
System.out.println(buffer_1);
}
public void close() {
file_reader.close();
}
public File get_file_to_read() {
return file_to_read;
}
public int get_nr_letters() {
return nr_letters;
}
public char[] get_buffer_1() {
return buffer_1;
}
//...
}
//main method # class Start:
package R_2;
import java.io.File;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.lang.NullPointerException;
public class Start {
public static void main(String[] args) {
File_RW file = null;
try {
file = new File_RW();
} catch (NullPointerException e_1) {
System.out.println("File not found.");
}
//...
}
}
I can't find any mistake. I have also tried to include a try catch statement into the constructor of the class "File_RW", but the error messages were the same.
Yes, there are errors in your code - which are of really basic nature: you are declaring variables instead of fields.
Meaning: you have them in the constructor, but they need to go one layer up! When you declare an entity within a constructor or method, then it is a variable that only exists within that constructor/method.
If you want that multiple methods can make use of that entity, it needs to be a field, declared in the scope of the enclosing class, like:
class FileRW {
private File fileToRead = new File...
...
and then you can use your fields within all your methods! Please note: you can do the actual setup within your constructor:
class FileRW {
private File fileToRead;
public FileRW() {
fileToRead = ..
but you don't have to.
Finally: please read about java language conventions. You avoid using "_" within names (just for SOME_CONSTANT)!
javacode already running...thx
same program edited with c++ in visual Studio express...
visit the stackoverflow entry link:
c++ file read write-error: Microsoft Visual C++ Runtime libr..debug Assertion failed, expr. stream.valid()

Why is DataOutputStream in java not working as expected?

I am learning about file IO in java, and wanted to test this, but I am not sure why I am getting weird results. Here is the code.
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.DataOutputStream;
public class driver {
public static void main(String[] args) throws IOException {
FileOutputStream out = new FileOutputStream("Hello.txt");
DataOutputStream dos = new DataOutputStream(out);
dos.writeBoolean(true);
dos.writeInt(68);
dos.writeChar('c');
dos.writeDouble(3.14);
dos.writeFloat(56.789f);
}
}
My input file "Hello.txt doesn't exist yet and I want to put all these values like 68, c, 3,14 etc into this file, however after running the above program, these are the contents of "Hello.txt".
D c# ¸Që…Bc'ð
So what exactly is happening here?

Beginning Android 4 Games Development error

In the book Beginning Android 4 games development by Mario Zechner and Robert Green I am following along to start making a game of my own. We create a framework for the game first and then implement it into one class, and I got one error which I could not figure out. The error occured when I tried to instantiate something from a class called AndroidFileIO.
This is how the book describes how to instantiate it:
FileIO fileIO
fileIO = new AndroidFileIO(getAssets());
and the class it gets it from is:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.AssetManager;
import android.os.Environment;
import android.preference.PreferenceManager;
import com.example.android4gamedevtut.FileIO;
public class AndroidFileIO implements FileIO{
Context context;
AssetManager assets;
String externalStoragePath;
public AndroidFileIO(Context assetManager) {
this.context = assetManager;
this.assets = assetManager.getAssets();
this.externalStoragePath = Environment.getExternalStorageDirectory()
.getAbsolutePath() + File.separator;
}
#Override
public InputStream readAsset(String fileName) throws IOException {
return assets.open(fileName);
}
#Override
public InputStream readFile(String fileName) throws IOException {
return new FileInputStream(externalStoragePath + fileName);
}
#Override
public OutputStream writeFile(String fileName) throws IOException {
return new FileOutputStream(externalStoragePath + fileName);
}
public SharedPreferences getPreferences() {
return PreferenceManager.getDefaultSharedPreferences(context);
}
}
The error that is stopping me is saying "The constructor AndroidFileIO(AssetManager) is undefined" and eclipses suggests too fixes to the problem. The first being to "change the constructor AndroidFileIO(Context) to AndroidFileIO(AssetManager)" and the second being "create a constructor AndroidFileIO(AssetManager)". Please answer this question in simple terms I am very new to java.
you should use
fileIO = new AndroidFileIO(this);
instead of getAsset();
refer to line 54 in https://code.google.com/p/beginning-android-games-2/source/browse/trunk/ch09-jumper/src/com/badlogic/androidgames/framework/impl/GLGame.java?r=4

End of Audio file in java

I have written this code for playing audio file, I want to get indication when my audio file ends after playing. I have tried AS.getMicrosecondLength() == AS.getMicrosecondPosition() but these methods are undefined for the AudioStream. Please tell how I can do that.
import java.io.FileInputStream;
import sun.audio.AudioPlayer;
import sun.audio.AudioStream;
public class A {
public static void main(String arg[]) throws Exception {
AudioStream AS = new AudioStream(new FileInputStream("sounds.wav"));
AudioPlayer.player.start(AS);
}
}
AS.getMicrosecondLength() == AS.getMicrosecondPosition() could be used to set off the flag when the clip has ended

Categories

Resources