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

Discussion in 'Android Questions' started by rleiman, Oct 21, 2018.

  1. rleiman

    rleiman Well-Known Member Licensed User

    I seem to be having a bit of a problem with the execution of this code which is in my service:

    Code:
    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.

    Code:
    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.
     
  2. stevel05

    stevel05 Expert Licensed 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

    Code:
    Sub IsMusicPlaying As Boolean
        
    Dim JO As JavaObject
        JO.InitializeContext
        
    Return JO.RunMethodJO("getSystemService",Array("audio")).RunMethod("isMusicActive",Null)
    End Sub
     
    Last edited: Oct 21, 2018
    Erel, Peter Simpson and rleiman like this.
  3. rleiman

    rleiman Well-Known Member Licensed User

    Hi Steve,

    Thanks for the fast reply. I will try it out later today.

    :)
     
  4. rleiman

    rleiman Well-Known Member Licensed 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.
     
  5. stevel05

    stevel05 Expert Licensed 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.
     
  6. rleiman

    rleiman Well-Known Member Licensed 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.
     
  7. stevel05

    stevel05 Expert Licensed User

    In Globals:
    Code:
    Private AudioManager As JavaObject
    In Appstart or initialize

    Code:
    AudioManager.RunMethod("getSystemService",Array("audio"))
    Then

    Code:
    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.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice