Android Question Volume of Active AudioStream?

persianpowerman1

Active Member
Licensed User
Longtime User
hey guys... im studying AudioStreams ... request some help...
For Eg...

GetVolume (Channel As Int) As Int
Returns the volume of the specified channel.
Channel - One of the VOLUME constants.

.... but what if i want the volume & name of which ever Audio Channel... is presently active(or most relevant)..? then what do i do?
like eg.. in the SDK all i could find is...

adjustVolume(int direction, int flags)
Adjusts the volume of the most relevant stream.

But this adjusts... i only want to know "at this moment" which is the most relevant Audio Channel(presently active audio stream) and what is its volume?

any ideas?... thanx in advance

YO!han
 

stevel05

Expert
Licensed User
Longtime User
Try this:

B4X:
Sub GetCurrentAudioMode As Int
    Dim R As Reflector
    Dim AudioManager As JavaObject
    Dim Context As JavaObject = R.GetContext
    AudioManager = Context.RunMethod("getSystemService",Array As Object(Context.GetField("AUDIO_SERVICE")))
    Return AudioManager.RunMethod("getMode",Null)
 
End Sub

The documentation is here: http://developer.android.com/reference/android/media/AudioManager.html#getMode()

You'd need to match the returned value to the stream numbers as documented here http://developer.android.com/reference/android/media/AudioManager.html#STREAM_ALARM

It may not be exactly what you're after
 
Upvote 0

persianpowerman1

Active Member
Licensed User
Longtime User
hey steve... nice one man!... but Erel had already given me this code... "get mode"...

B4X:
Sub GetMode As Int
  Dim r As Reflector
  r.Target = r.GetContext
  r.Target = r.RunMethod2("getSystemService", "audio", "java.lang.String")
  Return r.RunMethod("getMode")
End Sub

what i want isnt exactly working with that... but i've thought of a work around... will share if i get success


hey.. one more thing... since we on this topic...
i really cant understand how to use this Reflection/target/run method thingy... it seems so useful but can seem to get my head round it..

can u tell me what to read up.. or... examples etc... so i can understand the logic behind this?

thanx man,
YO!han
 
Upvote 0

persianpowerman1

Active Member
Licensed User
Longtime User
Bud

dy.. as i have begun reading it... it seems so lovely as a perfect tutorial.. wanted to thank you for the effort you took in explaining... am letting it sink in slowly... thanx again.. going to keep learning it slowly and will then try to make something out of it so it sticks

For eg... was also reading the other SDK post you linked me to on top
found this
public void adjustVolume (int direction, int flags)
Adjusts the volume of the most relevant stream. For example, if a call is active, it will have the highest priority regardless of if the in-call screen is showing. Another example, if music is playing in the background and a call is not active, the music stream will be adjusted.
This method should only be used by applications that replace the platform-wide management of audio settings or the main telephony application.


DirectionThe direction to adjust the volume. One of ADJUST_LOWER, ADJUST_RAISE, or ADJUST_SAME.

Flags
One or more flags.

Now... for starters how could i use something like this
would it be
B4X:
Dim r As Reflector
  r.Target = r.GetContext
  r.Target = r.RunMethod2("getSystemService", "audio", "java.lang.String")
  Return r.RunMethod("adjustVolume")

...OR... something like this???

B4X:
Dim r As Reflector
  r.Target = r.GetContext
  r.Target = r.RunMethod2("getSystemService", "audio", "java.lang.String")
  Return r.RunMethod3("adjustVolume", 1, type1? , 0,type2?)


you see what i mean is im still not sure what to do with Type1, type2?...

also the ("getSystemService", "audio", "java.lang.String") part i got cz thats what Erel showed me...
but on my own.. it'd be a bouncer!
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Look here.
adjustVolume need two parameters.

snap0029.png


I´m REALLY NOT familar with reflection. But after looking a few minutes in the docoumentation i THINK it could be something like

raise
B4X:
Return r.RunMethod3("adjustVolume","1", "java.lang.Integer","0", "java.lang.Integer")
lower
B4X:
Return r.RunMethod3("adjustVolume","-1", "java.lang.Integer","0", "java.lang.Integer")

But i dont know what the flags must set to. the "0" in my code.

NOTE: I´m REALLY NOT familar with reflection! I dont know...

Just wanted to try to help
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
That's where JavaObject is a little simpler to understand. They both do the same thing at the end of the day. And with JavaObject, you don't specify the variable type, you just have to make sure the variable is the right type. And you can use meaningful variable names as in my example.

Using JavaObject it would be:

B4X:
    Dim R As Reflector
    Dim AudioManager As JavaObject
    Dim Context As JavaObject = R.GetContext
    AudioManager = Context.RunMethod("getSystemService",Array As Object(Context.GetField("AUDIO_SERVICE")))
    Return AudioManager.RunMethod("adjustVolume",Array As Object(1,0))

In case there is any confusion Context.GetField("AUDIO_SERVICE") just get's the Android Context class constant value for "AUDIO_SERVICE" which is "audio". Which you could use directly as

B4X:
AudioManager = Context.RunMethod("getSystemService",Array As Object("audio"))

Using JavaObject, parameters are passed in an array of type Object so you don't have to worry about finding the java qualified name for Int or Float or whatever. The values you pass have to be of the correct type, so if you wanted to pass a variable to the above, they would have to be of type Int.

Neither is a 'Better' way to do it, just whichever you can remember easiest.
 
Upvote 0

persianpowerman1

Active Member
Licensed User
Longtime User
thanx a million guys... im trying both... how ever getting one small error by the IDE on the last line?
says...
"Return type(in sub signature) should be set explicitly(warning #3)"


are you sure RETURN is suppose to be there?
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Yes Return needs to be there, make sure your Sub signature specifies a Return type e.g.:

B4X:
Sub GetMode As Int

Where the 'As Int' is the Return type.
 
Upvote 0

persianpowerman1

Active Member
Licensed User
Longtime User
yeah... still giving up some error :(
ok... i think im tired.. calling it a night... but heres the file...
I'll explain you what im trying....

When my phone rings... i want it to decline in volume by -1... hence i have caught the present/relevant audio stream for that moment... similarly for the Alarm... similarly for media...
So my app stores all the AudioStreams original volumes... then when a value of (-1) is fired... i would immediately know which Audio stream was Live/relevant by comparing to original values... as only that particular AudioStream's Volume, that was live will be -1 lesser in value... get it??

see if you could find where my error was in the code... it'll be great...

really appreciate all the great help guys... learnt so much today

thanx.. catch you in the morning

YO!han
 

Attachments

  • VolumeStreamsTrial.zip
    8.1 KB · Views: 212
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Ahh, you are using the other command! No return doesn't need to be there, neither does the As Int in the Sub signature.
 
Upvote 0

persianpowerman1

Active Member
Licensed User
Longtime User
Hey manfred & steve... thanx a lot...
got up in the morning.. and it worked like a charm... wouldnt have happened without your help... still im only half way there in what i really need... thanx... will stay in touch!:)
 
Upvote 0
Top