Skip to content Skip to sidebar Skip to footer

Stream Videos In Client App Using Ajax And Java Restlet

Hi I want to stream videos in client app but videos are located in server app. I am using java Restlet and Jquery Ajax to connect client app to server app. Through Ajax call i am c

Solution 1:

After reading your comment, here is my understanding of what you should do. I would not send json to a resource in order to get something, I would just send a simple GET request. You need:

  • a resource that returns the file of a video according to its identifier. For the matter of illustration, let's say its url template is /videos/{videoid}
  • a web page that contains the links, and the empty video player
  • some javascript that set the "src" attribute video player with the url defined above: /videos/{videoid}. The way you compute the videoid is your own business.

Here is the server code:

  • the Restlet application, that defines the URI templates
@Override
public Restlet createInboundRoot() {

    Router router = new Router(getContext());

    // attaches the resource that represents a video, according to its identifier
    router.attach("/videos/{videoid}", VideoServerResource.class);
    // ... other instructions

    return router;
}
  • the video server resource:
public class VideoServerResource extends ServerResource {

    private File video;
    @Override
    protected void doInit() throws ResourceException {
        String videoId = getAttribute("videoid");
        // Compute path
        String path = "/tmp/" + videoId + ".mp4";
        video = new File(path);
        // takes care of not found status responses.
        setExisting(video.isFile());
    }

    @Get("mp4")
    public File represent() {
        return video;
    }
}

Here is the client code. This is a sample Web page, with an empty video player. When clicking on the button, the video player is asked to play the http://example.com:9000/videos/testvideo video. In your case, the value testvideo is simply deduced from the link the user click on.

<!DOCTYPE html> 
<html> 
<head>
    <script src="/static/jquery.js"></script>
    <script>
$('#playVideo').click(function (){
    srcPath = "http://127.0.0.1:9000/videos/testvideo";
    $('#videoTab').attr('src', srcPath);
    $('#videoTab').css('display', 'block');
    $('#videoTab').attr('autoplay', true);
});
    </script>
</head>
<body> 

<button id="playVideo" class="btn-primary">PlayVideo</button>
<video id="videoTab" height="300" width="500" style="display: none" controls ></video>
</body> 
</html>

I hope this will help you.


Post a Comment for "Stream Videos In Client App Using Ajax And Java Restlet"