Problems with BigClip - java

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.

Related

Downloading part of a song and then playing it

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();
}

How do I turn off a program with a java code?

Hi guys I open in my java application I open a browser page with this code:
String URL = "https://www.google.com/";
try {
java.awt.Desktop.getDesktop().browse(java.net.URI.create(URL));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Now I have to turn off the browser, Is there a code to do that?
No, there is no Java API to close the browser.
There sort-of is but it's not very reliable. But if you have to, you can try with keyboard shortcuts and mouse clicks using Java Robot API.
public class RobotTest {
public static void main(String[] args) {
String URL = "https://www.google.com/";
try {
java.awt.Desktop.getDesktop().browse(java.net.URI.create(URL));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
Thread.sleep(3*1000);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Robot javaRobot;
try {
javaRobot = new Robot();
javaRobot.keyPress(KeyEvent.VK_ALT);
javaRobot.keyPress(KeyEvent.VK_F4);
javaRobot.keyRelease(KeyEvent.VK_ALT);
javaRobot.keyRelease(KeyEvent.VK_F4);
} catch (AWTException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

Program won't run because variables "may be uninitialized"?

I'm trying to make a new thread for parsing xml from an rss feed. When I click run it says there are errors please correct them etc. I have 2 classes in my project. The other class has no errors and this class below has only warnings that a lot of the things in the try/catch statements may be uninitialized. I understand that and figured I should still be able to run the program anyways, I expect them to be initialized and if they're not that's fine I want to know about it. Is this really what's going on or am I missing something? I thought it would compile if something may be uninitialized but its not certainly uninitialized.
public class RssParse extends Thread {
Thread th=new Thread() {
public void run(){
System.out.println("1");
URL iotd;
try {
iotd = new URL("http://www.nasa.gov/rss/image_of_the_day.rss");
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("2");
BufferedReader in;
try {
in = new BufferedReader(new InputStreamReader(iotd.openStream()));
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.out.println("3");
XmlPullParserFactory factory;
try {
factory = XmlPullParserFactory.newInstance();
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
factory.setNamespaceAware(true);
System.out.println("4");
XmlPullParser xpp;
try {
xpp = factory.newPullParser();
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("5");
try {
xpp.setInput(in);
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("6");
int eventType;
try {
eventType = xpp.getEventType();
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(eventType+"!!!!!!!!!!!!!!!!");
while(eventType!=XmlPullParser.END_DOCUMENT){
if(eventType==XmlPullParser.START_DOCUMENT){
System.out.println("start");
}
}
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}//method
};//thread
}//class
Look at this try/catch block for example :
URL iotd;
try {
iotd = new URL("http://www.nasa.gov/rss/image_of_the_day.rss");
} catch (MalformedURLException e) {
e.printStackTrace();
}
If iotd = new URL("...") fails, iotd will remain uninitialized.
There are two ways to deal with this :
Assign a default value to iotd, like : URL iotd = null; However, it's bad here because if you use iotd later its value may be null and can throw a NullPointerException.
Stop the execution of your function if something failed instead of just printing the stack trace. For example you can add a return statement in the catch block :
URL iotd;
try {
iotd = new URL("http://www.nasa.gov/rss/image_of_the_day.rss");
} catch (MalformedURLException e) {
e.printStackTrace();
return;
}
All the warnings you are getting are because all your catch blocks are not dealing with the exception at all (just printing the stacktrace to standard out).
Let's see it through an example:
URL iotd;
try {
iotd = new URL("http://www.nasa.gov/rss/image_of_the_day.rss");
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
at that snipped you are declaring a iotd variable as a URL but without initializing it (not assigning any value), you do it inside the try block - which isn't wrong by the way. However if for any reason the statement inside the try block throws an exception program flow will go to the catch block leaving the iotd variable with its initial value (unassigned).
So, in that case, execution of the program will continue and when reaching this statement:
in = new BufferedReader(new InputStreamReader(iotd.openStream()));
it will find no value assigned to the iotd variable.
To remove the warning regarding the uninitialized value you can either assign a null value to the variable when declaring it or rethrow another exception inside the catch block, stopping the program flow.
In the other hand, the snippet you posted here is not just one class, it's actually two as you are extending the Thread class and then creating an anonymous one inside its body. Using threads is easier than that in Java, just implement the Runnable interface and then instantiate a new thread from that interface:
public class MyRunnable implements Runnable {
public void run() {
// do stuff
}
}
and then:
new Thread(new MyRunnable()).start();
cheers
you need to initialize the variables above the try catch block, or give them a value in catch or finally block
find updated code here
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
public class RssParse extends Thread {
Thread th=new Thread() {
public void run(){
System.out.println("1");
URL iotd=null;
try {
iotd = new URL("http://www.nasa.gov/rss/image_of_the_day.rss");
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("2");
BufferedReader in=null;
try {
in = new BufferedReader(new InputStreamReader(iotd.openStream()));
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.out.println("3");
XmlPullParserFactory factory=null;
try {
factory = XmlPullParserFactory.newInstance();
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
factory.setNamespaceAware(true);
System.out.println("4");
XmlPullParser xpp=null;
try {
xpp = factory.newPullParser();
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("5");
try {
xpp.setInput(in);
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("6");
int eventType=-1; // set to a default value of your choice
try {
eventType = xpp.getEventType();
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(eventType+"!!!!!!!!!!!!!!!!");
while(eventType!=XmlPullParser.END_DOCUMENT){
if(eventType==XmlPullParser.START_DOCUMENT){
System.out.println("start");
}
}
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}//method
};//thread
}//class

Android app using HttpURLConnection crashes at connect()

I have a following piece of code, basically copy-pasted from examples as I am new to Java and Android (not to programming):
URL vurl = new URL(voteurl); //vuteurl is a string containing a proper URL
HttpURLConnection hc;
hc=null;
hc = (HttpURLConnection)vurl.openConnection();
hc.setRequestMethod("GET");
hc.setDoOutput(true);
hc.setReadTimeout(10000);
hc.connect();
On the line "hc.connect();" the application crashes and Android informs me that it had been stopped.
Adding android.permission.INTERNET to the permisions used by the app did not help.
OK, turns out Android doesn't like network operations in the main thread.
Doing a request in a separate thread does the trick. Thanks guys for Your help!
URL vurl = null;
try {
vurl = new URL(voteurl);
} catch (MalformedURLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} //vuteurl is a string containing a proper URL
HttpURLConnection hc;
hc=null;
try {
hc = (HttpURLConnection)vurl.openConnection();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
hc.setRequestMethod("GET");
} catch (ProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
hc.setDoOutput(true);
hc.setReadTimeout(10000);
try {
hc.connect();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

Audio Player in J2ME

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")

Categories