I am using amazon s3 bucket technology and I have a program that downloads bytes from parts of a .wav file and, for instance, I would call a method like
getPartOfASongFile(String filename, long byteToStartAt, long byteToEndAt)` and then play it using a Clip.
The program works as long as byteToStartAt==0. Anything above 0 and I get a
javax.sound.sampled.UnsupportedAudioFileException: could not get audio input stream from input file`.
My question is: How do I play this file if I want to get the byteToStartAt to be greater than zero?
This is basically the current program:
getPartOfASongFile(String filename,long byteToStartAt,long byteToEndAt){
File tempFile= null;
try {
tempFile = File.createTempFile(filename, ".wav");
} catch (IOException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
tempFile.deleteOnExit();
FileOutputStream out=null;
try {
out = new FileOutputStream(tempFile);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
GetObjectRequest rangeObjectRequest=new GetObjectRequest("myBucketName",filename);
rangeObjectRequest.setRange(byteToStartAt, byteToEndAt);
try {
IOUtils.copy(awsClient.getObject(rangeObjectRequest).getObjectContent(), out);
} catch (AmazonClientException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
AudioInputStream audioInputStream=null;
audioInputStream = AudioSystem.getAudioInputStream(tempFile);
Clip clip = AudioSystem.getClip();
clip.open(audioInputStream);
clip.start();
}
Related
I found something wrong when I write the Properties file by using FileOutputStream.
public synchronized static void setProperties(String file,String Properties,String value)
{
try {
FileInputStream is = new FileInputStream(file);
Properties proper = new Properties();
proper.load(is);
proper.setProperty(Properties.toUpperCase(), value);
is.close();
FileOutputStream os = new FileOutputStream(file);
proper.store(os,"Update the file:"+Properties);
os.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Look at the two lines below:
FileOutputStream os = new FileOutputStream(file);
proper.store(os,"Update the file:"+Properties);
When the first line executed, the file will be empty, until the second line execute finished. Now, I assume the second line will execute within 3 seconds. During this period, the program crashed or another reason lead to the file to be unsuccessfully written. I will get an empty Properties file when I'm running my program next time. Anyone can tell me how to prevent this kind of situation to occur?
I changed my program like below, Seems it more better than before. At least I won't get a empty properties file, Thanks all guys.
public synchronized static void setProperties(String file,String Properties,String value)
{
try {
FileInputStream is = new FileInputStream(file);
Properties proper = new Properties();
proper.load(is);
proper.setProperty(Properties.toUpperCase(), value);
is.close();
proper.store(new FileOutputStream(file+".tmp"),"Update the file:"+Properties); //Prevent empty file
File old = new File(file);
File tmp = new File(file+".tmp");
if(tmp.exists() && tmp.length()>0)
{
old.renameTo(new File(file+".old"));
tmp.renameTo(new File(file));
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
I have been trying to play music in my app. I've been using the example BigClip code:
try {
url = new URL(Sounds.class.getResourceAsStream("title1.wav").toString());
} catch (MalformedURLException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
BigClip clip = new BigClip();
AudioInputStream ais = null;
try {
ais = AudioSystem.getAudioInputStream(url);
} catch (UnsupportedAudioFileException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
clip.open(ais);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (LineUnavailableException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
clip.start();
JOptionPane.showMessageDialog(null, "BigClip.start()");
clip.loop(4);
JOptionPane.showMessageDialog(null, "BigClip.loop(4)");
clip.setFastForward(true);
clip.loop(8);
// the looping/FF combo. reveals a bug..
// there is a slight 'click' in the sound that should not be audible
JOptionPane.showMessageDialog(null, "Are you on speed?");
}
When I only use title1.wav, I get this error:
java.net.MalformedURLException: no protocol: java.io.BufferedInputStream
When I add the protocol file://, I get a NullPointerException, although I can't see what could be causing that.
Am I using the wrong protocol, or have I done something else wrong? Thanks in advance!
Assuming your file is in the same package ("directory") as the Sounds class, use
url = Sounds.class.getResource("title1.wav");
because
new URL(Sounds.class.getResourceAsStream("title1.wav").toString())
is just bound not to work. You are calling toString on an instance of InputStream.
The NPE probably happens because AudioSystem.getAudioInputStream fails due to bad URL path so ais is null and BigClip throws NPE from open.
I have this code that creates a file and saves the users input, but it keeps overwriting and I want it to save each entry a user gives. How can I do this?
File file = new File("info.txt");
BufferedWriter output = null;
try {
output = new BufferedWriter(new FileWriter(file));
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
output.write("Users pick: " + myint+ "\t");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
output.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//file writer
Change
output = new BufferedWriter(new FileWriter(file));
to
output = new BufferedWriter(new FileWriter(file,true));
for opening the file in append mode.
Im trying to implement the section "Using the Internal Storage" in http://developer.android.com/guide/topics/data/data-storage.html
I think I did the first part correctly, saving string to a file, but how do I read the string later?
Her is what my code looks like:
String FILEPROFILE = "profileinfo";
FileOutputStream fos = null;
BufferedInputStream fis = null;
OutputStream out = null;
try {
fos = openFileOutput(FILEPROFILE, Context.MODE_PRIVATE);
fos.write(profile.toString().getBytes());
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
fis = new BufferedInputStream(new FileInputStream(FILEPROFILE));
Log.d("UsersThoughts", "BufferedInputStream is " + fis.read());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
EDIT:
I change it to this:
try {
fis = openFileInput(FILEPROFILE);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
Log.d("UsersThoughts", "This blah object read " + fis.read());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.e("UsersThoughts", "This blah object trying to read: " + e.toString());
}
try{
Log.d("UsersThoughts", "This blah object toString " + fis.toString());
} catch (Exception e)
{
e.printStackTrace();
Log.e("UsersThoughts", "This blah object trying to make string: " + e.toString());
}
The output in the logcat looks like this:
11-15 18:41:34.862: D/UsersThoughts(7777): This blah object read123
11-15 18:41:34.862: D/UsersThoughts(7777): This blah object toString java.io.FileInputStream#46356128
Looks like it's reading the files as bites....how do I get the text back that I copied?
Follow the answer i gave here to store a String to a file in Android: Should I store data in sqlite in case user is offline?
I'm quoting this from the same source you've cited:
To read a file from internal storage:
Call openFileInput() and pass it the name of the file to read. This returns a FileInputStream.
Read bytes from the file with read().
Then close the stream with close().
EDIT: For example,
FileInputStream fis = openFileInput("FILENAME");
BufferedInputStream blah = new BufferedInputStream(fis);
Then do whatever you were doing earlier with this blah object.
I am new to J2ME. I want to play an audio song in my application. I have written
Player p = null;
try {
p = Manager.createPlayer(getClass().getResourceAsStream("aa.wav"),"audio/x-wav");
p.start();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MediaException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
where "aa.wav" is a wav format song placed in resource folder. when i debug this code
getClass().getResourceAsStream("aa.wav")
it returns null.
Can you please Help me thanks
if resource folder is under src then.
make it
getClass().getResourceAsStream("/resource/aa.wav")