I'm using SoundPool for my android app. I load about 75 one- to three-second sounds into the pool in my main activity and then reference them from other activities using the Sound method which looks like this:
public void Sound(int s){
AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
float volume = (float) audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
MainActivity.spool.play(s, volume, volume, 1, 0, 1f);
};
s is the integer I define in the MainActivity class, for example:
static int sound_e;
and then pass into the Sound method like this:
Sound(sound_e);
I would like to be able to define a string like this:
String letter_sound = "MainActivity.sound_" + currentLetter;
//Example value of this string would be MainActivity.sound_a
And then pass that string into Sound as an integer. This is to avoid 26 if statements, which I did for the numbers like this:
if (randomNum == 1) {Log.v(TAG, "Sound playing for: " + randomNum); Sound(MainActivity.sound_1);}
else if (randomNum == 2) {Log.v(TAG, "Sound playing for: " + randomNum); Sound(MainActivity.sound_2);}
else if (randomNum == 3) {Log.v(TAG, "Sound playing for: " + randomNum); Sound(MainActivity.sound_3);}
else if (randomNum == 4) {Log.v(TAG, "Sound playing for: " + randomNum); Sound(MainActivity.sound_4);}
else if (randomNum == 5) {Log.v(TAG, "Sound playing for: " + randomNum); Sound(MainActivity.sound_5);}
else if (randomNum == 6) {Log.v(TAG, "Sound playing for: " + randomNum); Sound(MainActivity.sound_6);}
else if (randomNum == 7) {Log.v(TAG, "Sound playing for: " + randomNum); Sound(MainActivity.sound_7);}
else if (randomNum == 8) {Log.v(TAG, "Sound playing for: " + randomNum); Sound(MainActivity.sound_8);}
else if (randomNum == 9) {Log.v(TAG, "Sound playing for: " + randomNum); Sound(MainActivity.sound_9);}
else if (randomNum == 10) {Log.v(TAG, "Sound playing for: " + randomNum); Sound(MainActivity.sound_10);}
else Log.v(TAG, "None of the ifs are true. randomNum: " + randomNum);
I didn't see in the android documentation any way to pass string values into the SoundPool play, just integers. Thanks for helping me figure out what I'm missing!
I'm using SoundPool for my android app. I load about 75 one- to three-second sounds into the pool in my main activity and then reference them from other activities using the Sound method...
Firstly...DO NOT DO THIS. The Android Activity class is not just a normal Java class and the proper (and safe) Android programming approach is that an Activity should be self-contained. Other Activities or app components should not have access to public methods or data fields.
If you wan't to provide a common class to be used by multiple app components then simply create a 'helper' POJO class. If it needs to use a Context (as SoundPool does for example), you can simply pass the Activity context into methods as this. Be careful about holding a reference to the Activity Context however as it can cause memory leaks.
Secondly to give an easy answer to your problem...
The load(...) methods of SoundPool return a soundID which is what you use to call the play(...) method. If all you want to do is play a random sound then simply create an array of some sort and use the random number to get the soundID from the array by index. Simple - just one line of code to access the relevant soundID.
Related
I am making an app using ML KIT. I am currently implementing counting while doing squats. The problem is that it counts when it goes down to a certain angle, but because it is a real-time tracking, it counts multiple times rather than once. In other words, practically when I do one squat, the counting is much more than that.
So I tried solving the problem using a timer handler. But I failed. Here is my code What should I do?
//going down
if((rightKneeAngle<=90 && leftKneeAngle<=90) || (rightHipAngle<=135 && leftHipAngle<=135)){
//Timer setting
mTimer.schedule(new CustomTimer(), 2000);
Log.d("PoseGraphic.class", "goingdown"+"rightKneeAngle : " + rightKneeAngle+ " leftKneeAngle : " + leftKneeAngle);
Log.d("PoseGraphic.class","leftHip"+leftHipAngle+"RighHip"+rightHipAngle);
Log.d("PoseGraphic.class", "cnt: " + cnt);
//going up
if((rightKneeAngle>170 && leftKneeAngle>90)|| (rightHipAngle>135 && leftHipAngle>135)){
Log.d("PoseGraphic.class", "going up"+"rightKneeAngle : " + rightKneeAngle+ " leftKneeAngle : " + leftKneeAngle);
Log.d("PoseGraphic.class","leftHip"+leftHipAngle+"RightHip"+rightHipAngle);
if(cnt==12) {
canvas.drawText("complete!", x, y, textPaint);
//ExerciseCount.cnt = 0;
cnt=0;
}
}
}
// TIMER handler class
class CustomTimer extends TimerTask
#Override
public void run() {
cnt++;
}
I cannot fully understand how you do it without seeing more code.
An suggestion would be:
Instead of +1 to the count when the pose is in squat_down state, record the entering of that state with a boolean and +1 to the count only once the user exit squat_down and enter squat_up state. Then reset the state boolean.
By doing this, you will only +1 when user finish an entire cycle from squat_down to up.
Actually, ML Kit just launched an example for squat and pushup classification and rep-counting. Here is the app you can try. If you want to classify some other poses, here is some guide and a Colab.
I'm using onPartialResult method to look if the hypotesis is one of the keyword I'm interested in and it works well.
Here's my code:
#Override
public void onPartialResult(Hypothesis hypothesis) {
Log.d(TAG, "onPartialResult");
if (hypothesis == null) {
return;
}
String text = hypothesis.getHypstr();
String wordWithProb = "";
String mostProbableWord = "";
int probability = -10000;
if (text.contains("|")) {
for (Segment seg : recognizer.getDecoder().seg()) {
wordWithProb += "|" + seg.getWord() + " " + seg.getProb() + "|";
if (seg.getProb() > probability)
mostProbableWord = seg.getWord().trim();
}
}
else
mostProbableWord = text.trim();
Log.i(TAG, "onPartialResults: " + mostProbableWord);
String recognizedCommand = "Please repeat";
if (mostProbableWord.equals("one")) {
//do something...
} else if (mostProbableWord.equals("two")) {
//do something...
} else if (mostProbableWord.equals("three")) {
//do something...
}
//text to speech
speak(recognizedCommand);
startListening(KWS_SEARCH);
}
Now I would like to handle the case where a user say something and it is not recognized as a keyword; in this case the hypotesis in the onPartialResult method is always null: is this expected? I was expecting a not null hypotesis here...
Considering that onPartialResult method is automatically called by pocketsphinx consinuously (also when there isn't any sound in the air) I cannot use the null hypothesis as my driving condition.
Moreover there is a text to speech after every recognition and so the recognition listener restart has to be handled carefully: recognizer must not be listening while text to speech is ongoing...
I tried some solution with onEndOfSpeech but none was good until now...
Any idea?
in this case the hypotesis in the onPartialResult method is always null: is this expected?
Yes
Moreover there is a text to speech after every recognition and so the recognition listener restart has to be handled carefully: recognizer must not be listening while text to speech is ongoing
Correct
I'm in the middle of developping a map app that enables user to send duration data that it takes him to get to another place, to the database (MySQL) , for this reason I tried to extract the double value of duration directely from url as it's showing in the method below :
#Override
public void setDouble(String result) {
String res[]=result.split(",");
Double min=Double.parseDouble(res[0])/60;
int dist=Integer.parseInt(res[1])/1000;
Duree.setText("Duration= " + (int) (min / 60) + " hr " + (int) (min % 60) + " mins");
Distance.setText("Distance= " + dist + " kilometers");
}
In this method it worked, but when I tried to do it like this :
url = "https://maps.googleapis.com/maps/api/distancematrix/json?origins="+latt+","+lngg+"&destinations="+lt+","+lg+"&mode=driving&language=fr-FR&avoid=tolls&key=API_KEY";
String res[]=url.split(",");
Double duration=Double.parseDouble(res[0])/60;
It showed me that it's not a valid double value, knowing that I need to send this value when I click a button showed on a popup window after marker click (So the problem of inner class is posed).
Can you help me know what is the right way to do it ,if that is possible !
Don't expect your first result-field contains the duration but make your parsing a little more intelligent. Use, by example, the org.json library (introduction on json
Following code snippet should help a little:
LOGGER.debug("---google duration in seconds = {}", jsonElement.opt("duration") == null ? 0 : jsonElement.getJSONObject("duration").get("value"));
transportInfo.setDuration(jsonElement.opt("duration") == null ? 0 : (Integer) jsonElement.getJSONObject("duration").get("value"));
And, btw, I would recommend removing your API key ASAP from your post .... Be aware this key could be used by other people.
I'm trying to show the results of a timer implemented in following code in my Android OpenCV application:
case ImageManipulationsActivity.VIEW_MODE_MEDIAN:
capture.retrieve(mRgba, Highgui.CV_CAP_ANDROID_COLOR_FRAME_RGBA);
if (mRgbaInnerWindow == null|| mGrayInnerWindow == null)
CreateAuxiliaryMats();
long start = System.currentTimeMillis();
Imgproc.medianBlur(mRgbaInnerWindow, mIntermediateMat, 25);
Core.convertScaleAbs(mIntermediateMat, mIntermediateMat, 1./10, 0);
Core.convertScaleAbs(mIntermediateMat, mRgbaInnerWindow, 10, 0);
long end = System.currentTimeMillis();
long elapse = end - start;
Toast.makeText(ImageManipulationsView.context.getActivity(), "" + elapse + "ms is used
to process", Toast.LENGTH_LONG).show();
I'm getting an error "Context cannot be resolved or is not a field" What am I doing wrong? Is my timer correct? How can I display the results of it?
#Sahil Mahajan Mj
Now I have an error in line:
Toast.makeText(ImageManipulationsView.this, "" + elapse + "ms is used to",
Toast.LENGTH_LONG).show();
from the code I've pasted in the beginning. The error is "The method makeText in the type Toast is not applicable for the arguments"
If it helps, my app is a modification of Image-manipulations sample from OpenCV4Android package
#Blundell
I've changed the context variable to public and the whole app crashes when I choose medianBlur from my app menu.
If you are talking about this,
Toast.makeText(ImageManipulationsView.context.getActivity(), "" + elapse + "ms is used
to process", Toast.LENGTH_LONG).show();
You just need to the Activity's Context like this,
ImageManipulationsView.this
If there is some other error, you need to post the code, where actually the error is. Only then I can edit my answer.
In Android design standards your code is a bit wrong.
But it looks like you can't 'see' the context variable from this class.
In your ImageManipulationsView class change the context variable to public.
public Context context;
I am building an android game where objects randomly come in from either side of the screen towards a sprite in the middle of the screen. When one passes the tripwire on the central sprite it is dealt with by my collision detection which sets the enemy objects co-ordinates to somewhere off the screen, thats desired but as this happens in my updatePhysics() method when we loop back to the doDraw() method the canvas has redrawn to an empty state (ie. without any enemy objects on it). Any objects that were on screen and not colliding with the sprite are erased too and new ones regenerated. Why is it removing all the enemy objects rather than just updating the position of the ones left in play at that point???
I will include my code to make this simpler to understand. This is the collision detection part of my updatePhysics():
// COLLISION DETECTION
int n = 0,haveHidden = 0; //haveHidden is number of objects that are hidden in this current pass of CD
while(n < mCurrentDistractionsOnScreen){
switch(DistractionsArray[n].dGeneralDirection){
case LEFT_ORIGIN:
// If the X co-ordinate is past the edge of the performer sprite
if((DistractionsArray[n].ObjmX2 > LEFT_TRIPWIRE) && (DistractionsArray[n].ObjmX2 < RIGHT_TRIPWIRE))
{
mConcentration -= DistractionsArray[n].dDamageToInflict;
Log.w(this.getClass().getName(), "Object: " + n + "LEFT Damage: " + DistractionsArray[n].dDamageToInflict + " leaves " + mConcentration);
DistractionsArray[n].hideDistraction();
haveHidden++;
}
break;
case RIGHT_ORIGIN:
// If the X co-ordinate is past the edge of the performer sprite
if((DistractionsArray[n].ObjmX > LEFT_TRIPWIRE) && (DistractionsArray[n].ObjmX < RIGHT_TRIPWIRE))
{
mConcentration -= DistractionsArray[n].dDamageToInflict;
Log.w(this.getClass().getName(), "Object: " + n + "RIGHT Damage: " + DistractionsArray[n].dDamageToInflict + " leaves " + mConcentration);
DistractionsArray[n].hideDistraction();
haveHidden++;
}
break;
}
n++;
mCurrentDistractionsOnScreen -= haveHidden;
}
This is the hideDistraction() method that is a method defined in the DistractionObjects inner class in this thread (DistractionsArray consists of these objects):
public void hideDistraction(){
// Set attributes to default
this.dName = "Default";
this.dVelocity = 0;
this.dMaxPoints = 10;
this.dDamageToInflict = 0;
this.dOrigin = 100;
this.dGestureRequired = "";
// Can be reused again
this.isUseable = true;
Log.w(getClass().getName(), "HIDING FROM: " + this.ObjmX + " " + this.ObjmY + " " + this.ObjmX2 + " " + this.ObjmY2 + " ");
// Position it off-screen
this.ObjmX = -150;
this.ObjmX2 = -150 + this.dDistractionObjectSprite1.getIntrinsicWidth();
this.ObjmY = -150;
this.ObjmY2 = -150 + this.dDistractionObjectSprite1.getIntrinsicHeight();
Log.w(getClass().getName(), "HIDING TO: " + this.ObjmX + " " + this.ObjmY + " " + this.ObjmX2 + " " + this.ObjmY2 + " ");
}
And the doDraw method starts by canvas.save() and at the end does a canvas.restore. To render my objects at their new position it does this:
// For each object in play, draw its new co-ordinates on the canvas
int n = 0;
while(n < mCurrentDistractionsOnScreen){
DistractionsArray[n].dDistractionObjectSprite1.setBounds((int)DistractionsArray[n].ObjmX,
(int)DistractionsArray[n].ObjmY,
(int)DistractionsArray[n].ObjmX2,
(int)DistractionsArray[n].ObjmY2);
DistractionsArray[n].dDistractionObjectSprite1.draw(canvas);
n++;
}
I am literally devoid of any ideas now after trying so many different fixes, even attempted to serialize the position of those other enemy objects on screen in an integer array that is rebuilt in the physics method but nothing!!
To summarise, I need a way of hiding the enemy object off-screen like in hideDistraction() when it passes the tripwire, but I want all the other objects to still continue on course as they are without being moved off screen and re-rendered from scratch.
If you need more details please ask....and if you can, pleaseeee help!
Many thanks
Greenhouse,
You are controlling your loop using the mCurrentDistractionsOnScreen, but you are also resetting the mCurrentDistractionsOnScreen inside of the loop. You likely don't want to be doing this. The exact reason it is removing everything is likely very difficult to pin down (i'm not surprised you couldn't figure it out!). Try re-factoring that main while loop to something like this:
for (Distraction distraction: DistractionsArray) {
switch(distraction.dGeneralDirection) {
case LEFT_ORIGIN:
// Your code goes here ...
break;
case RIGHT_ORIGIN:
// Your code goes here ...
break;
}
}
When pasting your code, remove all references to DistractionsArray[n] and replace them with distraction
EDIT:
If this helps you, then please consider 1) accepting more answers on your questions and 2) Not putting stuff like 'please help' in your titles. It's a site geared towards help, we know what you are looking for ;)
Your hideDistraction() and doDraw code is likely fine.