B4J Question How to play video? eg MP4 file

roberttdavey

Member
Licensed User
Longtime User
I have been using B4A for quite a while and developed an app that needs a short video (few secs) played, and have been able to do that with the VideoView player. I have been greatly impressed by the ease with which an android app can be developed using B4A. I have now started using the free (thank you!!) B4J to develop a cross-platform app that also needs to play a short (few secs) MP4 video. The JavaFX Scene Builder works great with B4J, and the modules/classes are a great and simple way of dividing a program up into meaningful chunks. Everything works great for me... except I cannot find a way to play the video file. When I search for help on it, I keep getting directed to B4A VideoView which is for android. Can anyone help me please? I've also searched through these questions and cannot find anyone else asking about this... so maybe I am missing something very obvious to others... sorry! Thanks in anticipation of your help. Robert
 

roberttdavey

Member
Licensed User
Longtime User
Thanks for the reply Steve... your reference: http://www.b4x.com/b4j/help/jfx.html#mediaplayer and the MediaPlayer tutorial: http://www.b4x.com/android/forum/threads/mediaplayer-tutorial.6591/ however make it clear that only audio files are played. Out of interest though, I wasn't able to play an audio file either, didn't seem to like my URI, and the B4J-jFX documentation isn't clear on what is required. It mentions you cannot use the assets folder for a wav file, but surely that is a reference to B4A? Confused! Thanks anyway.
 
Upvote 0

roberttdavey

Member
Licensed User
Longtime User
You should use File.GetUri to create the Uri.

For example:
B4X:
Dim mp As MediaPlayer
mp.Initialize("mp", File.GetUri(File.DirApp, "SomeFile"))
mp.Play
Thanks Erel for the help with the URI... I can play audio files now in debug and release mode... and also from subfolders within Files and Objects. It will even play the audio from an MP4 video file, though not show the video of course, as it is an audio player. Just to pursue my original question a little more, has anyone found a way to play a video file using B4J... is there maybe a way of accessing an external application like VLCmedia player from within B4J? I need to play a small (few secs) video about 250 pixels square in size, on top of the form layout. Thanks.
 
Upvote 0

icefairy333

Active Member
Licensed User
Longtime User
B4X:
Dim mp As MediaPlayer
    Dim jo,mv As JavaObject
mp.Initialize("mp",File.geturi(File.DirApp,"test.mp4"))
    'mp.Initialize("mp",File.GetUri(File.DirAssets,"darling.mp4"))
    mv=jo.InitializeNewInstance("javafx.scene.media.MediaView",Array As Object(mp))
    MainForm.RootPane.AddNode(mv,0,0,MainForm.Width,MainForm.Height)
mp.Play
 
Upvote 0

FK Leung

New Member
Licensed User
Longtime User
B4X:
Dim mp As MediaPlayer
    Dim jo,mv As JavaObject
mp.Initialize("mp",File.geturi(File.DirApp,"test.mp4"))
    'mp.Initialize("mp",File.GetUri(File.DirAssets,"darling.mp4"))
    mv=jo.InitializeNewInstance("javafx.scene.media.MediaView",Array As Object(mp))
    MainForm.RootPane.AddNode(mv,0,0,MainForm.Width,MainForm.Height)
mp.Play

Sorry, I am new to B4A, would appreciate if you can show a simple example on how to we can make the video works?
 
Upvote 0

FK Leung

New Member
Licensed User
Longtime User
Sorry, pls excuse me.
I followed exactly what is in the sample code...when I deployed it.. it said it could not play the video?
I have already included the audio library in the application..Did I miss something?
B4X:
Simple example of using VideoView:
Sub Globals
    Dim vv As VideoView
End Sub
Sub Activity_Create(FirstTime As Boolean)
    vv.Initialize("vv")
    Activity.AddView(vv, 10dip, 10dip, 250dip, 250dip)
    'vv.LoadVideo(File.DirRootExternal, "somefile.mp4")
    'vv.LoadVideo(File.DirRootExternal, "test.mp4").
    'vv.LoadVideo(File.DirAssets,"test.mp4")
    'vv.LoadVideo(File.DirDefaultExternal,"test.mp4")
    vv.LoadVideo("http","http://www.example.com/video.mp4")
    vv.Play
    vv.Play
End Sub
Sub vv_Complete
    Log("Playing completed")
End Sub
 
Upvote 0
Top