Audio Player in J2ME - java

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

Related

How do i open all the links for a given URL using java?

Okay So I have the code to open up an URL using firebox. I'm just trying to find out how to open up all the links on that page, it can be done one at a time or all at once, but I just want to know. I can't seem to find this anywhere.
String url = "http://www.example.com";
if(Desktop.isDesktopSupported()){
Desktop desktop = Desktop.getDesktop();
try {
desktop.browse(new URI(url));
} catch (IOException | URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
Runtime runtime = Runtime.getRuntime();
try {
runtime.exec("xdg-open " + url);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

Can't list files when accessing FTP folder from Heroku app

I'm have created a java app that reads files from a FTP server.I have tested this app locally(with success) and now i have deployed this app to heroku.
This is a piece a my code :
public void CSVListing() {
String[] fnames = {"1","2","3","4"};
try {
try {
client.connect(host);
} catch (SocketException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException ex) {
// TODO Auto-generated catch block
ex.printStackTrace();
}
//----------------------------------------
boolean login = false;
try {
login = client.login(user, pass);
} catch (IOException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
if (login) {
System.out.println("Login success...");
//boolean logout = false;
System.out.println("ready to work");
}
else {
System.out.println("Login fail...");
}
//----------------------------------------
System.out.println(client.printWorkingDirectory());
fnames = client.listNames();
System.out.println("FNAMES ARE" + fnames);
for(String s: fnames){
System.out.println(s);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
......................................................
I have locally executed this code and it gives me a list of the files, but when I execute this on my heroku app online >>fnames = client.listNames();<< returns null according to my logs. I'm accessing the same FTP host in both executions (locally and online).
Can somebody help me and tell me where i'm wrong?
Did you check your permissions? Awhile ago I pass through something similar, the problem was that the user connecting to the FTP server didn't have permission to download files.

Problems with BigClip

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.

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

Reflecting methods to clear Android app cache

I am trying to clear the app cache of other android apps besides my own. To do this, I am using reflection on the PackageManager class. However, whenever I initialize the method before I invoke it, it always ends up being null.
private void initiateClearUserData() {
// Invoke uninstall or clear user data based on sysPackage
String thePackageName;
PackageManager pm = speedy.this.getPackageManager();
List<ApplicationInfo> installedApps = pm.getInstalledApplications(0);
ApplicationInfo ai;// = installedApps.get(0);
ActivityManager.RunningAppProcessInfo process;
for(int x=0; x<4; x++){
ai = installedApps.get(x);
Here is where my problem is:
thePackageName = ai.packageName.toString();// mAppEntry.info.packageName;
Method deleteApplicationCacheFiles = null;
mClearCacheObserver = new ClearCacheObserver();
try {
deleteApplicationCacheFiles = pm.getClass().getMethod(
"deleteApplicationCacheFiles", String.class, PackageManager.class);
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(deleteApplicationCacheFiles!= null){
try {
deleteApplicationCacheFiles.invoke(thePackageName, mClearCacheObserver);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
Toast.makeText(speedy.this, "Hell naw",
Toast.LENGTH_SHORT).show();
}
}
}
Because Method deleteApplicationCacheFiles is null, my toast message shows up. Any suggestions?
Take a look at the docs for Security on Android: http://developer.android.com/guide/topics/security/security.html
A central design point of the Android security architecture is that no application, by default, has permission to perform any operations that would adversely impact other applications, the operating system, or the user. This includes reading or writing the user's private data (such as contacts or e-mails), reading or writing another application's files, performing network access, keeping the device awake, etc.
It sounds like the system will block you from doing this (through reflection too).

Categories