Android Question [Solved]Using Reflector lib Versus to using JavaObject lib

Theera

Expert
Licensed User
Longtime User
Refer to AmirPYTHON's code ,I would like to use javaobect instead for learning code. My code have error warning at line
is "unreachable code detected"
mystring.RunMethod("setVolume", Array(0.0, 0.0))


B4X:
Sub MuteVideo(videov As VideoView)
    Dim r1, r2 As Reflector
    r1.Target = videov
    r1.Target = r1.GetField("mMediaPlayer")
    If r1.Target = Null Then
        CallSubDelayed2(Me, "MuteVideo", videov)
        Return
    End If
    Dim mp As MediaPlayer
    r2.Target = mp
    r2.SetField2("mp", r1.Target)
    mp.SetVolume(0,0)
End Sub

Sub UnMuteVideo(videov As VideoView)
    Dim r1, r2 As Reflector
    r1.Target = videov
    r1.Target = r1.GetField("mMediaPlayer")
    If r1.Target = Null Then
        CallSubDelayed2(Me, "UnMuteVideo", videov)
        Return
    End If
    Dim mp As MediaPlayer
    r2.Target = mp
    r2.SetField2("mp", r1.Target)
    mp.SetVolume(1,1)
End Sub

My code is

B4X:
Public Sub MuteVideo(videov As VideoView)
        Try
        'Using CrossPlatform
            'Add  equal to Me Because using B4XPages
            Dim jo As JavaObject=Me
            'delete this under line Because using B4XPages
            ' jo.initializeContext
            Dim mystring As JavaObject = videov
            mystring=jo.GetField("mMediaPlayer")
            CallSubDelayed2(Me, "MuteVideo", videov)
            Return
        Catch
        'Using Non-CrossPlatform   
            Dim jo As JavaObject
            jo.initializeContext
            Dim mystring As JavaObject = videov
            mystring=jo.GetField("mMediaPlayer")
            CallSubDelayed2(Me, "MuteVideo", videov)
            Return
        End Try       
        mystring.RunMethod("setVolume", Array(0.0, 0.0))

End Sub

Public Sub UnMuteVideo(videov As VideoView)
        Try
            'Using CrossPlatform
            'Add  equal to Me Because using B4XPages
            Dim jo As JavaObject=Me
            'delete this under line Because using B4XPages
            ' jo.initializeContext
            Dim mystring As JavaObject = videov
            mystring=jo.GetField("mMediaPlayer")
            CallSubDelayed2(Me, "MuteVideo", videov)
            Return
        Catch
            'Using Non-CrossPlatform
            Dim jo As JavaObject
            jo.initializeContext
            Dim mystring As JavaObject = videov
            mystring=jo.GetField("mMediaPlayer")
            CallSubDelayed2(Me, "UnMuteVideo", videov)
            Return
        End Try
        mystring.RunMethod("setVolume", Array(1.0, 1.0))
End Sub
 
Solution
Your error is happening because of the Return line within the "Try" structure just after "CallSubDelayed2(Me,"MuteVideo",videov). Just remove that Return and the error will go away.

AmirPYTHON

Member
Try This Code

Example:
Sub MuteVideo(videov As VideoView)
    Dim jo As JavaObject = videov
    Dim mp As JavaObject = jo.GetField("mMediaPlayer")
    If mp.IsInitialized = False Then
        CallSubDelayed2(Me, "MuteVideo", videov)
        Return
    End If
    
    Dim params(2) As Float
    params(0) = 0.0
    params(1) = 0.0
    mp.RunMethod("setVolume", params)
End Sub

Sub UnMuteVideo(videov As VideoView)
    Dim jo As JavaObject = videov
    Dim mp As JavaObject = jo.GetField("mMediaPlayer")
    If mp.IsInitialized = False Then
        CallSubDelayed2(Me, "UnMuteVideo", videov)
        Return
    End If
 
    Dim params(2) As Float
    params(0) = 1.0
    params(1) = 1.0
    mp.RunMethod("setVolume", params)
End Sub
 
Upvote 0

Theera

Expert
Licensed User
Longtime User
Try This Code

Example:
Sub MuteVideo(videov As VideoView)
    Dim jo As JavaObject = videov
    Dim mp As JavaObject = jo.GetField("mMediaPlayer")
    If mp.IsInitialized = False Then
        CallSubDelayed2(Me, "MuteVideo", videov)
        Return
    End If
   
    Dim params(2) As Float
    params(0) = 0.0
    params(1) = 0.0
    mp.RunMethod("setVolume", params)
End Sub

Sub UnMuteVideo(videov As VideoView)
    Dim jo As JavaObject = videov
    Dim mp As JavaObject = jo.GetField("mMediaPlayer")
    If mp.IsInitialized = False Then
        CallSubDelayed2(Me, "UnMuteVideo", videov)
        Return
    End If
 
    Dim params(2) As Float
    params(0) = 1.0
    params(1) = 1.0
    mp.RunMethod("setVolume", params)
End Sub
There are many different ways to code, which makes me confused about how to write it. I try to study Non-CrossPlatforms and CrossPlatforms writing. When learning, try to write it down and save it for later study. I believe that there are probably people who are not as good at English and coding as I am.
 
Upvote 0

Fernando Solá

Member
Licensed User
Longtime User
There are many different ways to code, which makes me confused about how to write it. I try to study Non-CrossPlatforms and CrossPlatforms writing. When learning, try to write it down and save it for later study. I believe that there are probably people who are not as good at English and coding as I am.
You are correct. It is just like "speaking" there may be quite a lot of combinations of words to communicate the same message, and mostly no one wakes up as a todler one day and start communicating like a professional speaker. There are some better practices than others though, but ony by learning and practicing and expanding our knowledge and experience we can master anything. I can tell you are learning and still have some way to go before you are being able to do everything that you want to do with these new programming skills you are learning. Just keep coding and testing and seeing what works and what doesn't.. and in the way, focus on understanding why some things work (better) or why they don't.
 
Upvote 0

Theera

Expert
Licensed User
Longtime User
Thank you all of you.
 
Upvote 0

Theera

Expert
Licensed User
Longtime User
You are correct. It is just like "speaking" there may be quite a lot of combinations of words to communicate the same message, and mostly no one wakes up as a todler one day and start communicating like a professional speaker. There are some better practices than others though, but ony by learning and practicing and expanding our knowledge and experience we can master anything. I can tell you are learning and still have some way to go before you are being able to do everything that you want to do with these new programming skills you are learning. Just keep coding and testing and seeing what works and what doesn't.. and in the way, focus on understanding why some things work (better) or why they don't.
I think using JavaObject is useful more than using Reflector because when you create the b4xlib for B4i It will write similar code, but using NativeObject instead JavaObject.
 
Upvote 0

Fernando Solá

Member
Licensed User
Longtime User
There are important diferences between JavaObject and Reflection. Think of Reflection as a library that opens a window to do an "introspection" on an object. It is not as straight forward as JavaObject. Surely JavaObject is very useful and more commonly used, still Reflection is very useful too if you really know when and how to use it. It is not an easy thing to learn and really understand if you haven't mastered many other Java concepts before.
 
Upvote 0
Top