We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi all, I'm trying to use the code posted here by yelele_brother to play sound in Android mode. (Code posted below for ease of reference.)
It is currently throwing an error that the getAssets(); method does not exist. I checked the Media Player reference here and can't see a getAssets() method. So since this method is no longer available, my question is: How do I make this code work? I've tried looking for ApWidgets but the google code page is shutdown, Cassette doesn't work for me and there appear to be few other solutions as far as I can find.
import android.media.*;
import android.content.res.*;
void setup()
{
size(10,10,P2D);
try {
MediaPlayer snd = new MediaPlayer();
AssetManager assets = this.getAssets();
AssetFileDescriptor fd = assets.openFd("test.mp3");
snd.setDataSource(fd.getFileDescriptor(), fd.getStartOffset(), fd.getLength());
snd.prepare();
snd.start();
}
catch (IllegalArgumentException e) {
e.printStackTrace();
}
catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
Answers
From this next post: https://forum.processing.org/two/discussion/12819/how-do-i-get-sound-to-play-on-the-phone
It seems you need to add these lines:
import android.content.Context; Context context;
And to add this change :
AssetFileDescriptor fd = context.getAssets().openFd("your.mp3");//which is in the data folder
Let us know if this fix your problem.
Kf
This stopped the errors but when I compile and it is sent to the phone, it tries to launch and says "SketchName has stopped working."
Phone tested was a Samsung Galaxy 7. Other programs work fine on the phone.
Neither the code above or below work, they both get this error.
I did try just having the snd.prepare(); & snd.play(); in the setup and this doesn't work either.
main:
Button class:
All references to
context.getAssets()...
needs to be replaced with
Kf
Error: the function "getActivity()" does not exist.
It worked for me. What Processing version are you using and OS? Also include
Kf
Processing 3.2.3
Windows 10 + anniversary update
already had import android.content.Context;
added import android.app.Activity; no change.
@SnailPropulsionLabs===
just changing some imports, some lines of code as you are in a fragment, using only 1 mp3 in order to simplify, and adding the onStop() , onPause() etc methods which are mandatory when using an instance of media player. Of course the .mp3 has to be in the data folder. The code below works, tested SonyXperiaZ3 and nexus tablet.
Still throwing "The function "getActivity()" does not exist" on line 19. What on earth is wrong with my setup if this works for both of you guys?
I might have asked you before, maybe it was another post. Could you install the ketai library and run one of their examples, like the one with the video camera or the sensor one? I just want to test if your Android SDK is setup properly. Have you run any other sketch in your device before?
These details could help in figuring it out what is happening.
Kf
yes i've run selfmade apps on this device previously.
installed ketai, ran the gettingstartedcamera example. It loaded a landscape screen with a small grey box, then gave the "appname has stopped working error", tried the accelerometer example, same error.
So i've got the sdk installed incorrectly?
You can copy and paste the error you get from the console in Processing. Just select the text and use the keys ctrl+c to copy and paste in the forum by pressing ctrl+v.
Do the examples come with the sketch's size defined in setup? If not add fullScreen();
By the way, a quicker test to test your android mode is to run something similar to the following lines, which do not depend on external libraries. The code below is not tested, so they might have minor typos.
Kf:
@snailpropulsionlabs=== it happens (processing bug??) that it fires an error with "this.getactivity()" but it compiles; have you tried?
Just for knowledge's sake, the console says that the sdk version is: Android SDK Tools Revision 25.2.3
@kfrajer: The code you gave compiled, black screen with white ellipse in top left corner, I suppose it should follow my touch inputs but I didn't have time to try (not my phone).
Also, the ketai camera example compiled and didn't crash this time, didn't display an image from the camera as I imagine it is supposed to, but not crashing is a step in the right direction.
@akenaton: Yes and it gave this error:
here's the first part of the xml file it refers to:
@SnailPropulsionLabs
I made a small mistake in my example code. Instead of keyPressed() use mousePressed(). The first function will not work on Android as you will need a keyboard.
A white circle in the corner, yes, that sound about right. I believe mousePressed events will be interpreted as touch events in Android. With the above modification, every time you touch the screen on your device, the white circle will move to that touch point. I can say from what you describe about this small simple program that Android mode is working in Processing :)>-
All my code testing has been done using API 23 and Processing 3.1.2
Another suggestion. You are trying to get the reference to your current activity to access your files. To work around your problem, can you try accessing your file using
dataPath("your_file_name.mp3");
instead of
Make sure your mp3 file is inside your data folder in your current sketch folder. This is a way to work around but I am not sure it will work.
Kf
yup, your program works @kfrajer.
Though the datapath("file_path.mp3") didn't solve the issue. I'm gonna lose my mind, it works for you guys but not here. Still just getting the Android error of "Sorry, appname has stopped working." Useful descriptive error messages from android.
As per the error in line 28 of the xml file above, what is javac.fork and could this be the source of my issues? What is it skipping on line 15?
@snailPropulsionLabs===
the error in .xml is when compiling so i dont understand: does it compiles or no??? you are speaking about crashing at run time, which means that the app was compiled and installed... btw: try to save as changing your app name which seems rather weird....
@SnailPropulsionLabs
This next post could be relevant. Try and see if it solves anything: https://forum.processing.org/two/discussion/19458/0255-the-method-oncreate-bundle-is-undefined-for-the-type-papplet#latest
I can't reproduce your error so I can help anymore. Maybe create a new post in the forum to see how to install Processing with a previous version of Android mode. Also I suggest running API23 instead of 21.
Doing a light browsing, calls to getActivity has changed before in Processing. I do not know much about what changes are implemented so my suggestions are just things I will do. I understand your frustration. Maybe you should text @codeanticode. I think he is in this forum or try stackoverflow.
Kf
@kfrajer, turns out using API23 got things to launch in the emulator.
@akenaton, your code from Nov 30 works for me now.
New question in this vein (that's why it's not in a new thread)
How can I make this play a different sound on each press of the button? It only plays the first sound it loads (even if the fd = context.getAssets().openFd("Ni-1.mp3"); is also present in the mousePressed method ) and if I try and make it load a random sound from the data folder It either doesn't go or just sits silent.
This is a previous code adapted to play a random filename. You need to have 5 sound files inside the data folder stored under the Sound folder. This is using the cassette library from Processing's library manager.
Kf
It works and plays the sounds i've put in there but it only plays 7 times and then just plays silence. Any variation I have tried has similar results, 7 times and silence thereafter.
In the console, it is outputting
debug: No event method
every time I click, this is during the times the sound plays and those when it doesn't.