Вы находитесь на странице: 1из 30

15/1/2014

How to play audio and video files in Java applications | BUDDHIMA'S COMPUTER LAB

All About My Experiments

HOME

POSTS

ABOUT WRITER

BUDDHIMA'S COMPUTER LAB


How to play audio and video files in Java applications
May 1, 2011 by Buddhima Wijeweera | 86 Comments

AB OUT ME

Introduction
In this post Im going to show you how to play an audio and a video file using a java application. At first I have to mention that Im using Java Media Framework (JMF) for this purpose. So you have to download and install JMF from here. According to the web page you can, Playback audios and videos Capture from devices Transmitt over an internet connection JMF is providing you necessary classes and you have to do less. But in here Ive to mention that JMF is a bit old too. In many cases you wont be able to play every media file you want. Because there are some restrictions. Through following examples Ill show you how to get the use of JMF.

Im a final year undergraduate student in Department of Computer Science and Engineering, at University of Moratuwa, Sri Lanka. I have already did contributions to Joomla! project and Apache Synapse project. I'm interest to learn and explore computer related stuffs.

LA TEST PO STS My Industrial Training Period Parsing HTML pages with Jsoup Using webcam in C# applications Creating a Web or a Mobile Application with REST API using IXAPI Using OpenStreetMaps DB Report Mediator in WSO2 ESB Basic Alert System With WSO2 ESB Normalizer Message Transformation Pattern With WSO2 ESB
1/30

Creating an audio player


Before going to build an audio player you have to be familier with the classes and their methods that are going to use. Player: As the name says Player is responsible for playing the given file. It has start(), stop(), close() methods. Player also have number of states too.

file:///E:/Advanced%20Java/Java%20Multimedia/How%20to%20play%20audio%20and%20video%20files%20in%20Java%20applications%20%20%20BUDDHI

15/1/2014

How to play audio and video files in Java applications | BUDDHIMA'S COMPUTER LAB

Prefetched Prefetching Realized Realizing Started Unrealized

Connect WSO2 ESB with WSO2 MB Implementing Resequencer For Apache Synapese Graceful Start For WSO2 ESB Java web services and Android clients II Java web services and Android clients I Automated programs in Java 2011 in review How to create a custom window Speaking and Listening Applications Using webcam with a Java application How to play audio and video files in Java applications

Manager: Manager is responsible for creating appropriate player object using an URL or a Media Locator. Manager is also use to create a processor will be explained later. SimpleAudioPlayer.java

Execute DOS commands in Java

BL OG P OSTS September 2013 (1)

1 2 3 4 5 6 7 8 9 1 0 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8

i m p o r tj a v a . i o . F i l e ; i m p o r tj a v a . n e t . M a l f o r m e d U R L E x c e p t i o n ; i m p o r tj a v a . n e t . U R L ; i m p o r tj a v a x . m e d i a . * ; / /i m p o r tJ M Fc l a s s e s i m p o r tj a v a x . s w i n g . J F i l e C h o o s e r ; / * * * *@ a u t h o rB U D D H I M A * / p u b l i cc l a s sS i m p l e A u d i o P l a y e r{ p r i v a t eP l a y e ra u d i o P l a y e r=n u l l ; p u b l i cS i m p l e A u d i o P l a y e r ( U R Lu r l ){ t r y{

May 2013 (1) February 2013 (2) January 2013 (1) December 2012 (1) November 2012 (1) October 2012 (2) September 2012 (2) May 2012 (2) April 2012 (1) January 2012 (1) December 2011 (1) October 2011 (1)
2/30

file:///E:/Advanced%20Java/Java%20Multimedia/How%20to%20play%20audio%20and%20video%20files%20in%20Java%20applications%20%20%20BUDDHI

15/1/2014

How to play audio and video files in Java applications | BUDDHIMA'S COMPUTER LAB

1 9 2 0 2 1 2 2 2 3 2 4 2 5 2 6 2 7 2 8 2 9 3 0 3 1 3 2 3 3 3 4 3 5 3 6 3 7 3 8 3 9 4 0 4 1 4 2 4 3 4 4 4 5 4 6 4 7 4 8 4 9 5 0 5 1 5 2 5 3 5 4 5 5 5 6 5 7 5 8 5 9 6 0 6 1 6 2 6 3 6 4 6 5 6 6 6 7 6 8 6 9 7 0 7 1 7 2 7 3 7 4 7 5

May 2011 (2)

/ / M e d i a L o c a t o rm l = n e wM e d i a L o c a t o r ( u r l ) ; a u d i o P l a y e r=M a n a g e r . c r e a t e P l a y e r ( u r l ) ; }c a t c h( E x c e p t i o ne x ){ S y s t e m . o u t . p r i n t l n ( e x ) ; } }

April 2011 (2) March 2011 (1) February 2011 (1) January 2011 (4)

EM AI L SU BSCR I PTI ON Enter your email address to subscribe to this blog and receive notifications of new posts by email. Join 30 other followers Sign me up!

p u b l i cS i m p l e A u d i o P l a y e r ( F i l ef i l e )t h r o w sM a l f o r m e d U R L E x c e p t i o n{ t h i s ( f i l e . t o U R I ( ) . t o U R L ( ) ) ; } p u b l i cv o i dp l a y ( ){ a u d i o P l a y e r . s t a r t ( ) ;/ /s t a r tp l a y i n g }
Follow

Follow BUDDHIMA'S p u b l i cv o i ds t o p ( ) { COMPUTER LAB


Get every new post a u d i o P l a y e r . s t o p ( ) ; / / s t o pp l a y i n g delivered to your Inbox. a u d i o P l a y e r . c l o s e ( ) ; }
Join 30 other followers

BL OG C ALE NDAR May 2011 M T W T 2 3 4 5 F 6 S 7 S 1 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 Apr Oct

Enter your email address p u b l i cs t a t i cv o i dm a i n ( S t r i n g [ ]a r g s ){ t r y{


Sign me up

/ /T O D Oc o d ea p p l i c a t i o nl o g i ch e r e Pow ered by WordPress.com J F i l e C h o o s e rf c=n e wJ F i l e C h o o s e r ( ) ; f c . s h o w O p e n D i a l o g ( n u l l ) ; F i l ef i l e=f c . g e t S e l e c t e d F i l e ( ) ;

CA TAGO RI E S

S i m p l e A d u i o P l a y e rs a p=n e wS i m p l e A d u i o P l a y e r ( f i l e ) ; C Sharp ESB s a p . p l a y ( ) ; / / s a p . s t o p ( ) ; }c a t c h( M a l f o r m e d U R L E x c e p t i o ne x ){ S y s t e m . o u t . p r i n t l n ( e x ) ; }


BL OG S TATS

Java

Ubuntu
Uncategorized Web Applications

Windows WSO2

file:///E:/Advanced%20Java/Java%20Multimedia/How%20to%20play%20audio%20and%20video%20files%20in%20Java%20applications%20%20%20BUDDHI

3/30

15/1/2014

How to play audio and video files in Java applications | BUDDHIMA'S COMPUTER LAB

7 6 7 7 7 8

} }

40,036 hits

With this example you can play MP3, WAV, and AU files.

VI SI T SRI LANKA

Creating a video player


Not like audio player, Video player deals with GUI. Therefore you should have a little knowledge about Swing components. In addition to previous things you have to get Visual component and Control Panel component from the Player instance created by you. Then you have to place those in a necessary Swing component (here JPanel is used). MediaPlayer.java
NetworkedBlogs Blog: BUDDHIMA'S COMPUTER LAB Topics: computer engineering, ubuntu, windows
Follow my blog

1 2 3 4 5 6 7 8 9 1 0 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 2 0 2 1 2 2 2 3 2 4 2 5 2 6 2 7 2 8 2 9 3 0 3 1 3 2

i m p o r tj a v a . a w t . B o r d e r L a y o u t ; i m p o r tj a v a . a w t . C o m p o n e n t ; i m p o r tj a v a . n e t . M a l f o r m e d U R L E x c e p t i o n ; i m p o r tj a v a . n e t . U R L ; i m p o r tj a v a x . m e d i a . M a n a g e r ; i m p o r tj a v a x . m e d i a . M e d i a L o c a t o r ; i m p o r tj a v a x . m e d i a . P l a y e r ; i m p o r tj a v a x . s w i n g . J F i l e C h o o s e r ; i m p o r tj a v a x . s w i n g . J F r a m e ; / * * * *@ a u t h o rB U D D H I M A * / p u b l i cc l a s sM e d i a P l a y e re x t e n d sj a v a x . s w i n g . J P a n e l{ p u b l i cM e d i a P l a y e r ( U R Lm e d i a u U r l ){ i n i t C o m p o n e n t s ( ) ; s e t L a y o u t ( n e wB o r d e r L a y o u t ( ) ) ; t r y{ P l a y e rm e d i a P l a y e r = M a n a g e r . c r e a t e R e a l i z e d P l a y e r ( n e w C o m p o n e n tv i d e o = m e d i a P l a y e r . g e t V i s u a l C o m p o n e n t ( ) ;

C o m p o n e n tc o n t r o l = m e d i a P l a y e r . g e t C o n t r o l P a n e l C o m p o n e n t ( ) ; i f( v i d e o ! = n u l l ){
4/30

file:///E:/Advanced%20Java/Java%20Multimedia/How%20to%20play%20audio%20and%20video%20files%20in%20Java%20applications%20%20%20BUDDHI

15/1/2014

How to play audio and video files in Java applications | BUDDHIMA'S COMPUTER LAB

3 3 3 4 3 5 3 6 3 7 3 8 3 9 4 0 4 1 4 2 4 3 4 4 4 5 4 6 4 7 4 8 4 9 5 0 5 1 5 2 5 3 5 4 5 5 5 6 5 7 5 8 5 9 6 0 6 1 6 2 6 3 6 4 6 5 6 6 6 7 6 8 6 9 7 0 7 1 7 2 7 3 7 4 7 5 7 6 7 7 7 8 7 9 8 0 8 1 8 2

a d d ( v i d e o ,B o r d e r L a y o u t . C E N T E R ) ; } a d d ( c o n t r o l ,B o r d e r L a y o u t . S O U T H ) ; m e d i a P l a y e r . s t a r t ( ) ; }c a t c h( E x c e p t i o ne ){ } } p u b l i cs t a t i cv o i dm a i n ( S t r i n g [ ]a r g s ){

/ /p l a c et h ev i d e oc o m p o n e n ti nt h ep a n

/ /p l a c et h ec o n t r o li n p a n e l

J F i l e C h o o s e rf i l e C h o o s e r=n e wJ F i l e C h o o s e r ( ) ; f i l e C h o o s e r . s h o w O p e n D i a l o g ( n u l l ) ; U R Lm e d i a U r l = n u l l ; t r y{ m e d i a U r l=f i l e C h o o s e r . g e t S e l e c t e d F i l e ( ) . t o U R I ( ) . t o U R L ( ) ; }c a t c h( M a l f o r m e d U R L E x c e p t i o ne x ){ S y s t e m . o u t . p r i n t l n ( e x ) ; } J F r a m em e d i a T e s t=n e wJ F r a m e (" M o v i eP l a y e r ") ; m e d i a T e s t . s e t D e f a u l t C l o s e O p e r a t i o n (J F r a m e . E X I T _ O N _ C L O S E) ; M e d i a P l a y e rm e d i a P a n e l=n e wM e d i a P l a y e r (m e d i a U r l) ; m e d i a T e s t . a d d (m e d i a P a n e l) ; m e d i a T e s t . s e t S i z e (8 0 0 ,7 0 0) ;/ /s e tt h es i z eo ft h ep l a y e r m e d i a T e s t . s e t L o c a t i o n R e l a t i v e T o ( n u l l ) ; m e d i a T e s t . s e t V i s i b l e (t r u e) ; } }

When you run this code and select a file, you would get something like this. But you can only play limited number of video fromats only (.mpg, some .avi files). Give a try !
file:///E:/Advanced%20Java/Java%20Multimedia/How%20to%20play%20audio%20and%20video%20files%20in%20Java%20applications%20%20%20BUDDHI 5/30

15/1/2014

How to play audio and video files in Java applications | BUDDHIMA'S COMPUTER LAB

Willing to tell more intractive activities using JMF

Resources:
1. JMF official site : http://www.oracle.com/technetwork/java/javase/tech/index-jsp140239.html 2. JMF examples : http://blogs.sun.com/geertjan/date/20070121
About these ads

Like
One blogger likes this.

Re late d

Using webcam in C# a

Java web services and

Java web services and


6/30

file:///E:/Advanced%20Java/Java%20Multimedia/How%20to%20play%20audio%20and%20video%20files%20in%20Java%20applications%20%20%20BUDDHI

15/1/2014

How to play audio and video files in Java applications | BUDDHIMA'S COMPUTER LAB

In "C Sharp"

In "Java"

In "Java"

Categories: Java , Ubuntu, Windows | Tags: buddhima , java , jmf, media , player, wijeweera | Permalink.

Author: Buddhima Wijeweera I'm an undergraduate student in University of Moratuwa. I'm studying in the computer science and engineering field.I like to explore the world of computers in different angles.

8 6 T H OU GH T S ON H OW T O P L A Y A U DI O A ND V I D EO F I L ES I N J A V A A P P L I CA T I ONS
Leave a comment

chandu
February 3, 2012 at 2:24 PM

thanks for giving valuable information


Reply

Buddhima Wijeweera
February 3, 2012 at 4:41 PM

You are welcome !


Reply

Saleem
April 8, 2012 at 10:42 PM

please did you know how to do steganography on video?


file:///E:/Advanced%20Java/Java%20Multimedia/How%20to%20play%20audio%20and%20video%20files%20in%20Java%20applications%20%20%20BUDDHI 7/30

15/1/2014

How to play audio and video files in Java applications | BUDDHIMA'S COMPUTER LAB

Buddhima Wijeweera
May 14, 2012 at 1:15 PM

Hi Saleem, Im not aware of doing video steganography using JMF. (But there might be a way). Through searching I found following programs will do that: http://compression.ru/video/stego_video/index_en.html (click .exe program link). Sorry for being late as I was busy with academics.

saleem
May 16, 2012 at 5:58 PM

Hey I need help on how to read and save a video file just like in images I used buffered reader. please I need help on that as it is very urgent.

gautham
April 6, 2012 at 12:02 PM

This code is not working can u help me out


Reply

Buddhima Wijeweera
April 6, 2012 at 8:48 PM

Hi gautham, Need more information. Also please refer the references given at the end. This worked fine on my machine.
Reply

Saleem
May 14, 2012 at 1:36 PM

hey please I also have problem in the video but the audio works will here is what I am experiencing (Exception in
file:///E:/Advanced%20Java/Java%20Multimedia/How%20to%20play%20audio%20and%20video%20files%20in%20Java%20applications%20%20%20BUDDHI 8/30

15/1/2014

How to play audio and video files in Java applications | BUDDHIMA'S COMPUTER LAB

thread main java.lang.Error: Unresolved compilation problem: The method initComponents() is undefined for the type MediaPlayer at MediaPlayer.(MediaPlayer.java:16) at MediaPlayer.main(MediaPlayer.java:66) )
Reply

janak kumar lal


July 14, 2012 at 5:17 PM

simple question..how do i add jmf to the eclipse or the package to run your codes. i have downloaded jmf and installed itbut dont know how to use it. Your code doesnt work when i write them in eclipse as it dont have javax.media
Reply

Buddhima Wijeweera
July 15, 2012 at 8:46 PM

Hi Janak, You can add libraries (jar files) that are placed where you installed JMF, to eclipse project. Then refresh the project.. Thats it !
Reply

Satish Verma
April 3, 2013 at 11:24 AM

hiiii,, can u little ellaborate about jmf for running video file .we have to install jmf or plugin?

file:///E:/Advanced%20Java/Java%20Multimedia/How%20to%20play%20audio%20and%20video%20files%20in%20Java%20applications%20%20%20BUDDHI

9/30

15/1/2014

How to play audio and video files in Java applications | BUDDHIMA'S COMPUTER LAB

Buddhima Wijeweera
April 3, 2013 at 11:34 AM

Hi Satish, For the application to run a video file, you need to install JMF in your PC. Link to download it is provided in the article. Then you need to add the libraries in that JMF to the project libraries. After that you will be able to import javax.media.*;. Theres no plugin involved in this process. Hope this small guidance will help you

Kaledawit
July 16, 2012 at 7:25 PM

please help me i have a project to transmit audio and video via LAN using java can you help me to give a code ??? Thanks!!!!
Reply

Buddhima Wijeweera
July 16, 2012 at 8:11 PM

Kaledawit, Could you explain where to transmit, restrictions and more details.
Reply

Priti
July 27, 2012 at 1:59 PM

unable to open the .avi files


Reply

file:///E:/Advanced%20Java/Java%20Multimedia/How%20to%20play%20audio%20and%20video%20files%20in%20Java%20applications%20%20%20BUDDH

10/30

15/1/2014

How to play audio and video files in Java applications | BUDDHIMA'S COMPUTER LAB

Buddhima Wijeweera
July 27, 2012 at 7:27 PM

Hi, There may be unsupported versions of .avi (http://www.pavtube.com/avi/) . Could you convert it to another version and give a try. I think it will work.
Reply

kishna
August 27, 2012 at 5:27 PM

thanks for giving solution


Reply

Buddhima Wijeweera
August 27, 2012 at 5:32 PM

You are welcome !!


Reply

sam
September 1, 2012 at 11:45 AM

hey.. import javax.media.Manager; import javax.media.MediaLocator; import javax.media.Player; javax is not found. why? can you help?
Reply

Buddhima Wijeweera
September 1, 2012 at 6:35 PM

Hi,
file:///E:/Advanced%20Java/Java%20Multimedia/How%20to%20play%20audio%20and%20video%20files%20in%20Java%20applications%20%20%20BUDDH 11/30

15/1/2014

How to play audio and video files in Java applications | BUDDHIMA'S COMPUTER LAB

You have not added JMF libraries to the project. After adding those libraries you wont get an error.
Reply

Ranjithkumar
September 17, 2012 at 1:54 PM

hai this is ranjith . i need a program for video compression using java.. can u help me?
Reply

Buddhima Wijeweera
September 18, 2012 at 1:47 PM

Hi Ranjith, Currently Im overloaded with projects, so sadly I wont be able to work on your idea. You could have a look at following links to have an idea : 1. http://www.cs.odu.edu/~cs778/jeffay/Lecture4.pdf 2. http://code.google.com/p/kanzi/wiki/Overview
Reply

Ranjithkumar
September 21, 2012 at 2:56 PM

Thank you.
Reply

gani
September 23, 2012 at 2:43 PM

hi ,i got an error tht unable to handle xvid format


Reply

Buddhima Wijeweera
September 23, 2012 at 4:50 PM

Hi Gani, You can have a better understanding on formats that


file:///E:/Advanced%20Java/Java%20Multimedia/How%20to%20play%20audio%20and%20video%20files%20in%20Java%20applications%20%20%20BUDDH 12/30

15/1/2014

How to play audio and video files in Java applications | BUDDHIMA'S COMPUTER LAB

JMF supports through this: http://www.oracle.com/technetwork/java/javase/formats138492.html You may need to convert your video in to a format that JMF support or you have to integrate following plugin to the JMF framework http://jffmpeg.sourceforge.net/ According to documents it should support xvid format http://jffmpeg.sourceforge.net/formats.html Hope you get the solution
Reply

gani
September 23, 2012 at 11:01 PM

hi ,i am getting audio only not video,what is the use of jmf customixer.exe,jmf registry.exe,jmfstudio.exe.
Reply

Buddhima Wijeweera
September 24, 2012 at 9:36 AM

Hi, You can refer following link to get solutions for your confusions. http://www.oracle.com/technetwork/java/javase/documentation138769.html
Reply

Ranjithkumar
September 25, 2012 at 12:39 PM

Hai did you know how convert video into images in java? If you know please tell me .
Reply

file:///E:/Advanced%20Java/Java%20Multimedia/How%20to%20play%20audio%20and%20video%20files%20in%20Java%20applications%20%20%20BUDDH

13/30

15/1/2014

How to play audio and video files in Java applications | BUDDHIMA'S COMPUTER LAB

Buddhima Wijeweera
September 25, 2012 at 10:43 PM

Hi, You can do that as JMF grab frames from webcam (grabFrame()). Following link will help you. http://www.coderanch.com/t/273577/java/java/image-video-file
Reply

praneeth
September 26, 2012 at 9:57 AM

hey how to enable features like play next, previous,forward,rewind and shuffle?

Buddhima Wijeweera
September 26, 2012 at 10:29 AM

Hi Praneeth, Could you have a look at the following link pls. Link: http://www2.syscon.com/ITSG/virtualcd/Java/archives/0502/decarmo/index.html

Ranjithkumar
September 26, 2012 at 1:09 PM

Thanks a lot..

anu
October 1, 2012 at 7:48 PM

hi.. when i run the code i got the following Exception Unable to handle format: mpeglayer3, 44100.0 Hz, 16-bit,
file:///E:/Advanced%20Java/Java%20Multimedia/How%20to%20play%20audio%20and%20video%20files%20in%20Java%20applications%20%20%20BUDDH 14/30

15/1/2014

How to play audio and video files in Java applications | BUDDHIMA'S COMPUTER LAB

Stereo, LittleEndian, Signed, 16000.0 frame rate, FrameSize=32768 bits Failed to realize: com.sun.media.PlaybackEngine@6ab7501b Error: Unable to realize com.sun.media.PlaybackEngine@6ab7501b
Reply

Buddhima Wijeweera
October 2, 2012 at 1:04 PM

Hi Anu Smily, I think the problem is with the file you try to play. According to Link http://www.oracle.com/technetwork/java/javase/formats138492.html JMF doesnt support mpeglayer3 (layer1 and 2 does). Refer that link to see supporting formats.

Reply

Meitei
November 25, 2012 at 6:08 PM

Hi, Can you please help me with simply storing a video file over a web service and it being downloaded or played by the client in java. Thanks in advance.
Reply

Buddhima Wijeweera
November 27, 2012 at 11:14 AM

Hi Meitei, I think you need to find about SOAP messages with attachments (or similar area depending on your messaging format). Following links may useful: 1. http://java.boot.by/wsd-guide/ch02s04.html 2. http://www.roseindia.net/webservices/netbeans/SOAPwithAttachmentsAPIforJava.shtml Hope you have some clues now
file:///E:/Advanced%20Java/Java%20Multimedia/How%20to%20play%20audio%20and%20video%20files%20in%20Java%20applications%20%20%20BUDDH 15/30

15/1/2014

How to play audio and video files in Java applications | BUDDHIMA'S COMPUTER LAB

Reply

Meitei
December 18, 2012 at 4:01 PM

Thanx for the reply. SOAP with attachments worked for me

Buddhima Wijeweera
December 18, 2012 at 10:12 PM

You are welcome Meitei !!!

sombir
November 29, 2012 at 7:37 PM

how i can add jmf files in the projects when we dont have jar file for that package?/
Reply

Buddhima Wijeweera
November 29, 2012 at 10:30 PM

Hi Sombir, You can download files from the link given. Link: http://www.oracle.com/technetwork/java/javase/tech/indexjsp-140239.html
Reply

meekness mivanyi
December 7, 2012 at 3:05 PM

help pls and pls im a student in Adamawa State University, Mubi Adamawa State, Nigeria, West Africa force to write a Project On JAVA multimedia Player using JAVA Media Framework(jmf). help pls I DONt KNOW HOW TO GO ABOUT IT EVEN program using JAVA
Reply
file:///E:/Advanced%20Java/Java%20Multimedia/How%20to%20play%20audio%20and%20video%20files%20in%20Java%20applications%20%20%20BUDDH 16/30

15/1/2014

How to play audio and video files in Java applications | BUDDHIMA'S COMPUTER LAB

Buddhima Wijeweera
December 7, 2012 at 8:32 PM

Hi Mivanyi, First you must find someone who familiar with Java and Netbeans (or any-other) IDE. ( since Ill not be available with in next few days). First you need to create a JAVA project and import JMF jars. Then copy paste MediaPlayer code to a newly created file named MediaPlayer.java. If you need more help, feel free to contact
Reply

Atul Patil
December 19, 2012 at 11:16 PM

Buddhima Wijeweera good u r doing greatdo u know anything about jdevelooper


Reply

Buddhima Wijeweera
December 20, 2012 at 9:31 AM

Hi Atul, Thanks !!! I havent worked with JDeveloper IDE yet. But it seems to be the main development platform for the Oracle Fusion Middleware suite of products (main difference compared to other IDEs). You can find more through following links. 1. http://www.oracle.com/technetwork/developertools/jdev/overview/index.html 2. http://docs.oracle.com/cd/E18941_01/tutorials/toc.htm 3. http://docs.oracle.com/cd/E16162_01/user.1112/e17455/working_jdev.htm
Reply
file:///E:/Advanced%20Java/Java%20Multimedia/How%20to%20play%20audio%20and%20video%20files%20in%20Java%20applications%20%20%20BUDDH 17/30

15/1/2014

How to play audio and video files in Java applications | BUDDHIMA'S COMPUTER LAB

Guillaume
February 2, 2013 at 3:27 AM

Hi, thank you for your post. But the method initComponents is not defined. Moreover, some lines are wrong according to the package mediaplayer. The line MediaPlayer mediaPanel = new MediaPlayer( mediaUrl ); must be replaced by: MediaPlayer mediaPanel = new MediaPlayer(); mediaPanel.setCodeBase(mediaUrl) ; Is that really work for you?
Reply

Buddhima Wijeweera
February 2, 2013 at 12:32 PM

Hi Guillaume, This example is working. It seems like you have a confusion with the code. initComponents(); method is an auto generated method by IDE(in here Netbeans) to to call initComponents() in JPanel (the super class of MediaPlayer). In here MediaPlayer is not referring external class. It refers the MediaPlayer class in the sample code. So you can see that constructor of MediaPlayer class in here takes a URL. Hope the explanation will help to solve your confusion

Reply

Nithin
March 12, 2013 at 12:59 PM

hi , could you please tel me where i can pas the video URL(from system) directly in to the program.
Reply

file:///E:/Advanced%20Java/Java%20Multimedia/How%20to%20play%20audio%20and%20video%20files%20in%20Java%20applications%20%20%20BUDDH

18/30

15/1/2014

How to play audio and video files in Java applications | BUDDHIMA'S COMPUTER LAB

Buddhima Wijeweera
March 12, 2013 at 8:25 PM

Hi Nithin, I think you want to get mediaUrl from a file path ? Did you try using, mediaUrl = new File(file_path).toURI().toURL() ;
Reply

shaik
March 14, 2013 at 4:29 PM

i am getting an error at initComponents() can u help me please


Reply

Buddhima Wijeweera
March 14, 2013 at 6:04 PM

Hi Shaik, This method is generated by Netbeans IDE to initialize UI components like frames, panels etc. So make sure there are no issue with your GUI designing.
Reply

mohitmittal
March 26, 2013 at 10:51 AM

i am facing a problem in video player Exception in thread main java.lang.UnsupportedOperationException: Not yet implemented at videoplayer.VideoPlayer.initComponents(VideoPlayer.java:85)
file:///E:/Advanced%20Java/Java%20Multimedia/How%20to%20play%20audio%20and%20video%20files%20in%20Java%20applications%20%20%20BUDDH 19/30

15/1/2014

How to play audio and video files in Java applications | BUDDHIMA'S COMPUTER LAB

at videoplayer.VideoPlayer.(VideoPlayer.java:22) at videoplayer.VideoPlayer.main(VideoPlayer.java:72) Java Result: 1 please solve my problem ..


Reply

Buddhima Wijeweera
March 26, 2013 at 12:10 PM

Hi Mohit, I used Netbeans to create this program. So initComponents() is a method which is auto generated to initialize components in the frame. If you are using another IDE you need to call appropriate methods which does the same thing.
Reply

T Muthulakshmi
April 3, 2013 at 8:35 PM

Thank u I am studying Computer Science & Engineering in Dr. SACOE. I have run mediaplayer.java. but i have this error how to clear it.Pls reply me friend. Unable to handle format: H264, 640360, FrameRate=25.0, Length=691200 0 extra bytes Unable to handle format: mpeglayer3, 44100.0 Hz, 0-bit, Stereo, Unsigned, 3200 0.0 frame rate, FrameSize=9216 bits Failed to realize: com.sun.media.PlaybackEngine@3ae941 Error: Unable to realize com.sun.media.PlaybackEngine@3ae941 Press any key to continue
Reply

Buddhima Wijeweera
April 3, 2013 at 9:10 PM

Hi Muthu,
file:///E:/Advanced%20Java/Java%20Multimedia/How%20to%20play%20audio%20and%20video%20files%20in%20Java%20applications%20%20%20BUDDH 20/30

15/1/2014

How to play audio and video files in Java applications | BUDDHIMA'S COMPUTER LAB

Im so glad to hear that you are also an Computer Science and Engineering undergraduate. It seems like the problem is with the file type you are going to ply in this media player. This player can only play few types of media files (mpg and some avi files). So can you try on such files and let me know the result? Thanks !
Reply

T Muthulakshmi
April 6, 2013 at 10:06 PM

Thank u..

Buddhima Wijeweera
April 6, 2013 at 10:22 PM

You are welcome Muthu !

Igor
April 19, 2013 at 12:22 AM

How I can play streaming video at my system player in full screen. I have URL and I need run some system media player in full screen. Is it possible? Thanks
Reply

Buddhima Wijeweera
April 19, 2013 at 6:56 PM

Hi Igor, You can follow following link to learn about streaming video using JMF. http://bcook.cs.georgiasouthern.edu/cs523/2vandana/index.html To get full screen in Java, you can use setFullScreenWindow() method as given in the following link. http://weblogs.java.net/blog/mkarg/archive/2010/01/03/fullscreenfile:///E:/Advanced%20Java/Java%20Multimedia/How%20to%20play%20audio%20and%20video%20files%20in%20Java%20applications%20%20%20BUDDH 21/30

15/1/2014

How to play audio and video files in Java applications | BUDDHIMA'S COMPUTER LAB

mode-cool Hope those will help you


Reply

muthulakshmi thayumanasundaram
April 23, 2013 at 7:35 PM

my project go well ma.. very thanks to help me .. then another doubt how to capture audio from a particular place using microphone and send it to server in every 15 min
Reply

Buddhima Wijeweera
April 25, 2013 at 8:16 AM

Hi Muthu, Hope you can do capturing part through a looping process. To capture sound Java Sound API may be more suitable.

http://www.java2s.com/Tutorial/Java/0120__Development/CapturingAudiowithJavaSoundAPI.h To send files following links may help you. Actually they show how to send a file from server-to-client. But after get the concept, reverse of that can be applied to your project too. http://stackoverflow.com/questions/4687615/how-toachieve-transfer-file-between-client-and-server-usingjava-socket http://www.rgagnon.com/javadetails/java-0542.html Btw, what king of application are you creating? is the server is a file server (using FTP) ? Techniques may differ depending your application. Thanks !
Reply

sharan
May 7, 2013 at 11:04 AM

when i run this code in netbeans its showing lot of errors ..


Reply
file:///E:/Advanced%20Java/Java%20Multimedia/How%20to%20play%20audio%20and%20video%20files%20in%20Java%20applications%20%20%20BUDDH 22/30

15/1/2014

How to play audio and video files in Java applications | BUDDHIMA'S COMPUTER LAB

Buddhima Wijeweera
May 7, 2013 at 3:19 PM

Hi Sharan, What errors did you get?


Reply

chika
June 2, 2013 at 10:23 PM

Hi Buddhima, I try that code is run very well.. but how to implements that code in jframe (from main class which must changes) and not open directories like that?? and also i try to make my sound looping but failed..
Reply

Buddhima Wijeweera
June 2, 2013 at 11:28 PM

Hi, This code uses a JFrame (you can see it in main method). What you are going to do is not clear. You can cancel JFileChooser, but you need to provide a URL of media inside your code or somehow. Thank You !
Reply

Kylar
June 4, 2013 at 10:20 PM

Very Very Helpful !!! Thank You Very Much !!!


Reply

Buddhima Wijeweera
June 4, 2013 at 10:34 PM

You are welcome Kylar !


Reply
file:///E:/Advanced%20Java/Java%20Multimedia/How%20to%20play%20audio%20and%20video%20files%20in%20Java%20applications%20%20%20BUDDH 23/30

15/1/2014

How to play audio and video files in Java applications | BUDDHIMA'S COMPUTER LAB

kanika23
June 5, 2013 at 4:30 PM

i have written the codes at 2 places but none seems to work 1. netbeans : i wrote the entire code in netbeans for audio and included d jmf library as you told you , the program is complex. no errors.. but certainly i am not able to see any output . nothing is being displayed.. whats the problem?? 2. texteditor : wrote the code in text editor.. compiled it.. but getting error with media package.. i saved my java file in d same jmf/bin but still there is an error. please help me out. i have a macbook pro. !
Reply

Buddhima Wijeweera
June 5, 2013 at 5:46 PM

Hi Kanika, Did you debug your code using Netbeans? Through debugging you will be able to track the fault.

Reply

Pradeep VR (@PVRTweets)
July 17, 2013 at 8:29 PM

Hi Buddhima, Thnx for the article. For me video is not visible, but audio is getting played.
Reply

shwshank
July 24, 2013 at 1:27 AM

its working .THANKS


Reply

file:///E:/Advanced%20Java/Java%20Multimedia/How%20to%20play%20audio%20and%20video%20files%20in%20Java%20applications%20%20%20BUDDH

24/30

15/1/2014

How to play audio and video files in Java applications | BUDDHIMA'S COMPUTER LAB

Amit
August 17, 2013 at 6:13 PM

i got error in MediaPlay.java .The error was initComponents();


Reply

Buddhima Wijeweera
August 17, 2013 at 6:26 PM

Hi Amith, initComponents(); is a method auto generated by Netbeans IDE, so consider about that. Thanks
Reply

Richardo
August 20, 2013 at 1:25 PM

formats only (.mpg, some .avi files). Any library to play video format like mp4,flv and etc? i need idea for this.. thx before
Reply

Buddhima Wijeweera
August 20, 2013 at 4:47 PM

Hi Richardo, You could try vlcj, . Link: https://code.google.com/p/vlcj/ Cheers!


Reply

file:///E:/Advanced%20Java/Java%20Multimedia/How%20to%20play%20audio%20and%20video%20files%20in%20Java%20applications%20%20%20BUDDH

25/30

15/1/2014

How to play audio and video files in Java applications | BUDDHIMA'S COMPUTER LAB

Richardo
August 21, 2013 at 2:37 PM

thx u buddhima.. hmm i still confuse use vlcj, do you have tutorial for this? and JMF can intregation with vlcj? sorry iam newbie in programming >,<

Buddhima Wijeweera
August 21, 2013 at 7:42 PM

Hi Richardo, I think following links may help you. 1. http://www.capricasoftware.co.uk/projects/vlcj/tutorial1.html 2. https://code.google.com/p/vlcj-apps/

Narveer
September 1, 2013 at 9:03 PM

Dear sir I am working on web project using servlet and jsp i need a media player for that using javaso please help me for this i need code for player so that i can run hat code on my web pages ..the player are totally based on java So please help me to doing this Thank you Your Code was Really really good please help me to doing my task
Reply

Buddhima Wijeweera
September 1, 2013 at 10:41 PM

Hi Narveer,
file:///E:/Advanced%20Java/Java%20Multimedia/How%20to%20play%20audio%20and%20video%20files%20in%20Java%20applications%20%20%20BUDDH 26/30

15/1/2014

How to play audio and video files in Java applications | BUDDHIMA'S COMPUTER LAB

Thank you for appreciations ! After searching I found following links, which may help you: [1] http://pramodjain.ulitzer.com/node/45832/mobile [2] http://goo.gl/5azE0t Cheers!
Reply

rupal
September 12, 2013 at 3:47 PM

Hi, I am getting like this error, Unable to handle format: MPEG, 352240, FrameRate=29.9, Length=126720 can not see the video but getting sound while running this
Reply

Buddhima Wijeweera
September 12, 2013 at 9:42 PM

Hi Rupal, These situations may cause because of incompatible video formats. You may need to tryout another solution. Thanks !
Reply

Gilles
September 24, 2013 at 5:45 PM

Hi and thank you for this very clear example of code. Im working on a project management advertising screens. Currently, the solution is on a Net Platform, but we need a more lightweight solution for deployment (screens often have an embedded version of Windows on which the .Net framework can not be installed). So I thought of using Java for portability, but the limitation in terms of video formats is a
file:///E:/Advanced%20Java/Java%20Multimedia/How%20to%20play%20audio%20and%20video%20files%20in%20Java%20applications%20%20%20BUDDH 27/30

15/1/2014

How to play audio and video files in Java applications | BUDDHIMA'S COMPUTER LAB

real problem. Do you think there is another library that can handle a variety of formats? Ill try vlcj library as you now recommend, hoping it can meet my need for movies. Unfortunately, I think there is not a lot of sample code But, I also flash videos to integrate this program. You would know a library capable of playing flash videos? Congratulations again for this article.
Reply

Buddhima Wijeweera
September 24, 2013 at 9:50 PM

Hi Gilles, Thank You very much for appreciations. I hope the links I have mentioned will be helpful to you with vlcj. Also consider this link too: http://www.youtube.com/watch? feature=player_embedded&v=orMgNh0o38A I did search on playing .flv videos, and I came across following link. Link: http://blog.pursuit.be/2007/09/playing-flashvideo-flv-with-java.html Cheers !
Reply

Rohit Chudasama
October 29, 2013 at 3:55 PM

I have an error SimpleAudioPlayer cannot be resolved to a type in SIMPLEAUDIOPLAYER.JAVA


Reply

Buddhima Wijeweera
October 30, 2013 at 9:39 AM

Hi Rohit, Can you express the issue in more descriptive manner


file:///E:/Advanced%20Java/Java%20Multimedia/How%20to%20play%20audio%20and%20video%20files%20in%20Java%20applications%20%20%20BUDDH 28/30

15/1/2014

How to play audio and video files in Java applications | BUDDHIMA'S COMPUTER LAB

? Thanks!
Reply

maya
November 23, 2013 at 3:18 PM

need your help for my school project. I need to play music when play button is clicked. I tried to import the javax.media.* but it shows error that package does not exist even when I have imported d package :/ please help.!!
Reply

Buddhima Wijeweera
November 23, 2013 at 7:47 PM

Hi Maya, It seems you havent add JMF as a library to NetBeans project. For that first you have to right click on the project name on the projects window. Click Properties from the pop-up you get. Go to Libraries section. Click Add Jar/Folder and select the jmf.jar from the location where you install the JMF. Hope you will be able to solve the problem. Thank You!
Reply

Leave a Reply

Previous Post

Next Post

file:///E:/Advanced%20Java/Java%20Multimedia/How%20to%20play%20audio%20and%20video%20files%20in%20Java%20applications%20%20%20BUDDH

29/30

15/1/2014

How to play audio and video files in Java applications | BUDDHIMA'S COMPUTER LAB

Blog at WordPress.com. | The Yoko Theme. Top

file:///E:/Advanced%20Java/Java%20Multimedia/How%20to%20play%20audio%20and%20video%20files%20in%20Java%20applications%20%20%20BUDDH

30/30

Вам также может понравиться