MediaPlayer

Zate

Member
I want to play a .mp3 file.
B4X:
Sub Button1_Click
    MediaPlayer1.Initialize
    MediaPlayer1.Load(File.DirAssets, "name.mp3")
    MediaPlayer1.Play
End Sub
B4A compiles with no problem, but when I run the apk on my device I see:
Error occured
An error has occured in sub:
main_play(java line: 553)
java.io.FileNotFoundException:
name.mp3
Continue?

The message is clear, I've added the .mp3 file by clicking on files (between modules and logs) but nothing has changed...
 

XverhelstX

Well-Known Member
Licensed User
Longtime User
Try the following:

B4X:
Sub Button1_Click
    MediaPlayer1.Initialize
    MediaPlayer1.Load(File.dirRootExternal, "name.mp3")
    MediaPlayer1.Play
End Sub

and place name.mp3 in your SD card directory.
You can also copy the code from file.dirassests to your root with File.copy. (tell me if i'm worng :s)

XverhelstX
 
Upvote 0

Zate

Member
Where is File.DirInternal? :sign0104:
If I just change File.DirAssets in File.DirInternal there's another error:
Error occurred
An error has occured in sub:
main_play(java line: 553)
java.io.IOException: Prepare
failed.: status=0x1
Continue?
 
Upvote 0

Zate

Member
You mean
B4X:
If FirstTime Then
        If File.Exists(File.DirInternal, "Ronzio.mp3") = False Then
          File.Copy(File.DirAssets, "Ronzio.mp3", File.DirInternal, "Ronzio.mp3")
        End If
End If
This doesn't work
 
Upvote 0

Merlot2309

Active Member
Licensed User
Longtime User
Hi,

This works fine for me:

B4X:
        Dim ship, blub as Int

   If File.Exists(File.DirDefaultExternal, "Name.mp3") Then   
      ship = soundfile.Play(blub, 1, 1, 1, 0 , 1)
   Else
   ToastMessageShow("No sound!¿?!", True)
   End If

Forgot why I dimmed ship as Int, sorry.

Helen.
 
Upvote 0

Merlot2309

Active Member
Licensed User
Longtime User
Ok, start with the basic: do you hear the sound when you play it on the device in any player?

If yes, the code for checking if the file exists should work.

If no message, try different parameters.
 
Last edited:
Upvote 0

XverhelstX

Well-Known Member
Licensed User
Longtime User
The .mp3 is the sound for an app... I can't put it in the sd if I want to share the app.

That's why I added this:

You can also copy the code from file.dirassests to your root with File.copy.)

So when your app starts, you can copy the mp3 file from your assets to the SD card.
 
Upvote 0

Merlot2309

Active Member
Licensed User
Longtime User
Zate,

Deutsch?? Vielleicht zoll Klaus oder einer der anderen Deutschern morgen deine frage sehen oder kunnen Sie die komplete Kode fuer das mp3 zehen lassen?.

Entschuldigung fuer meine Deutsch :BangHead:

Ha, ha, I was so busy trying to write some German I missed the last ones. So skip this post.

Helen.
 
Last edited:
Upvote 0

Zate

Member
LOL! NO! I'm Italian, but the code is the same everywhere
I study German at school though, but I'm just 13 and I'm not that good

Fixed... I deleted the play routine and made my code a bit longer...
@XverhelstX, I understood the wrong thing
 
Upvote 0

Merlot2309

Active Member
Licensed User
Longtime User
Indeed lol, be happy that you are 13, there is a whole world ahead of you.
I will let you comunicate with Xverhelst then, the two of you will understand eachother way better.

Succes and I hope to see that you got the sound running.

Ciao,
Helen.
 
Upvote 0

Zate

Member
Thanks Helen, ciao!
Is it possible to resume instead of restart? If I write MediaPlayer1.Play I don't hear anything, but if I write MediaPlayer1.Load ecc it restarts...
 
Last edited:
Upvote 0

Merlot2309

Active Member
Licensed User
Longtime User
This is what I use with the Audio lib:

B4X:
Sub Activity_Resume

   If File.Exists(File.DirDefaultExternal, "/SoundVogels/" & Wet.Text & ".mp3") Then
      ship = speel.Play(blub, 1, 1, 1, 0 , 1)
   Else
   ToastMessageShow("Geluid is nog niet beschikbaar", True)
   End If
   
End Sub

Sub Activity_Pause (UserClosed As Boolean)
   
   speel.Stop(ship)
   speel.Unload(blub)
   ship = 0

End Sub
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…