I've tried to add music to an application I've made. First I tried with .wav files though they became so huge that the application became too large to upload anywhere.
So I changed the files to .mp3, tried JMF and JLayer though both of them won't work on runnable jars (even if they work fine when I haven't exported them).
So anyone got any tips on how to play compressed music with a runnable jar?
Here's the code for JLayer, when exported it stops working at f = new File(u.toURI()) without throwing any exceptions...
try {
URL u = cl.getResource("New Beginnings.mp3");
f = new File(u.toURI());
} catch (URISyntaxException e1) {
e1.printStackTrace();
}
try {
FileInputStream fis = new FileInputStream(f);
p = new Player(fis);
p.play();
} catch (Exception e) {
System.out.println(e);
}
Edit: Fixed with changing the above code to:
try {
InputStream fis = ClassLoader.getSystemClassLoader().getResourceAsStream(temp+".mp3");
p = new Player(fis);
p.play();
} catch (Exception e) {
System.out.println(e);
}
Where did you place the sound file exactly?
You should create a new package inside your project and place the resources there, then read the file by sending a complete path:
ex. create a new package called sounds, then:
InputStream fis = ClassLoader.getSystemClassLoader().getResourceAsStream("/sounds/"+temp+".mp3");
p = new Player(fis);
p.play();
To be honest, I didn't try it with sounds, but this problem happened to me when I used images. I placed them in a package, and everything worked fine..
Related
I have been experimenting with writing to text files for output instead of System.out.println(). When I try this, though, nothing seems to be written. What is the issue with my code?
try{
List<String> lines = Arrays.asList("Data Goes Here");
Path file = Paths.get("output.txt");
Files.write(file, lines, Charset.forName("UTF-8"));
}
catch (IOException ex) {
System.out.println("Frick, something broke. Sorry folks, go home.");
}
I just did a small change to your code by passing the path as the resources directory located in the root of my project. I was able to write to the file successfully.
Here is the updated code:
try {
List<String> lines = Arrays.asList("Data Goes Here");
Path file = Paths.get("./resources/test.txt");
Files.write(file, lines, Charset.forName("UTF-8"));
}
catch (IOException ex) {
ex.printStackTrace();
System.out.println("Frick, something broke. Sorry folks, go home.");
}
I have been trying to write to a file in Android. It is not working and it doesn't even create a file. It always executes the catch block. Here is the part of my program.
private void write(){
try {
FileWriter fileWriter = new FileWriter("C:\\Users\\Administrator\\AndroidStudioProjects\\SunCalculator\\app\\src\\main\\res\\raw\\au_locations.txt");
Log.e("Data","path detected");
BufferedWriter bfWriter = new BufferedWriter(fileWriter);
bfWriter.write("Text Data");
bfWriter.close();
Log.e("Data","worked");
} catch (IOException e) {
e.printStackTrace();
Log.e("Data","not worked");
}
}
I also tried to create a File object and passing it to the FileWriter constructor. None of these worked. I am using Android Studio 2.3.3
You are trying to write a file in location C:\\Users.... which is the directory structure of Windows OS.
But Android is built upon Linux OS. To get the user writeable directory in android, you should use Environment.getExternalStorageDirectory() as below:
File file = new File(Environment.getExternalStorageDirectory(), filename);
PrintWriter p;
try {
p = new PrintWriter(new FileWriter("xp.mdb"));
} catch (FileNotFoundException e) {
return;
} catch (IOException e) {
return;
}
p.println("mytext");
p.close();
I'm new to Android programming and don't quite understand how writing to files work. I want to create a new file and write some text to it. The above code works on Windows but on Android I just keep getting FileNotFoundException.
How can I make it create the file if it doesn't exist?
You need to put the file in internal storage, external storage, or (on Android 4.4+) removable storage. Passing a bare filename does not work on Android.
How can I convert an inputStream to a URL? Here is my code:
InputStream song1 = this.getClass().getClassLoader().getResourceAsStream("/songs/BrokenAngel.mp3");
URL song1Url = song1.toUrl(); //<- pseudo code
MediaLocator ml = new MediaLocator(song1Url);
Player p;
try {
p = Manager.createPlayer(ml);
p.start();
} catch (NoPlayerException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
I am not sure you really want to do this. If you need URL for your specific task just do the following:
URL url = this.getClass().getClassLoader().getResource("/songs/BrokenAngel.mp3");
If however you retrieve input stream in one part of you code, then pass it to another module and there want to find what was the source URL for this input stream it is "almost" impossible. The problem is that you get BufferedInputStream that wraps FileInputStream that does not store the information about it source. You cannot retrieve it even using reflection. If you really want to do this you can do the following.
implement you own UrlInputStream extends InputStream the gets into constructor URL, stores it in class varible, creates input stream by invocation of url.openStream() and wraps it.
Now you can use your stream as usual input stream until you have to retrieve the URL. At this point you have to cast it to your UrlInputStream and call getUrl() method that you will implement.
Note that this approach requires the mp3 to be within your application's sub-directory called songs. You can also use relative pathing for the /songs/BrokenAngel.mp3 part (../../ or something like that. But it takes your applications directory as base!
File appDir = new File(System.getProperty("user.dir"));
URI uri = new URI(appDir.toURI()+"/songs/BrokenAngel.mp3");
// just to check if the file exists
File file = new File(uri);
System.out.println(file.exists())
URL song1Url = uri.toURL();
I think what you want is ClassLoader.findResource(String)
That should return a properly formatted jar:// URL. I haven't tested it myself though, so beware
Try this
URI uri = new URI("/songs/BrokenAngel.mp3");
URL song1Url = uri.toURL();
MediaLocator ml = new MediaLocator(song1Url);
Player p;
try {
p = Manager.createPlayer(ml);
p.start();
} catch (NoPlayerException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Try this
InputStream input = new URL("http://www.somewebsite.com/a.txt").openStream();
I have a program which writes out some data- I am using this logic
FileOutputStream outpre = new FileOutputStream(getfile());
FileChannel ch = outpre.getChannel();
ch.position(startwr);
ch.write(ByteBuffer.wrap(dsave));
outpre.close();
It writes out the correct data to the correct location, the only problem is that everything before the starting location to write (startwr) gets replaced with 00's, and the file is also changed making the point at which the writing was done, the end of the file.
How can I write data to the file without corrupting the previous data and changing the file size?
You need to instruct the stream to either append or overwrite the contents of the file...
Take a look at FileOutpuStream(File, boolean) for more details
Updated
After some mucking around, I found that the only solution I could get close to working was...
RandomAccessFile raf = null;
try {
raf = new RandomAccessFile(new File("C:/Test.txt"), "rw");
raf.seek(3);
raf.writeBytes("BB");
} catch (IOException exp) {
exp.printStackTrace();
} finally {
try {
raf.close();
} catch (Exception e) {
}
}
You can fix it this way
FileChannel fc = FileChannel.open(getFile().toPath(), StandardOpenOption.APPEND);