Android Question [SOLVED] Is it possible to start & stop playing a music file with the Media Player Lib?

Mashiane

Expert
Licensed User
Longtime User
Ola

I'm trying SoundPool and its doing what I want but only works once. I am able to play a sound (infinity) when a firebase notification arrives.

In my activity I can click and button and stop the sound. This works, but it only happens once and follow up sounds played cannot be stopped (kinda frustrating).

I'm wondering if there is an alternative. The purpose is to play the sound, someone should stop it manually but it should not stop until someone intervens.

SoundPool is able to do this, but for some reason I stop the sound, it stops, then when other notifications come, the sound plays, it does not stop when pressing stop.

I'm just using SP.Stop(notifID)

Thanks.
 

Mashiane

Expert
Licensed User
Longtime User
This is what I've tried and the problems I have encountered.

In B4XMainPage.Class_Globals

B4X:
Private MP As MediaPlayer

In B4XMainPage.Initialize

B4X:
'MP.initialize2("mp")
    'Dim jo As JavaObject = MP
    'Dim p As Phone
    'jo.getfieldjo("mp").runmethod("setAudioStreamType", Array(p.VOLUME_NOTIFICATION))
    'MP.load(File.DirAssets, "alarm.mp3")

On B4XMainPage.HexMenu_Clik

B4X:
    'attempt to stop the sound
'    If MP.IsPlaying Then
'        MP.Stop
'        Return
'    Else
'        MP.Looping = True
'        MP.play
'    End If

THIS WORKS - however it only does this once. I am able to play the sound and stop it. When i click to play it again, nothing fires.

I have done the same exercise with the soundPool and it also does the same thing and with soundPool I cant stop the sound anymore.

Perhaps I am missing something.
 
Upvote 0

Max Koopman

Member
Licensed User
Longtime User
Hi Mashiane,
To be honest with you, that is actually the expected behavior for the mediaplayer library. As you can see in the MPlibrary docs (https://www.b4x.com/android/help/mediaplayer.html), calling stop means you have to use load again.
I think the easiest way to correct this is to pause the sound and then reset the position to 0. Or load it again as the library says.
 
Upvote 1
Solution
Top