B4J Question How do you get exoplayer to play a file from a websocket?

cbal03

Active Member
Licensed User
Longtime User
I'm hoping someone can help me understand how to get a sample of this working.

The former functionality of my b4a app uses an apache webserver to get its content for exoplayer to play and works nicely. But now I'd like to remove the apache webserver from the mix.
Since traditional webserver functionality appears to be unavailable for b4j (thanks @aeric), I thought that getting the content using websockets might be possible.
jServer is the answer! Thanks @Erel

I'm using websockets for connecting to b4j from a b4a app.
It is possible to have B4j send a media file to exoplayer through a socket?
If so, is there a sample somewhere I could view?

I'm unfamiliar with preparing a file for transport through a socket and handling the mobile side of receiving the file for playback.
I ran a few samples from these forums using asynchstreams but those just copy the files over to the device. I'd like for exoplayer to play the file as it arrives as though it were an http request or similar.

Are there any thoughts or pointers to get me moving in the right direction? Maybe a few steps of what is needed to achieve this?
For reference: I have attached a pic of the libraries I'm using both for B4A and B4J. I believe they are the latest but I could be wrong about one or more of them.

After some searching on the web: It does appear to be possible..
I found a solution here (See answer #1 a bit down the page) but this is not for B4X design environments so implementation would be difficult for me.

Thanks, Chris
 

Attachments

  • Untitled.png
    Untitled.png
    50.5 KB · Views: 26
Last edited:

aeric

Expert
Licensed User
Longtime User
I don't know why you stop using Apache server.

If you want to do live streaming, maybe you can use nginx server to support rtmp and play using vlc player. It works in desktop vlc player but I haven't tested on Android or B4A.

If you just want to play a video file hosted on a server, then B4J server can serve the file publicly as mentioned by Erel above.
 
Upvote 0

cbal03

Active Member
Licensed User
Longtime User
You don't need an apache server to serve files and you also don't need WebSockets. This just makes things more complicated and less efficient. jServer can serve files directly. You can put them in the www folder or you can serve them with a regular non-WebSockets handler.
A static folder inside the application path will not work. I need to be able to specify serving directories outside of the application path.
 
Upvote 0

cbal03

Active Member
Licensed User
Longtime User
I don't know why you stop using Apache server.

If you want to do live streaming, maybe you can use nginx server to support rtmp and play using vlc player. It works in desktop vlc player but I haven't tested on Android or B4A.

If you just want to play a video file hosted on a server, then B4J server can serve the file publicly as mentioned by Erel above.
I am removing apache so that the B4J application is a single stand-alone application. A user is not expected to understand apache to use this application.
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
A static folder inside the application path will not work. I need to be able to specify serving directories outside of the application path.
Have you tried setting the path to your preferred path?
It should work!
E.g
B4X:
srvr.StaticFilesFolder = File.Combine("C:\inetpub", "public_html")

Edit: I may have misunderstood.
If you want to set the static files folder inside your application folder, this is already given in the server tutorial.
E.g
B4X:
srvr.StaticFilesFolder = File.Combine(File.DirApp, "www")

Take note:
During debug, File.DirApp is Objects folder.
During release, it is the path same level as your app.jar file.
Make sure you have copied the static files folder e.g "www" to this location.
 
Last edited:
Upvote 0

cbal03

Active Member
Licensed User
Longtime User
Thanks so much for taking the time to provide the info.

Does this mean I can do:
B4X:
srvr.StaticFilesFolder = File.Combine("D:\music", "") '<-- blank folder
or
B4X:
srvr.StaticFilesFolder = "D:\movies"
and initialize several instances for more than one directory being served?

so that the user does not have to copy their files into any folders?
and access files like: http://ip address : port/file.mp4


So if I have D:\music, D:\movies, D:\photos in a list, each folder is served on a different port. Apache does this via virtual hosts.

The intention is to select a folder in B4J using a simple folder browser and add the selected folder to a serving list. The port is automatically assigned for the selected directory starting with the primary websocket port + 1 (the primary port is used for connections and communications e.g. getting information on where files are located and presenting selections to the user in B4A. <--this already works but getting the file from B4J to exoplayer in B4A app is stopping me.

This folder is then accessed by the mobile device via http for getting the file to exoplayer for playback. <-- i want to do this

I have had this system up and running for a few years (over the web and lan) and runs flawlessly albeit using a separate webserver limited only by the number of virtual hosts defined in apaches config file (one port per directory served).

I have duplicated everything from the old WAMP (windows, apache, mysql, php) setup in my B4J application except a method for file retrieval (the webservers job) and everything works as expected so far but I cannot find a solution for being limited to a static predefined directory in any library I've tried.

It is a beautiful system thanks to B4A and its capabilities and I would love for B4J to be able to serve directories in a similar fashion. This would make B4J an all-in-one solution for performing the same job as WAMP.

If B4J or any libraries cannot serve multiple specific directories for whatever reason then I am limited to manually streaming the files.
This is why I asked about using websockets.


Again, Thank you so much @Erel @aeric for your valuable responses.
 
Upvote 0

cbal03

Active Member
Licensed User
Longtime User
You don't need an apache server to serve files and you also don't need WebSockets. This just makes things more complicated and less efficient. jServer can serve files directly. You can put them in the www folder or you can serve them with a regular non-WebSockets handler.
I'll look into this. thanks!
 
Upvote 0

cbal03

Active Member
Licensed User
Longtime User
It works Beautifully!!
B4X:
'// Web server instances
private Sub start_HTTPservers
    'we start instances of jServer - http functionality for exoplayer
    'each "http server" serves a single directory root on a unique port (stored in directories table)
    'if serving = true then we start instance
    'instance is started after checking 'serving' flag
   
    Dim Cursor As ResultSet = DB.ExecQuery("SELECT * FROM directories")
    Do While Cursor.NextRow
        If(Cursor.GetString("serving") = "True") Then 'if this servers directory was scanned and files were catalogged
            Dim localpath As String = Cursor.GetString("path") 'get path from db
            Dim webpath As String = localpath.Replace("\", "/") 'convert to web friendly path
           
            Dim server As Server
            server.Initialize("") 'no events used
            server.Port = Cursor.GetString("port") 'port is auto assigned during server creation in ui
            server.StaticFilesFolder = webpath 'assign web path to server instance
            server.Start
           
            Log("Started HTTP Listener for Media Server: " & Cursor.GetString("name") & "HTTP PATH: " & webpath & " on PORT: " & server.Port)
        End If
    Loop 'next server item in table
    Cursor.Close
End Sub

jServer is really simple.
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
Does this mean I can do:
Yes

and initialize several instances for more than one directory being served?
Not sure.
As far as I know, only 1 path.

each folder is served on a different port
As for http port is only 1 port, https/ssl another port, ws another port, wss another...
I don't think you can have more ports under 1 protocol.

If you want to serve more than 1 and different ports then just compile and run more than 1 app.
This is what I understand.
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
Here is another solution
 
Upvote 0
Top