Android Question Activity_KeyPress

Declan

Well-Known Member
Licensed User
Longtime User
I have an Activity that plays either a Video or an Audio file.
I am attempting to use the device "Back" button to Stop/Close the video/audio play function if they are still running AND Close the Activity.
I have the following code:
B4X:
Sub Activity_KeyPress(KeyCode As Int) As Boolean
    If KeyCode = KeyCodes.KEYCODE_BACK  Then
        If pnlPlayAudio.Visible = True Then
            MediaPlayer1.Stop
            timerAudio.Enabled = False
            pnlPlayAudio.Visible = False
        End If
        If vv.IsPlaying = True Then
            vv.stop
            vv.RemoveView
        End If
       
        If pnlPlayAudio.Visible = False And vv.IsPlaying = False Then
            Activity.Finish
        End If
    Else

    End If
    Return True
End Sub

I am doing something dodgy that I can't seem to find.
I receive the following error when I use the above code:
B4X:
mygallery_activity_keypress (java line: 453)
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean ice.videoviewExt.isPlaying()' on a null object reference
    at ice.vvewrap.IsPlaying(vvewrap.java:71)
    at com.rob.ebuki.mygallery._activity_keypress(mygallery.java:453)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:169)
    at com.rob.ebuki.mygallery$HandleKeyDelayed.runDirectly(mygallery.java:228)
    at com.rob.ebuki.mygallery$HandleKeyDelayed.run(mygallery.java:225)
    at android.os.Handler.handleCallback(Handler.java:815)
    at android.os.Handler.dispatchMessage(Handler.java:104)
    at android.os.Looper.loop(Looper.java:194)
    at android.app.ActivityThread.main(ActivityThread.java:5631)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)
 

stevel05

Expert
Licensed User
Longtime User
As a guess, I would think that you have not played a video, and the variable vv has not been initialized.

Try changing the code to:

B4X:
If vv.isInitialized AND vv.IsPlaying

and

B4X:
If pnlPlayAudio.Visible = False And vv.IsInitialized And vv.IsPlaying = False Then
 
Upvote 0

Declan

Well-Known Member
Licensed User
Longtime User
Thanks, I have changed the code to:
B4X:
Sub Activity_KeyPress(KeyCode As Int) As Boolean
    If KeyCode = KeyCodes.KEYCODE_BACK  Then

        If pnlPlayAudio.Visible = True Then
            MediaPlayer1.Stop
            timerAudio.Enabled = False
            pnlPlayAudio.Visible = False
        End If
        If vv.isInitialized And vv.IsPlaying  Then
            vv.stop
            vv.RemoveView
        End If
        If pnlPlayAudio.Visible = False And vv.IsInitialized And vv.IsPlaying = False Then
            Activity.Finish
        End If
    Else

    End If
    Return True
End Sub
The pnlPlayAudio/MediaPlayer1 works as required.
If an audio file is playing and the user pressed the Back button, the MediaPlayer stops and the pnlPlayAudio is removed.

However, I have a problem with the video player vv.
1). If a video is playing and the Back button is pressed, the Activity is removed. I only wish to Stop the video and RemoveView.
2). If there is no Audio Playing or Paused AND there is no Video Playing or Paused and the Back button is pressed, the Activity.Finish.
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
That is because your three tests are not mutually exclusive, In the first test you are setting pnlPlayAudio.Visible to false and then testing it in the third test.

You only want to finish the activity if the third test is true when the sub is entered, so put that one first.

B4X:
If pnlPlayAudio.Visible = False And vv.IsInitialized And vv.IsPlaying = False Then
            Activity.Finish
 End If
If pnlPlayAudio.Visible = True Then
     MediaPlayer1.Stop
     timerAudio.Enabled = False
     pnlPlayAudio.Visible = False
 End If
 If vv.isInitialized And vv.IsPlaying  Then
     vv.stop
     vv.RemoveView
 End If

That should do it.
 
Upvote 0

Declan

Well-Known Member
Licensed User
Longtime User
Only glitch:
The user MUST play the vv video before pressing the Back button, otherwise the activity does not close.
If audio is played and then the Back button is pressed, the activity does not close.
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Try this

B4X:
If pnlPlayAudio.Visible = False And ((vv.IsInitialized And vv.IsPlaying = False) OR vv.IsInitialized = False) Then
 
Upvote 0
Top