I want to play an .ogg file, so i found this piece of code.
So it downloads .ogg file from the given url and plays it. I've tried it with external files like
ExamplePlayer player = new ExamplePlayer(new File("D:\\sound.ogg").toURI().toURL().toString())
and it works. But when i try this with internal files and get it using
ExamplePlayer.class.getClassLoader().getResource("sound.ogg")
it says that "There is a hole in the first packet data.". I think maybe it doesn't work because of JAR's compression or something.
So questions are: why it doesn't work? how could i fix it? If i can't fix it, is there any other way to play .ogg files using java? Thanks.
UPD: I found a lib, but the problem still the same, it cannot read from jar file. It
I'm looking at an application I wrote several years ago that uses ogg resources, and seeing that I first import the ogg file to an InputStream object using getResourceAsStream method.
Usually with wav files, importing via the URL is preferred. Unfortunately I don't recall why I did it this way--too much new tech under the bridge. First guess is that it's a requirement from the jcraft code, otherwise I would have used my preferred method.
Even if this works in the context of an IDE, IDK if it will also work after putting the code into a JAR. getResourceAsStream is often dicey in jars.
Before I invest more time, please let us know if switching to getResourceAsStream does the trick. Maybe the fix is that simple!
+++++++++++++
EDIT: Ugh. Looking at my code, I see where I copied a class from JCraft and edited it, supplying an InputStream via wrapper code. The edit was made in order to output float PCM data from the decoder rather than having the decoder play the sound. The float PCM is then saved in a custom object similar to AudioCue, and the app I wrote this for uses that for playback. All I can remember was that it took a lot tweaking to get this to work.
Related
I am currently building an app in android, and I am trying to record my audio over an existing audio file (without changing the length of the file), does anyone know how is it possible to do that? Everything I tried just puts the audio files one after another in one file.
A. You should use GGmpeg for Android, for example. Here you have some questions like yours.
question with code
more detailed tutorial
Google Group
another question
B. Another approach, although limited, is Android SoundPool. Check documentation here.
The sound pool requires you to manage multiple files rather than doing
mixdowns and playback of a single file. It would be cool if the
soundpool let you grap the output as a IOStream but, alas, the SDK
doesn't seem to allow it. Further, it would be nice if the
AudioRecorder allowed you to grab a stream from the music layer...but
that doesn't look possible either. List of audio inputs:
http://developer.android.com/reference/android/media/MediaRecorder.AudioSource.html
SOURCE: https://stackoverflow.com/a/2254086/9415337
C. Others suggest also doing it in a backend server.
D. This might also help you.
Every few days, I get a SocketException, too many files open. I tracked the issue to a temporary pdf file that is being used during a certain process. The process passes a name of a temporary file that the library creates. At some point, the library opens an input stream but doesn't close it. Given that my code only has the name of the file, is there any way for me to close the stream?
Details:
Java Web App running in Tomcat6
The best approach is to request a version of this library with this bug fixed.
If this is not possible, get the sources, fix the bug yourself.
If you can't (only a binary jar file), try a tool like jd-gui, decompile the faulty class, fix, recompile that class and replace the .class in the jar.
If it still does not work use ASM and add a close statement at the right place. THIS SOLUTION SHOULD BE AVOIDED. It's complex if you do not master this technology.
I have the following code in a static method:
clips.open(AudioSystem.getAudioInputStream(Sound.class.getResourceAsStream("folder/sound.wav")));
Also, folder is in the same directory as Sound.java. When I run the program in Eclipse, the sound is played. However, when I export the file to a JAR file, the sound no longer plays.
If I change getResourceAsStream() to getResource(), both Eclipse and the JAR file play the sound. Why does this happen? I have read around and many people suggest that getResourceAsStream() simply doesn't work in JAR files. Is this the case, and if so, why not?
I suspect that the issue is that Class.getResourceAsStream(String) returns an InputStream, and Class.getResource(String) returns a URL. Thus, in the first case, we are in effect using
AudioSystem.getAudioInputStream(InputStream inputStream)
and in the second case, using
AudioSystem.getAudioInputStream(URL url).
Why does this matter? I think the main issue is that when the InputStream is the parameter, mark/reset tests are done on your audio file and it is failing. When the URL is used as a parameter, this test does not occur. You might check out the API for AudioSystem.getAudioInputStream to confirm what I am reporting here.
Do you get an error message when the getResourceAsStream fails? Does it mention the mark/reset test error by chance? If you are getting a different error message then this might not be the correct answer and something else is going on.
I am having trouble finding out what these .w files are and how to using them with my Java code: see Assignment1.zip at http://see.stanford.edu/see/materials/icspmcs106a/assignments.aspx
I would post a screenshot of my files, but I don't have enough "reputation points" to post an image on this site--I usually just read answers from others b/c I'm just learning. I can't open these .w files or else I would paste some of their contents in here.
For the past year, I've worked a good amount with Python and C++, but I decided to learn some Java from these free Stanford online lectures: http://see.stanford.edu/see/lecturelist.aspx?coll=824a47e1-135f-4508-a5aa-866adcae1111.
I've looked all over and simply cannot find out what .w files are, and I haven't been able to open them. I realize they're clearly worlds for the graphical robot to move around in, but I can't get the .java & .class files to function with these .w (or "world") files.
I would be happy to post some of my code in here as well, if you want. And I could post an image of the files & folders once I get a good enough reputation.
Could someone please help me?
You seem to be missing the documentation for karel.jar. There is already code for reading and working with these files, in sandford.karel.KarelWorld there is a load(File) method. But you will need to be working with the rest of the classes in the jar file, too, so you'll need to look for documentation (maybe given in the lecture videos?) or you'll have a very frustrating time.
I have a text file i want to include in my Android application, it is not a string file it is a standard text file. It contains data that defines the characteristics of a "map" that is drawn on a board. The file is not an XML file so i am unsure where i should put it or if this isn't good file structure for android? Are you suppose to do this? If you are then under what directory are you suppose to put them? How then are you suppose to access the file? I know how to use FileInputStreams and FileOutputStreams i just need to know how to access the file. All relevant answers are welcome and appreciated!
Use assets or raw folder in your android folders structure to keep that file. For more info read this
You have to put your file in the assets folder as Waqas said.
Now to access it you do it like that.
I give you an example using BufferedReader
BufferedReader reader = new BufferedReader(
new InputStreamReader(getAssets().open("YourTextFile.txt")));
Be careful. In my case, I don't know why for now, I cannot read text files bigger than ~1MB and I had to split them in multiple small files. It seems other had the same problem of file size but I didn't find any information about that on Android developer site. If any one knows more about this ....
FOLLOW UP
My problem with the 1MB was due to a know bug/limitation of earlier versions of Android. Since using recent versions of Android, that problem is not present anymore.
I would just like to add to the accepted answer (I don't have enough reputation to comment unfortunately.) The link there to the tutorial that explains how to set up the res/raw method or the assets method is mostly good, but there's actually a MUCH easier way. Look at the function described there called LoadFile. That function is rather verbose. Lets say all you need is an InputStream variable so that you can read and write to a file. Then delete everything after line 77! Also you don't need the resource id at all! You can use this function:
//get the file as a stream
iS = resources.openRawResource(R.raw.mygpslog)
Now all you have to do is return iS and you will have your much desired file handle.
For reference, the tutorial is right here -> http://www.41post.com/3985/programming/android-loading-files-from-the-assets-and-raw-folders