Android Question JavaObject not always returning a value when media has finished playing

rleiman

Well-Known Member
Licensed User
Longtime User
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?

Thanks.
 

stevel05

Expert
Licensed User
Longtime User
Looking at the source code here: https://android.googlesource.com/pl...ter/media/java/android/media/AudioSystem.java

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.

The AudioManager class has a similar method isMusicActive, which may serve you better. : https://developer.android.com/reference/android/media/AudioManager

B4X:
Sub IsMusicPlaying As Boolean
    Dim JO As JavaObject
    JO.InitializeContext
    Return JO.RunMethodJO("getSystemService",Array("audio")).RunMethod("isMusicActive",Null)
End Sub
 
Last edited:
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
Hi Steve,

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.
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
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.
 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
Hi Steve,

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?

Thanks.
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
In Globals:
B4X:
Private AudioManager As JavaObject

In Appstart or initialize

B4X:
AudioManager.RunMethod("getSystemService",Array("audio"))

Then

B4X:
Sub IsMusicPlaying As Boolean
    Return AudioManager.RunMethod("isMusicActive",Null)
End Sub

If you are only running it every 15 mins, it may not make a difference.
 
Upvote 0
Top