Android Question MediaPlayer completed event, how to handle?

igorkaplan45

Member
Licensed User
Longtime User
Hi all,

Could someone please give me an example, how to handle the Completed event of MediaPlayer object? I have read the documentation, looked at tutorials. As I understood, you initialize the MediaPlayer object with Init2 and pass the event handler name. So I did the following:

Sub Process_Globals
dim MediaPlayer1 as MediaPlayer
...
End Sub

Sub Activity_Create(FirstTime As Boolean)
MediaPlayer1.Initialize2("MPStopped")
...
end Sub

Then, since I was not completely sure, what the name of event handler function should be, I have created 2 subs:

Sub MediaPlayer1_MPStopped
log("The playback has stopped")
end Sub

Sub MPStopped
log("MPStopped: the playback has stopped")
end Sub

Non of those methods are ever executed.
I started the playback, MediaPlayer plays fine, then I waited for playback to finish at the end of the song and did not get events to be fired.
I am probably doing something incorrectly, however was not able to find any example how to write event sub.
Would so much appreciate any clerifications.

Many thanks.
Igor.
 

klaus

Expert
Licensed User
Longtime User
Use this :
B4X:
Sub Process_Globals
    Dim MediaPlayer1 As MediaPlayer
End Sub

Sub Globals
    MediaPlayer1.Initialize2("MediaPlayer1")
End Sub

Sub MediaPlayer1_Complete
    Log("The playback has stopped")
End Sub
EventName is the first part of the event routine for the given view, MediaPlayer1 !
The second part is the event type, Complete.
It's the same as for all other views.
 
Upvote 0

igorkaplan45

Member
Licensed User
Longtime User
Klaus,

Thanks so much! I have tried everything, never would think, thatthe method name passed to initialize2 would the first part of event handler. However now it makes sense.
Many thanks once again!
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
I have tried everything, never would think, thatthe method name passed to initialize2 would the first part of event handler. However now it makes sense.

All initialize-commands which have an "Eventname" as property works the same. Eventname = first part of Event

B4X:
EditText1.initialize("button")
will result in a event button_Click
B4X:
Label1.initialize("lbl")
will result in a event lbl_Click
B4X:
Label1.initialize("myspecialevent")
will result in a event myspecialevent_Click
 
Upvote 0
Top