Android Question mediaplayer.IsInitialized

Sebastjan

Member
Licensed User
Hey guys, one question..

i was writing small mediaplayer procedure for playing small wav mp3 ... files like "tada, bling... etc"
the idea was when user presses command button it also plays this short - 1 second sound.

my "player" went into "code module" and it was like this:

B4X:
Sub PlayFile2(sndFileName As String)
    Dim myPlayer As MediaPlayer
    myPlayer .Load(File.DirAssets,sndFileName)
    Player.Play
End Sub
it worked ok, problem was when user fast pressed button several times, media player stopped working until i restarted a program.

after some trial and error rewrote my sub like this. (we are still in "code module")

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Private Player As MediaPlayer  ' <- player obj
    Private PlayerIni As Boolean    ' <- variable for checking initialization
End Sub

Sub PlayFile(sndFileName As String)
    If Not(PlayerIni) Then
        PlayerIni=True
        Player.Initialize()
    End If
    If Player.IsPlaying Then
        Player.stop
    End If
    Player.Load(File.DirAssets,sndFileName)
    Player.Play
End Sub
and here is my question: i'm missing "mediaplayer.IsInitialized" property. Did i went wrong about this player? what do you use for playing small "tadaaa" sounds in jour app?

ps: this is not a game i'm making, just an app for company i work for...
 

hogiebaer

Active Member
Licensed User
try to initialize the player object in the activity_create (in the firststart section)

B4X:
Sub Activity_Create(FirstTime As Boolean)
   If FirstTime = True Then
      Player.Initialize
   end if
..
..
End Sub


Sub PlayFile(sndFileName As String)
  If Player.IsPlaying Then Player.stop
  Player.Load(File.DirAssets,sndFileName)
  Player.Play
End Sub
 
Last edited:
Top