1

I am trying to play a video using jmf. After working hard for hours literally removing all errors and exceptions, Here I am getting a null pointer exception. Here I figured out after looking on NullPointerException, this should be due to incorrect declaration of Player mediaPlayer where it is not initialised to anything.

Another problem with directly initialising it to what values I am giving it later is, I have to catch the exceptions too, then it says player might not be declared.

How do I declare mediaURL and Player so that this nullpointerexception is removed and I can play this video.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.Graphics;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.media.*;
import java.net.URL;
import java.io.*;
import java.net.MalformedURLException;


    public class mediaPlayer extends JFrame
    {

    URL mediaURL;
    Player mediaPlayer;
        public mediaPlayer()
        {   
            JFrame f = new JFrame("new");
            f.setLayout(new BorderLayout());
            f.setSize(500,300);
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            //file you want to play
            try{
            String name = "file:///"+new File("output.mp4").getAbsolutePath();
                mediaURL = new URL(name);
            }catch (MalformedURLException ex){}
            //create the media player with the media url
            try{
                mediaPlayer = Manager.createRealizedPlayer(mediaURL);
            }catch(IOException ex){} catch(NoPlayerException ex){} catch(CannotRealizeException ex){}
            //get components for video and playback controls
            Component video = mediaPlayer.getVisualComponent();
            Component controls = mediaPlayer.getControlPanelComponent();
            add(video,BorderLayout.CENTER);
            add(controls,BorderLayout.SOUTH);
            f.setVisible(true);

        }
        public static void main(String[] args){
            new mediaPlayer();
        }
    }

Update2

javax.media.NotRealizedError: Cannot get visual component on an unrealized playe
r
        at com.sun.media.BasicPlayer.getVisualComponent(BasicPlayer.java:491)
        at com.sun.media.MediaPlayer.getVisualComponent(MediaPlayer.java:48)
        at mediaPlayer.<init>(mediaPlayer.java:29)
        at mediaPlayer.main(mediaPlayer.java:38)
Exception in thread "main" javax.media.NotRealizedError: Cannot get visual compo
nent on an unrealized player
        at com.sun.media.BasicPlayer.getVisualComponent(BasicPlayer.java:491)
        at com.sun.media.MediaPlayer.getVisualComponent(MediaPlayer.java:48)
        at mediaPlayer.<init>(mediaPlayer.java:29)
        at mediaPlayer.main(mediaPlayer.java:38)

Please help me in playing this video, removing this nullpointer exception. Thanks a lot to everyone who puts any effort on my problem:).

4

3 回答 3

0

Player 是一个接口,因此您无法创建它的对象。而是使用 Manager.createPlayer(source) 方法让您的工作。这是链接,链接

于 2013-07-27T12:19:31.437 回答
0

将您的代码更改为此,

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.Graphics;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.media.*;
import java.net.URL;
import java.io.*;
import java.net.MalformedURLException;


    public class mediaPlayer extends JFrame
    {
        public mediaPlayer()
        {   
        JFrame f = new JFrame("Video Demo");
            f.setLayout(new BorderLayout());
            f.setSize(500,300);
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            //file you want to play
            try{
                String name = "file:///"+new File("output.mp4").getAbsolutePath();
                URL mediaURL = new URL(name);
                Player mediaPlayer = Manager.createRealizedPlayer(mediaURL);
            //get components for video and playback controls
            Component video = mediaPlayer.getVisualComponent();
            Component controls = mediaPlayer.getControlPanelComponent();
            f.add(video,BorderLayout.CENTER);
            f.add(controls,BorderLayout.SOUTH);
            } catch (Exception e) { e.printStackTrace(); }
            f.setVisible(true);

        }
        public static void main(String[] args){
            new mediaPlayer();
        }
    }

学分 - 安德鲁汤普森。

于 2013-07-27T12:24:40.920 回答
0

如果您查看JMF 2.1.1 - Supported Formats页面,您会注意到明显没有提及 MP4。从理论上讲,解决问题就像获取 MP4 文件的服务提供者接口并将其添加到运行时类路径一样“简单”。

底线是,如果这个项目是为了播放有限的一组格式,或者是内置的,或者我们可以提供一个 SPI,那应该没问题。OTOH JMF适合作为“通用播放器”。它太旧了,支持的格式太少。

于 2013-07-27T12:46:02.773 回答