Javax Media Player-the Play

发布于 2025-01-29 15:18:32 字数 2792 浏览 3 评论 0原文

试图将.mp4视频嵌入jpanel。 jpanel将加载和执行,但视频不会。尝试{}会引发错误,但不确定为什么尝试{}会抛出错误。从另一个来源复制此代码,但不会编译。

import java.awt.BorderLayout;    
import java.awt.Component;    
import java.io.File;    
import java.net.MalformedURLException;    
import java.net.URL;    
import javax.media.Manager;    
import javax.media.MediaLocator;    
import javax.media.Player;    
import javax.swing.JFrame;    
import javax.swing.JPanel;    
public class Main extends JPanel 
{    

    public static void main(String[] args) 
    {

        URL mediaUrl=null;    
        File file = new File("Mortal Combat Video.mp4");     
        System.out.println(file);
        try 
        {    
            mediaUrl = file.toURL(); 
        } 
        catch (MalformedURLException ex) 
        {
            System.out.println(ex);    
        }
    
        JFrame mediaTest = new JFrame( "Movie Player" );    
        mediaTest.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );    
        MediaPlayer mediaPanel = new MediaPlayer( mediaUrl );
        System.out.println(mediaPanel);
        mediaTest.add( mediaPanel );
        
        mediaTest.setSize( 800, 700 ); // set the size of the player    
        mediaTest.setLocationRelativeTo(null);    
        mediaTest.setVisible( true );
    }    
}
import java.awt.BorderLayout;    
import java.awt.Component;    
import java.io.File;    
import java.net.MalformedURLException;    
import java.net.URL;    
import javax.media.Manager;    
import javax.media.MediaLocator;    
import javax.media.Player;    
import javax.swing.JFrame;    
import javax.swing.JPanel;    
public class MediaPlayer extends JPanel {    
public MediaPlayer(URL mediauUrl) {

setLayout(new BorderLayout());    

    Player mediaPlayer = null;
    Component video = null;
    
    Component control = null;
try 
    {

        mediaPlayer=Manager.createRealizedPlayer(new MediaLocator(mediauUrl));          
        video=mediaPlayer.getVisualComponent(); 
        control=mediaPlayer.getControlPanelComponent();
        System.out.println("here3");
        if (video == null)
            System.out.println("video");
        else
        {
            System.out.println("video2");
            add(video, BorderLayout.CENTER); // place the video component in the panel    
        }
        
        add(control, BorderLayout.SOUTH); // place the control in panel    
        mediaPlayer.start();    
    } 
    catch (Exception e) 
    {    
        System.out.println("error");
    }    
}
}

捕获{}打印“错误”,但不确定为什么尝试{}代码不会编译。任何建议将不胜感激。

视频播放,控制功能,它现在正在运行,但视频本身并不可见(音频播放)。 这是屏幕截图:

Trying to embed a .mp4 video in a JPanel. JPanel will load and execute but video does not. The try{} throws an error but not sure why the try{} is throwing the error. Copied this code from another source but it will not compile.

import java.awt.BorderLayout;    
import java.awt.Component;    
import java.io.File;    
import java.net.MalformedURLException;    
import java.net.URL;    
import javax.media.Manager;    
import javax.media.MediaLocator;    
import javax.media.Player;    
import javax.swing.JFrame;    
import javax.swing.JPanel;    
public class Main extends JPanel 
{    

    public static void main(String[] args) 
    {

        URL mediaUrl=null;    
        File file = new File("Mortal Combat Video.mp4");     
        System.out.println(file);
        try 
        {    
            mediaUrl = file.toURL(); 
        } 
        catch (MalformedURLException ex) 
        {
            System.out.println(ex);    
        }
    
        JFrame mediaTest = new JFrame( "Movie Player" );    
        mediaTest.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );    
        MediaPlayer mediaPanel = new MediaPlayer( mediaUrl );
        System.out.println(mediaPanel);
        mediaTest.add( mediaPanel );
        
        mediaTest.setSize( 800, 700 ); // set the size of the player    
        mediaTest.setLocationRelativeTo(null);    
        mediaTest.setVisible( true );
    }    
}
import java.awt.BorderLayout;    
import java.awt.Component;    
import java.io.File;    
import java.net.MalformedURLException;    
import java.net.URL;    
import javax.media.Manager;    
import javax.media.MediaLocator;    
import javax.media.Player;    
import javax.swing.JFrame;    
import javax.swing.JPanel;    
public class MediaPlayer extends JPanel {    
public MediaPlayer(URL mediauUrl) {

setLayout(new BorderLayout());    

    Player mediaPlayer = null;
    Component video = null;
    
    Component control = null;
try 
    {

        mediaPlayer=Manager.createRealizedPlayer(new MediaLocator(mediauUrl));          
        video=mediaPlayer.getVisualComponent(); 
        control=mediaPlayer.getControlPanelComponent();
        System.out.println("here3");
        if (video == null)
            System.out.println("video");
        else
        {
            System.out.println("video2");
            add(video, BorderLayout.CENTER); // place the video component in the panel    
        }
        
        add(control, BorderLayout.SOUTH); // place the control in panel    
        mediaPlayer.start();    
    } 
    catch (Exception e) 
    {    
        System.out.println("error");
    }    
}
}

The catch{} prints "error" but not sure why the try{} code will not compile. Any advice is appreciated.

Its running now, video plays, controls work but the video itself is not visible (audio plays).
Here's screenshot:
enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文