MediaPlayer Problem

mjtaryan

Active Member
Licensed User
Longtime User
I'm trying to use MediaPlayer, but an't seem to get it to work. The app is o play a series of mp3 files in order and, therefore, I am using a Complete event to control the whole process (MP_Complete). NOTE: There is a section of the app that displays the text content of the mp3 files. Both Activities use the same process. The text alone is working precisely as it should. Therefore, the basic logic of the process seems to be acceptable, even if it's not elegent.

I've used Initialize2 as follows from within Sub Init:

MP.Initialize2("MP_Complete")
SM = False
SM = StartMsg
SCFlag = False

The app waits for the user to start the process by tapping the screen, which raises a pnlST_Clicck event, which calls MP_Complete for the first time to get the name of the first audio file.

Sub pnlST_Click
If (SM = True) AND (MPfile = "") Then
MP_Complete
End If

End Sub

Sub MP_Complete assigns the file name to MPfile and then further down in the sub, calls the sub that actually loads and plays the file. The Log is used to indicate whether MP_Complete has been called; if so Tmp wiill show 0, if not the Log will not be written. The sub that plays the audio, sets Tmp to 1 so that when MP_Complete is called again at the end of the playing audio, the Log will be written showing 1

Sub MP_Complete
Log("Tmp = " & Tmp)
MPfile = "file1.mp3"
.
.
.
ShowST
MP.Release
'Code to advance to the next mp3 file.
End Sub

ShowST is called to play the file:

Sub ShowST
MPfile = MPfile & PrLn & ".mp3"
Log("MPfile = " & MPfile)
If MPfile <> "sc_0.mp3" Then
Activity.Finish
End If
MP.Load(File.DirAssets, MPfile)
MP.Play
If MP.IsPlaying = True Then
Log("MP is playing")
Else
Log("MP is NOT playing")
End If

End Sub

PrLn is a conter that increments in the MP_Complete event (the mp3 file names use a prefix followed by a number and ".mp3" I've used the Log entries to try to determine where and possibly why things are going wrong. These show:

Tmp = 0
MPfile = sc_0.mp3
MP is playing

What happens is this. At the point when the first file is supposedly playing, there is no sound and the program stalls without displaying an error. So, I need some help determing what is wrong and how to correct it. Thanks.
 
Last edited:

mjtaryan

Active Member
Licensed User
Longtime User
Addendum:

I wrote a very short program that creates, initializes (initialize2), loads and plays a file. The program worked more or less expected, playing the file.

I added MP.Release in the MP_Complete event and encountered an error -- something about a pointer reference. When I removed the line, it worked.

I had a Msgbox in the complete event that clcosed the app (Activity.finish) when OK was tapped. It was only then that the audio file played, not while the app was running. I then removed the Msgbox and the audio played, but shortly after it started the app closed so that it didn't finish playing until after the app was closed. From this I can only conclude the Complete event is raised before the file finishes playing.

As you will notice in the previous post the MP_Complete event does have an MP.Release statement at its end. I suspect that may be contributing to the problem of not playing. However, it is also true that the MP_Complete event is not called after the first file supposedly plays and finishes nor does the first file have any sound.

What I want to happen is for the series of files to be played back to back, with each file starting to play as soon as the previous one finishes. So, after finding out how to get an initial file to play, how do I get the next one to play right away. Thanks.
 
Last edited:
Upvote 0

mjtaryan

Active Member
Licensed User
Longtime User
You should export your project as a ZIP and post it here so we can help you.

Hi Margret,


Thanks. I'm afraid I'm not able to do that for two reasons; first its proprietary (and is not owned by me) and second, the code is very long. It is also working fine except for this and I've posted the only code that actually references the MediaPlayer. The code I left out that's part of Sub MP_Complete merely increments the filee counter and then goes back to the top of the loop to assign the file name to variable MPfile and then goes to play the file. The rest of the code is conditional to determine what is to actually be played and the corresponding text to be displayed. Thanks.
 
Upvote 0

Djembefola

Active Member
Licensed User
Longtime User
B4X:
MP.Initialize2("MP_Complete")

should be

B4X:
MP.Initialize2("MP")


I did not try to completely understand the logic of your app, but this line seems to be false.
 
Upvote 0

mjtaryan

Active Member
Licensed User
Longtime User
B4X:
MP.Initialize2("MP_Complete")

should be

B4X:
MP.Initialize2("MP")


I did not try to completely understand the logic of your app, but this line seems to be false.

You would have to understand the whole package.

I tried the initialization code you suggested (using "MP" instead of "MP_Complete") and it didn't work either. For some reason the Complete event doesn't occur. I've stepped through the program and the event doesn't get triggered.

I put together a short test program I was going to attach the entire test project, but the mp3 files put over the top of max size accepted in the forums. So, I've merely attached the b4a source code. This test project encounters the same problem as the primary project. Thanks.
 

Attachments

  • MPTest.zip
    897 bytes · Views: 203
Upvote 0

Djembefola

Active Member
Licensed User
Longtime User
You would have to understand the whole package.

Please read my last post again. In your test program you have made the same mistake again. Your code
-
B4X:
mp.initialize2("mp_complete")
is wrong!


Your "mp_complete" procedure will never be called by the mediaplayer object

Change it to

B4X:
mp.initialize2("mp")

and media player will raise the event!

You only need to pass the prefix "mp", mediaplayer will automatically append "_complete".
 
Last edited:
Upvote 0

mjtaryan

Active Member
Licensed User
Longtime User
Please read my last post again. In your test program you have made the same mistake again. Your code
-
B4X:
mp.initialize2("mp_complete")
is wrong!


Your "mp_complete" procedure will never be called by the mediaplayer object

Change it to

B4X:
mp.initialize2("mp")

and media player will raise the event!

You only need to pass the prefix "mp", mediaplayer will automatically append "_complete".

Did you read my reply to your previous note? As I said, I tried doing as you suggested and MP_Complete was still not raised. However, the problem was solved when I renamed the MediaPlayer and used "MP" for the event name as you suggested. It seems that naming the MediaPlayer "MP" and having an event of the same name ("MP") caused some type of conflict or so it appears. Thanks for your help.
 
Upvote 0
Top