B4J Question looping mediaplayer

Status
Not open for further replies.

Olivier Zeegers

Active Member
Licensed User
Hello,

Why is the property looping not avaible for mediaplayer as in other examples in this forum ?
Library not up to date?

B4X:
    Dim m As MediaPlayer
m.Initialize("m",File.GetUri("D:\Kiwanis\2018-2019\Sportcommissie\DonkerRun\Rode Knop","loop.mp4"))
m.looping = true

gives error... looping is not an available property in the popup list
 

Daestrum

Expert
Licensed User
Longtime User
Not used the library but if its based on the javafx mediaplayer then you could probably use javaobject to set cycleCount
eg
B4X:
Dim jo As JavaObject = m
jo.RunMethod("setCycleCount",Array(noOfTimesAsAnInteger)) ' plays noOfTimesAsAnInteger times
'or
jo.RunMethod("setCycleCount",Array(jo.GetField("INDEFINITE")) ' repeats forever until stop or pause is applied
 
Last edited:
Upvote 0

Olivier Zeegers

Active Member
Licensed User
sorry, but I don't get it...

What I want to accomplish:

I have a screen with images on it.
In the background there should be a video with a fire effect running.
This I have made with this code:

B4X:
Private m as MediaPlayer
Private mv as JavaObject

m.Initialize("m",File.GetUri("D:\Kiwanis\2018-2019\Sportcommissie\DonkerRun\Rode Knop","effect5.mp4"))
mv.InitializeNewInstance("javafx.scene.media.MediaView",Array(m))
MainForm.RootPane.AddNode(mv,0,0,1000,1000)
mv.RunMethod("toBack",Null)

m.Play

This video should be running in LOOP.... I don't see properties on MediaPlayer for looping.

THEN....

So for now we have the startscreen with some images and labels on it... In the background the fire video looping....
Now when the users clicks the left mouse button I have to increment a value and display it AND a different video with an explosion effect should be playing on the background instead of the fire video file...
When this video with special effect has stopped the fire video file should begin again, Looped....

Any idea ?

thank you !!!!!!
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
if its the built in mediaplayer then there is
 

Attachments

  • cyclecount.png
    12.5 KB · Views: 315
Upvote 0

Olivier Zeegers

Active Member
Licensed User
ok, thank you that I did not see ;-) Now the fire effect is looping....
But when I press the mouse button, another mediaplayer should play the special effect video and when stopped I should again see the fire effect...
When I create a second mediaplayer it is placed on top of the fire effect and when the second mediaplayer stops playing the special effect it still is on top and i cannot see the fire effect...
does the mediaplayer fire an event when the video has stopped so I can delete the node ?
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

Olivier Zeegers

Active Member
Licensed User

ok, thank you DonManfred !

I cannot use the Wait for m_complete because the program has to be constantly listen for another mouseclick...
I assume that the command wait for pauses the program until m_complete is true ?

I created a sub m_complete
In this sub I delete the mediaplayer with the effect and create the mediaplayer again with the fire looping...
This works, but when I do not click on the mouse the sub m_complete is called when the fire loop video loops !
Strange, because I called this mediaplayer fire… and still it calls the m_complete sub....

B4X:
Sub m_complete
    Log("complete m")
    Dim MediaViewNode As Node = mv

    MediaViewNode.RemoveNodeFromParent
    
    fire.Initialize("m",File.GetUri("","fire.mp4"))
    firev.InitializeNewInstance("javafx.scene.media.MediaView",Array(fire))
    MainForm.RootPane.AddNode(firev,0,0,1000,1000)
    firev.RunMethod("toBack",Null)
    fire.CycleCount = -1
    
    fire.Play
    
End Sub
 
Upvote 0

Olivier Zeegers

Active Member
Licensed User
Hello,

Sorry to ask for your help again, but I really don't understand what happens even after watching the video on resumable subs.

I have 1 week left to finish this project and it will be used on a running contest so it has to be ready ;-)

I will put the code hereafter, I have uploaded a zip file with the source and the files..

What I don't understand is this:

The first time I click on the mousebutton the fire effect stops, the count is increased and numbers displayed and the fireworks effect shows. After that the fire effect starts again... This is how I like it to be...
But when I click for a second time, there is no firework effect... And the more I click the program gets slower and slower and hangs after some numbers ?

what am I doing wrong ??

thank you for your kind help !!

B4X:
#Region Project Attributes
    #MainFormWidth: 1920
    #MainFormHeight: 1080
#End Region

Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    
    Private Aantal As Int
    
    Private m As MediaPlayer
    Private mv As JavaObject
    Private fire As MediaPlayer
    Private firev As JavaObject
    
    Private Image0 As Image
    Private Image1 As Image
    Private Image2 As Image
    Private Image3 As Image
    Private Image4 As Image
    Private Image5 As Image
    Private Image6 As Image
    Private Image7 As Image
    Private Image8 As Image
    Private Image9 As Image
    
    Private EuroImage As Image
    
    Private Nummer1 As ImageView
    Private Nummer2 As ImageView   
    Private Nummer3 As ImageView
    Private Nummer4 As ImageView
    Private Nummer5 As ImageView
    
    
    Private mapOfViews As Map
    
    Private Euro As ImageView
    
    Private money As AudioClip
        
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    Dim Getal5 As Int
    
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Main")
    

    money.Initialize(File.GetUri("","Cha_Ching_Register-Muska666-173262285.wav"))
    
    Image0.Initialize("","0.png")
    Image1.Initialize("","1.png")
    Image2.Initialize("","2.png")
    Image3.Initialize("","3.png")
    Image4.Initialize("","4.png")
    Image5.Initialize("","5.png")
    Image6.Initialize("","6.png")
    Image7.Initialize("","7.png")
    Image8.Initialize("","8.png")
    Image9.Initialize("","9.png")
    EuroImage.Initialize("","euro-symbol2.png")
    
    mapOfViews.Initialize
    mapOfViews.Put("Im0", Image0)
    mapOfViews.Put("Im1", Image1)
    mapOfViews.Put("Im2", Image2)
    mapOfViews.Put("Im3", Image3)
    mapOfViews.Put("Im4", Image4)
    mapOfViews.Put("Im5", Image5)
    mapOfViews.Put("Im6", Image6)
    mapOfViews.Put("Im7", Image7)
    mapOfViews.Put("Im8", Image8)
    mapOfViews.Put("Im9", Image9)
    mapOfViews.Put("EuroImage",EuroImage)
    
    Aantal = 1
        
    Getal5 = 1
    
    Dim GetalPNG5 As Image = mapOfViews.Get("Im"&Getal5)
    Dim GetalPNG4 As Image = mapOfViews.Get("EuroImage")
    

    Nummer4.SetImage(GetalPNG4)
    Nummer5.SetImage(GetalPNG5)
    
        
    fire.Initialize("m",File.GetUri("","fire.mp4"))
    firev.InitializeNewInstance("javafx.scene.media.MediaView",Array(fire))
    MainForm.RootPane.AddNode(firev,0,0,1000,1000)
    firev.RunMethod("toBack",Null)
    fire.CycleCount = -1
    
    fire.Play

    Dim jfs As JavaObject = MainForm
    Dim stage As JavaObject = jfs.GetField("stage")
    stage.RunMethod("setFullScreen", Array As Object(True))

    MainForm.Show
End Sub

'Return true to allow the default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Return True
End Sub

Sub MainForm_MouseClicked (EventData As MouseEvent)
    Dim AantalStr As String
    Dim lengte As Int
    Dim i, x As Int
    Dim getal1, getal2, getal3, getal4, getal5, groepbreedte, links As Int
    
    Dim FadeOut As ImageView
    
    FadeOut.Initialize("")
    FadeOut.SetImage(Nummer5.GetImage)
    
    Aantal = Aantal + 1
    
    AantalStr = Aantal
    lengte = AantalStr.Length
    x = 5-lengte
    For i = 1 To x
        AantalStr = "0" & AantalStr
    Next
    
    getal1 = AantalStr.SubString2(0,1)
    getal2 = AantalStr.SubString2(1,2)
    getal3 = AantalStr.SubString2(2,3)
    getal4 = AantalStr.SubString2(3,4)
    getal5 = AantalStr.SubString2(4,5)
    
    Dim GetalPNG1 As Image = mapOfViews.Get("Im"&getal1)
    Dim GetalPNG2 As Image = mapOfViews.Get("Im"&getal2)
    Dim GetalPNG3 As Image = mapOfViews.Get("Im"&getal3)
    Dim GetalPNG4 As Image = mapOfViews.Get("Im"&getal4)
    Dim GetalPNG5 As Image = mapOfViews.Get("Im"&getal5)
    
    Select lengte
        Case 1
            
            groepbreedte = 475
            links = MainForm.WindowWidth/2
            links = links - (475/2)
            Nummer4.Left = links
            Nummer5.Left = links + 230 + 10
            Euro.Visible = False
            Nummer3.Visible = False
            Nummer2.Visible = False
            Nummer1.Visible = False
            Nummer4.Visible = True
            Nummer5.Visible = True
            
            Nummer4.SetImage(EuroImage)
            Nummer5.SetImage(GetalPNG5)
            
            
        Case 2
            
            groepbreedte = 230 +10 + 230 + 10 + 230
            links = MainForm.WindowWidth/2
            links = links - (groepbreedte/2)
            Nummer3.Left = links
            Nummer4.Left = links + 230 + 10
            Nummer5.Left = links + 230 + 10 + 230 + 10
            Euro.Visible = False
            Nummer1.Visible = False
            Nummer2.Visible = False
            Nummer3.Visible = True
            Nummer4.Visible = True
            Nummer5.Visible = True
            
            Nummer3.SetImage(EuroImage)
            Nummer4.SetImage(GetalPNG4)   
            Nummer5.SetImage(GetalPNG5)
        
        Case 3
            
            groepbreedte = 230 +10 + 230 + 10 + 230 + 10 + 230
            links = MainForm.WindowWidth/2
            links = links - (groepbreedte/2)
            Nummer2.Left = links
            Nummer3.Left = links + 230 + 10
            Nummer4.Left = links + 230 + 10 + 230 + 10
            Nummer5.Left = links + 230 + 10 + 230 + 10 + 230 + 10
            Euro.Visible = False
            Nummer1.Visible = False
            Nummer2.Visible = True
            Nummer3.Visible = True
            Nummer4.Visible = True
            Nummer5.Visible = True
            
            Nummer2.SetImage(EuroImage)
            Nummer3.SetImage(GetalPNG3)
            Nummer4.SetImage(GetalPNG4)
            Nummer5.SetImage(GetalPNG5)
            
        Case 4
            
            groepbreedte = 230 +10 + 230 + 10 + 230 + 10 + 230 + 230 + 10
            links = MainForm.WindowWidth/2
            links = links - (groepbreedte/2)
            Nummer1.Left = links
            Nummer2.Left = links + 230 + 10
            Nummer3.Left = links + 230 + 10 + 230 + 10
            Nummer4.Left = links + 230 + 10 + 230 + 10 + 230 + 10
            Nummer5.Left = links + 230 + 10 + 230 + 10 + 230 + 10 + 230 + 10
            Euro.Visible = False
            Nummer1.Visible = True
            Nummer2.Visible = True
            Nummer3.Visible = True
            Nummer4.Visible = True
            Nummer5.Visible = True
            
            Nummer1.SetImage(EuroImage)
            Nummer2.SetImage(GetalPNG2)
            Nummer3.SetImage(GetalPNG3)
            Nummer4.SetImage(GetalPNG4)
            Nummer5.SetImage(GetalPNG5)
            
        Case 5
            
            groepbreedte = 230 +10 + 230 + 10 + 230 + 10 + 230 + 230 + 10 + 230 + 10
            links = MainForm.WindowWidth/2
            links = links - (groepbreedte/2)
            Euro.Left = links
            Nummer1.Left = links + 230 + 10
            Nummer2.Left = links + 230 + 10 + 230 + 10
            Nummer3.Left = links + 230 + 10 + 230 + 10 + 230 + 10
            Nummer4.Left = links + 230 + 10 + 230 + 10 + 230 + 10 + 230 + 10
            Nummer5.Left = links + 230 + 10 + 230 + 10 + 230 + 10 + 230 + 10 + 230 + 10
            Euro.Visible = True
            Nummer1.Visible = True
            Nummer2.Visible = True
            Nummer3.Visible = True
            Nummer4.Visible = True
            Nummer5.Visible = True
            
            Euro.SetImage(EuroImage)
            Nummer1.SetImage(GetalPNG1)
            Nummer2.SetImage(GetalPNG2)
            Nummer3.SetImage(GetalPNG3)
            Nummer4.SetImage(GetalPNG4)
            Nummer5.SetImage(GetalPNG5)
            
    End Select
    
    money.Play
    
    Dim MediaViewNode As Node = firev
    MediaViewNode.RemoveNodeFromParent
    
    If Aantal = 10 Then
        
        m.Initialize("m",File.GetUri("","effect6.mp4"))
        mv.InitializeNewInstance("javafx.scene.media.MediaView",Array(m))
        MainForm.RootPane.AddNode(mv,0,0,1000,1000)
        mv.RunMethod("toBack",Null)
        m.Stop
        m.Play
        
    Else
        
        m.Initialize("m",File.GetUri("","effect5.mp4"))
        mv.InitializeNewInstance("javafx.scene.media.MediaView",Array(m))
        MainForm.RootPane.AddNode(mv,0,0,1000,1000)
        mv.RunMethod("toBack",Null)
        m.Stop
        m.Play
    
    End If
            
    Dim l As Int = Nummer5.Left
    Dim top As Int = 0 - Nummer5.Height
    Dim w As Int = Nummer5.Width / 2
    Dim h As Int = Nummer5.Height / 2
    

    FadeOut.Visible = False
    MainForm.RootPane.AddNode(FadeOut, Nummer5.Left, Nummer5.Top - 100, Nummer5.Width, Nummer5.Height)
    
        
    Nummer5.Visible = False
    FadeOut.Visible = True
    FadeOut.SetLayoutAnimated(500,l-100,top,w,h)
    Sleep(400)
    Nummer5.Visible = True
        

End Sub
Sub m_complete()
    Dim MediaViewNode As Node = mv

    MediaViewNode.RemoveNodeFromParent
    
    fire.Initialize("m",File.GetUri("","fire.mp4"))
    firev.InitializeNewInstance("javafx.scene.media.MediaView",Array(fire))
    MainForm.RootPane.AddNode(firev,0,0,1000,1000)
    firev.RunMethod("toBack",Null)
    fire.CycleCount = -1
    
    fire.Play
End Sub

http://www.zeegers.be/Donkerrun.zip
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I think that you are confused from this line:
B4X:
fire.Initialize("m",File.GetUri("","fire.mp4"))
You are setting the event name to "m". The complete event is fired whenever the playback completes (it will continue automatically). When that happens 'mv' wasn't initialized yet so you get an error.

All that you need to do in order to play fire.mp4 forever is to remove the event sub or change the event name:
B4X:
fire.Initialize("",File.GetUri("","fire.mp4"))
 
Upvote 0

Olivier Zeegers

Active Member
Licensed User

OK! it was that simple... ;-) thank you... that was the reason it was going into m_complete...

Now i have to figure out what to do when I click again while the second effect is playing...
When you click very fast the program hangs up...

Can I check if the fire video is running or not with

if MediaViewNode.IsInitialized = true then
endif

?
 
Upvote 0

Olivier Zeegers

Active Member
Licensed User

or

if MediaViewNode <> Null then
end if

but this does not work..
 
Upvote 0
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…