So I've been doing a lot of searching today, trying to figure out how to play videos inside of a JFrame. The reason I want this is because, I'm making a game, and want the option to add movie clips, like in most every good game, such as GW2, Medal of Honor, etc.
So, in my searches, I found JMF, but was completely unable to use it. It is sort of frustrating, but whatever. So, my question is this: Is there a way to play videos WITHOUT installing any other jar's, exe's, etc? For example, run a simple code such as new JFrame(); sort of quick and easy? or is that not possible, but there is a way to do it long and complex?
I've also been looking at other stack overflow stuff, and none of it really fits what i want... If worst comes to worst, i'll just use Xuggler, but i'd rather not.
Also, based on this answer, I plan on making a game engine in the future, so this could be added to it, for additional value.
Thank you in advance :)
PLEASE NOTE: I am not looking for references to JMF, or anything else like that. I'm looking for things such as built in methods/classes to call, or long work arounds, that work pretty good and would be implementable in numerous environments.
EDIT: I was thinking of using JEditorPane, and embedding the video with html, but.... that hasnt been working for me... here's what i've tried there:
JEditorPane jep = new JEditorPane();
jep.setEditable(false);
// jep.setContentType("text/html");
jep.setText("<html><video id=\"sampleMovie\" src=\"C:\\users\\austin\\desktop\\test.mp4\" controls></video></html>");
JScrollPane scrollPane = new JScrollPane(jep);
JFrame f = new JFrame("Test HTML");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(scrollPane);
f.setPreferredSize(new Dimension(800,600));
f.setVisible(true);
But this doesnt seem to be working... Please help!
JMF seems to have been around since 1997. No wonder then that, as you said, most of the Java-based players out there rely on it.
So I decided to take a trip back in time using Google's search options and found a result from Feb 2001: a very simple GNU-licensed MPEG-1 player implemented from the ground up using just Java and C. How about that? The following capture is from the website:
I know you emphasised you needed a solution with as little external code as possible. In this case, no additional libraries seem to be required but you have to do some compiling. Plus you're limited to MPEG-1. So, not exactly what you were looking for but perhaps worth a look.
Hope it helps!
if you have a File name "file":
import java.io.*;
import java.net.*;
import javax.swing.*;
//....
//....
//....
try{
mediaURL = file.toURI().toURL();
}
catch(Exception e){
}
if(mediaURL != null){
JFrame mediaTest = new JFrame();
MediaPanel m = new MediaPanel(mediaURL);
mediatTest.add(m);
mediaTest.setVisible(true);
}
Edited:
import javax.media.*;
import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.net.*;
public class MediaPanel extend JPanel{
public MediaPanel( URL mediaURL){
setLayout(new BorderLayout());
try{
Player mp = Manager.createRealizedPlayer( mediaURL);
Component video = mp.getVisualComponent();
Component controls = mp.getControlPaneComponent();
if(video != null){
add(video, BorderLayout.CENTER);
}
if(controls != null){
add(controls, BorderLayout.SOUTH);
}
mediaPlayer.start();
}
catch(Exception e){
}
}
}
Related
To be honest I am not entirely sure what is wrong. This is the short version of a ton of other basic robot command movements under the if and if else.
Whenever I run the program the mouse should move to the designated position and click. However when I run the program it does not move to the position I indicated, instead it moves to a different position each time I run it(I do not have any listeners designated to change the position so the position shouldn't be changing). I do not know if it is something with the code I have written itself or possibly my imports? The program was running correctly until recently in which I added the else at the end to end the program, I have run it without the else and still come up with the same issue. Any help would be much appreciated.
package creator;
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import javax.swing.*;
public class RobotDemo extends JFrame implements ActionListener
{
private static final long serialVersionUID = 1L;
public static void main(String[] args) throws AWTException, IOException
{
double value = (-0.66721);
{
if (value < -0.3)
{
Robot robotdelta = new Robot();
//Enters Chrome from java
robotdelta.delay(5000);
robotdelta.mouseMove(587, 1045);
robotdelta.delay(1000);
robotdelta.mousePress(InputEvent.BUTTON1_MASK);
robotdelta.delay(1000);
robotdelta.mouseRelease(InputEvent.BUTTON1_MASK);
robotdelta.delay(1000);
//Enters online platfrom
robotdelta.mouseMove(770, 21);
robotdelta.delay(1000);
robotdelta.mousePress(InputEvent.BUTTON1_MASK);
robotdelta.delay(1000);
robotdelta.mouseRelease(InputEvent.BUTTON1_MASK);
robotdelta.delay(1000);
}
//secondary situation
else if (value > 0.3)
{
Robot robotdelta = new Robot();
//Enters Chrome from java
robotdelta.delay(1000);
robotdelta.mouseMove(587, 1045);
robotdelta.delay(100);
robotdelta.mousePress(InputEvent.BUTTON1_MASK);
robotdelta.delay(100);
robotdelta.mouseRelease(InputEvent.BUTTON1_MASK);
robotdelta.delay(100);
//Enters online platfrom
robotdelta.mouseMove(770, 21);
robotdelta.delay(100);
robotdelta.mousePress(InputEvent.BUTTON1_MASK);
robotdelta.delay(100);
robotdelta.mouseRelease(InputEvent.BUTTON1_MASK);
robotdelta.delay(1000);
}
else
{
system.exit(0);
}
}
}
public void actionPerformed(ActionEvent e) {
}
}
As a disclaimer, I was playing around with this class for a while, and the most important thing I learned was this was a tool meant for very rudimentary testing, and really no large scale crucial operation should ever hinge on this class working exactly as expected.
To answer your question, there is really no way to get exactly where you tell mouseMove() to go (at least not when I was working with it). However, what seemed to get it pretty close was to call mouseMove() multiple times to the same place (Yes, this is very hacky and not desirable). For example, is I wanted to move the mouse to (300,600) on screen, I found that if you do:
mouseMove(300,600);
mouseMove(300,600);
mouseMove(300,600);
// ... can put more if you want
for some strange reason it gets much closer to where you want to go than if you just call mouseMove() one time. I have no idea why this might be the case, but hopefully this helps. Not to mention, it is also a good idea to put ample delays in between calling the robot to do different actions, and to ensure that waitForIdle() is invoked.
I’ve trimmed down the code to only the relevant parts and posted it below. The code works fine. The video plays when you run it but it doesn’t have a seekbar.
public class Screen {
//JFrmae
private JFrame frame;
// Panel which I add the canvas to
private JPanel pVid = new JPanel();
// Canvas
Canvas canvas = new Canvas();
// Embedded Media Player
EmbeddedMediaPlayer emp;
/**
* Create the application.
*/
public Screen() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
//Frame
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
//Adding the panel to the frame
frame.getContentPane().add(pVid);
//Adding the canvas to the panel
pVid.add(canvas);
//Setting canvas size
canvas.setSize(715, 402);
//Loading the VLC native library
NativeLibrary.addSearchPath(RuntimeUtil.getLibVlcLibraryName(), "lib");
Native.loadLibrary(RuntimeUtil.getLibVlcLibraryName(), LibVlc.class);
//Initializing the media player
MediaPlayerFactory mpf = new MediaPlayerFactory();
//Misc
emp = mpf.newEmbeddedMediaPlayer(new Win32FullScreenStrategy(frame));
emp.setVideoSurface(mpf.newVideoSurface(canvas));
//Video file name and playing
String file = "video.mp4";
emp.prepareMedia(file);
emp.play();
//pack method
frame.pack();
}
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Screen window = new Screen();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
I’ve looked for an answer online for the last 4 days. Finally I decided to ask here. The official website for vlcj has pictures of a vlcj player they’ve made. There is a seekbar in those pictures. Link to the webpage which has the pics: http://capricasoftware.co.uk/#/projects/vlcj
They have a number of useful tutorials there but they don’t have any instructions for adding the seekbar.
Then I tried downloading their vlcj-player project from their GitHub page. It showed an error because it couldn’t resolve the “com.google.common.collect.ImmutableList” which is supposed to be imported. (At the moment I’m reading about ImmutableList and stuff and see if there’s a way to fix it.) Since I couldn’t figure that out yet, I looked for a class named seekbar or the like in their project. I couldn’t find any.
I also searched elsewhere online for the answer but I just couldn’t find it. I’d really appreciate any help. Thank you.
Edit:
(This edit is in response to the suggestion given to me by #caprica. Read their comment to this question and my reply to that in the comment to understand what I'm talking about here in this edit. I think it'll be useful for others in the future.)
All right, there must have been some problem with my Eclipse or computer. (I’ll type out how I fixed it at the end of this comment.) It’s working now. I’ll type out what I did step by step so that may be it’ll be useful to others in the future to download and install the project.
Download the project.
Import it as a Maven project. (Import > Maven > Existing Maven Project)
Now in Eclipse right click the imported project and select Run As > Maven Install
And that’s it. Now you can just run the project normally. If you don’t know how to run the project, do it like this. Right click the project and select Run As > Java Application and then Select VlcjPlayer – uk.co.caprica.vlcplayer.
Alternatively you can open the class where the main method is and run it. VlcjPlayer class is where the main method is located. The class is in the package uk.co.caprica.vlcplayer.
The problem I faced was that somehow all the necessary files didn’t get downloaded when I ran it as Maven Install. But it worked fine in another computer. Since I knew where the files are downloaded to, I just copied the folder from that PC and put it in the same place in my PC. The folder name is ‘repository’. It’s location is C:\Users\User Name\ .m2. Perhaps Eclipse in this PC has some problem. I’ll reinstall it later to avoid problems in the future.
And this may be useful, the VLC that’s installed in this PC is 64 bit. Not sure if that makes a difference but mentioning it just in case.
Now that the app is working fine I will see the code and see how the seekbar is made. Thanks a lot #caprica for telling me that I should import it as a Maven project. :)
The Basic Controls tutorial shows the essential approach: Add a panel of buttons to the frame and give each button an ActionListener that invokes the relevant media player command. As an example, this notional Rewind button would "skip backwards 10 seconds (-10,000 milliseconds)."
JPanel controlsPane = new JPanel();
JButton rewindButton = new JButton("Rewind");
rewindButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
mediaPlayerComponent.getMediaPlayer().skip(-10000);
}
});
controlsPane.add(rewindButton);
frame.add(controlsPane, BorderLayout.SOUTH);
The software design is up to you, but you should at least be aware of
JToolBar, seen here and here.
Action, seen here and cited here.
Timer, seen here as a way to repeat an Action.
All right, guys. I’ve figured out how to do it. I’m not sure how it was done in the official Vlcj project but I’ve figured out my own simple way by learning from the official project.
It just takes a few lines of code. It’s very simple.
These are the steps you have to follow:
Create a JSlider.
To the JSlider, add a mouseMotionListener (‘mouseDragged’ to be exact).
Inside that put in the code which would update the video position based on
the change in the JSlider.
Create a Timer.
Put the code inside it to set the value of the JSlider based on the position
of the video.
And that’s it!
This is the code. It comes inside the initialize() method which you can see in the code I’ve given in the question. (And of course you'll also have to create the JSlider and add it to the panel. I haven't shown the code since it's simple.)
js.addMouseMotionListener(new MouseMotionAdapter() {
#Override
public void mouseDragged(MouseEvent e) {
if (js.getValue() / 100 < 1) {
emp.setPosition((float) js.getValue() / 100);
}
}
});
Timer timer = new Timer(100, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
js.setValue(Math.round(emp.getPosition() * 100));
}
});
timer.start();
Some explanation.
The value you get when you use emp.getPosition always seems to be in decimals. It’s something like 0.1334344 at the start of the video and it’s something like 0.998988 at the end. But the value of JSlider is in int. From 0 to 100. So in the mouseMotionListener added to the JSlider I’ve converted the int value of the JSlider to float by dividing it by 100.
And in the action listener inside the timer I’ve multiplied the value of the video position by 100 and then rounded it off to make it an int value. So that value can be set in the JSlider to make it move in sync with the video.
I’m sure the code is rudimentary and there could be some best practices which I may not have followed. Sorry about that but I’m just getting into java by learning the stuff which I find interesting. Those who are good at java and have used such code in an actual project can comment below with how it can be improved.
I have a JFrame application working nicely. However now I'd like to run it on the web as an Applet. This is what I've done:
import MyPackage.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
public class MyName extends JApplet
{
public void init() {
setSize(600,450);
new MyName()
}
public MyName() {
JShellFrame frame = new JShellFrame(true, null, null);
frame.setVisible(true);
}
}
How can I make an html file to run this applet? Also, I have an external jar file that the applet will need. Does the applet not need a main method?
Check out Getting Started With Applets. It covers relevant methods and life cycle. It mentions main method as well:
Unlike Java applications, applets do not need to implement a main
method.
Deployment section covers HTML file details. For dependency jars you can specify more than one jar in archive attribute of applet tag.
However now I'd like to run it on the web..
Then drop this nonsense and launch the frame from a link using Java Web Start. I say 'nonsense' for two reasons.
JWS has existed since Java 1.2, & has been discussed in these forums several times in the last few days in regard to applets. Seems you are not doing much research.
Of the 'gargantuan' 2 lines of applet code code shown above, one of them is ill-advised and the other is either pointless or would risk creating a stack overflow error (could not be bothered trying it to find out which).
here's some html that will work:
<applet archive = "appName.jar" width = 900 height = 506 code = "main.class"/>
You can change the width and height according to the size your app needs. Also make sure to put the url as a path to the jar.
Ok so i am trying to get a 3 JPanel JFrame where right and left panel have a fixed width but are vertically re-sizable and a center panel that can be re-sized both horizontally and vertically.
Since standard LayoutManagers are terrible and simply annoying i have been told that the industry standard and easiest to work whit and handle is JGoodies. However seems that a lot of link on JGoodies website are dead regarding their examples / tutorials there is a 400 page PDF i dont want to read.
Anyhow i have started implementing FormLayout to my first UI_View and i ran in to a problem
package ppe.view;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import com.jgoodies.forms.layout.*;
public class UI_View extends JFrame
{
private JScrollPane right = new JScrollPane();
private JList browse = new JList();
public UI_View()
{
this.setTitle("Prototype MVC Arhitecture");
this.setMinimumSize(new Dimension(800, 600));
this.setExtendedState(this.MAXIMIZED_BOTH);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
FormLayout layout = new FormLayout("right:pref, 7dlu","p, 1dlu");
layout.setColumnGroups(new int [][]{{1}});
JPanel content = new JPanel(layout);
CellConstraints c = new CellConstraints();
right.add(browse);
content.add(right, c.xy(1, 1));
this.add(content);
}
public static void main(String[] args)
{
new UI_View().setVisible(true);
}
}
You a missing a Jar file. JGoodies has several Jar files, make sure you have the ones you need.
MiG Layout is the best layout manager I've used, but I like JGoodies for its Binding and Validation libraries. You can find tutorial code samples in the older versions in the download archive.
What also might make your life easier is to use Eclipse with the WindowBuilder plugin. It's a layout tool that supports FormLayout.
I think I need to restate my question ...
I want to create a SIMPLE form application that edits certain areas of one very specific text file. Though I have some web development experience, I do not want to create a browser based application. Basically I want to give a Desktop application a try and I am looking for some help to get started including suggestions for the language of choice. The application should run on Mac OS X. Besides there's no restriction: Java, Cocoa, Python, even a some interactive shell script would be ok.
If you are interested in the details, continue to read here, but not that my question is not LaTex specific...:
I have an automatically generated report file that contains LaTex Code. Now I want to build a little application that creates a form field for every section and it's header. The document contains only a few hundred lines and the should work the following:
\section{ This is were the header text should go inside the document }
\normalsize{ This is where the normal text should go}
The header / normalsize pairs occur 5-6 times within the document. All I want is a little GUI that allows user to edit between the curly braces without seeing any TeX code. I know that there's LyX and other WYSIWYG approaches to LaTeX – I do not want to reinvent the wheel. I just want to protect the auto-generated code a litte from users and make it a little more comfortable to them.
EDIT:
here's my very first try. I guess I should use PlainDocument instead of directly sending it, but I´ll figure that out, since I got plenty of help from trashgod with the editor / Text Component links. The major problem is to single out the content from \section{} and \normalsize{} stuff. Probably I will some regexp here. I need to get a new text area for every appearance.
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.FileReader;
import javax.swing.*;
public class basicFrame extends JFrame {
// Declare Variables
JScrollPane bildlauf = new JScrollPane();
JTextArea txtfeld = new JTextArea();
public basicFrame() {
super();
// Main window
setTitle("ReportEditor");
setBackground(Color.LIGHT_GRAY);
// components
try {
File datei = new File("report.Rnw");
FileReader in = new FileReader(datei);
txtfeld.read(in, datei);
in.close();
} catch (Exception e) {
System.out.println("Error !");
}
// setLayout(new GridLayout(2,2));
// Scroll Shizzle
bildlauf.getViewport().add(txtfeld, null);
getContentPane().add(bildlauf, BorderLayout.CENTER);
//txtfeld.setSize(500,680);
//add(txtfeld);
//this.getContentPane().add(txtfeld);
// close
addWindowListener(new WindowLauscher());
}
// event handlers...
protected static final class WindowLauscher extends WindowAdapter {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
public static void main(String[] args) {
//Fesnter erzeugen und anzeigen, die main Sache halt
basicFrame hf = new basicFrame();
hf.setSize(500, 700);
hf.setLocation(100, 100);
hf.setVisible(true);
}
}
Thx in advance for any suggestions!
The TeX on Mac OS X wiki recommends jEdit, which supports plugins. LaTeXTools might be a good start.
Addendum:
I do not want to reinvent the wheel.
All I want to do is create a form application.
Although these goals are somewhat contradictory, you can always parse the file and use a suitable JTextComponent for each editable section. Here's an overview of Using Text Components; see Text Component Features if you want to create your own editor kit, as discussed in Customizing a Text Editor.
Addendum: In addition to the tutorial, you might look at this text field layout example. Also, consider a two-column JTable, which would allow you to cleanly separate your document model from your form view.
Addendum: A few notes on your code.
Class names are usually capitalized, e.g. BasicFrame.
Don't extend JFrame if you're not changing it's behavior; JPanel is a good container for other components, and it can be displayed in a JFrame.
Always buid your GUI on the EDT.
Keep you view and model separate.