I seem to be having a bit of a problem with the execution of this code which is in my service:
B4X:
Sub IsStreamActive(Stream As Int) As Boolean
Dim jo As JavaObject
Return jo.InitializeStatic("android.media.AudioSystem").RunMethod("isStreamActive", _
Array(Stream, 0))
End Sub
I'm using it so I know if any media like a video or music is playing. If media is not playing it returns false and if the media is playing it returns true. All might seem well but if I call again it after the media has finished playing, it will not return any value. I found out that was happening by using this coding.
B4X:
ToastMessageShow(IsStreamActive(3), False)
In that case, the toast is not showing anything. Not even a blank toast. The only way to get everything cleared and working again is to run the app and exit it.
Is there a way to refresh the JavaObject so it always returns a value?
It appears that the isStreamActive method is an Unsupported method meaning that is is not part of the SDK and may be removed from future releases. Which may well explain why the results are not consistent.
In fact, the android.media.AudioSystem class is no longer documented on the Android Developers website.
Sub IsMusicPlaying As Boolean
Dim JO As JavaObject
JO.InitializeContext
Return JO.RunMethodJO("getSystemService",Array("audio")).RunMethod("isMusicActive",Null)
End Sub
I just checked it and it works like the other coding. All I need to find out is how to refresh the coding so it will always return a true or false value.
It has the same issue as the other coding. I did some detailed steps to reproduce the problem.
If the coding returns false, then I can call it multiple times without any problem. If it returns true, then I can call it several times but the entire sub routine never executes. That means I need a way to clean out whatever the call to the java object does because restarting the app always clears it out.
I don't think calling the method call actually does anything except query the status.
If you are calling it a lot ,you could try setting the AudioManager as a Process or Class global variable, initialize it once either in AppStart or in your Class Initialize method if it's in a class, then just call RunMethod("isMusicActive",Null) on it in the sub.
The app calls the sub routine once every 15 minutes to play a sound file. Each call works until the subroutine returns a true value. When it returns true, I do some processing and let the user know the sound file will not play until the video like YouTube or other media they are listening or watching has finished. The only way currently to clear it out so the subroutine returns false when called is if I restart the app since all of this coding is in a service.
Can you show the needed coding that you described?