Android Question MediaPlayer

mae08

New Member
Hello, can you help me? I badly want to fix it right now because this project should be pass tomorrow morning. I made an lyrics application and then if I press twice the play button of the MediaPlayer, the music will play like a boomerang. What should I do? This is the code I used:

Dim Mp As MediaPlayer
Dim Pause1 As Button
Dim Play1 As Button
Dim Stop1 As Button

Sub Pause1_Click
Mp.Load(File.DirAssets, "BTS - ANPANMAN.mp3")
Mp.Pause
End Sub
Sub Play1_Click
Mp.Initialize
Mp.Load(File.DirAssets, "BTS - ANPANMAN.mp3")
Mp.Play
End Sub
Sub Stop1_Click
Mp.Load(File.DirAssets, "BTS - ANPANMAN.mp3")
Mp.Stop
End Sub
 

ac9ts

Active Member
Licensed User
Longtime User
It's been awhile since I used MediaPlayer but your boomerang might be because you are reloading the sound file each time a button is pressed. Load the sound file from a different sub and have your buttons do the single task. This is very simplified.

B4X:
Dim Mp As MediaPlayer
Dim Pause1 As Button
Dim Play1 As Button
Dim Stop1 As Button

Sub LoadSound
   Mp.Initialize
   Mp.Load(File.DirAssets, "BTS - ANPANMAN.mp3")
End Sub

Sub Pause1_Click
   Mp.Pause
End Sub

Sub Play1_Click
   Mp.Play
End Sub

Sub Stop1_Click
   Mp.Stop
End Sub

Also, if you want to know when the sound file is done playing on its own (not when you press stop), modify LoadSound and add MP_Complete

B4X:
Sub LoadSound
   MP.Initialize2("MP")
   Mp.Load(File.DirAssets, "BTS - ANPANMAN.mp3")
End Sub

Sub MP_Complete

   ' Do something here like loading the next track, informing the user the track is complete, etc.

End Sub
 
Last edited:
Upvote 0

Harris

Expert
Licensed User
Longtime User
Sub Play1_Click
mp.Pause ' no ill effects if something isn't playing...
Mp.Play

End Sub

If something is playing, it will be stopped.
There are much better options than MediaPlayer - like ExoPlayer.
 
Upvote 0
Top