java.media problem

compatability issue

Page 1 of 1

5 Replies - 899 Views - Last Post: 27 June 2009 - 12:48 PM Rate Topic: -----

#1 painkiller102  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 15
  • View blog
  • Posts: 281
  • Joined: 27-February 08

java.media problem

Posted 27 June 2009 - 11:46 AM

I'm not sure what I'm doing wrong in this code and cannot figure out why this error is coming up. the error is: Incompatible Types and i know what this error means, but in this case i don't know how to fix it. I am using online tutorials in my attempt to get a good fully working mp3 player and all of them are using the java.media package and the javax.sound.samples package. Every which way i have tried to get this to work using multiple tutorials including: http://www.developer...cle.php/2173111 i cannot seem to get any of them to work! here is my code:
import java.io.*;
import javax.media.format.AudioFormat;
import javax.sound.sampled.*; 
public class main extends javax.swing.JFrame {
  AudioFormat audioFormat;
  AudioInputStream audioInputStream;
  SourceDataLine sourceDataLine;
  boolean stopPlayback = false;
	public main()
	{
		playAudio();
	 }
	private void playAudio()
	{
		try
		{
			File soundFile = new File("");
			audioInputStream = AudioSystem.getAudioInputStream(soundFile);
			audioFormat = audioInputStream.getFormat();
			System.out.println(audioFormat);
		}
		catch(Exception ex)
		{
			System.out.println("Sound read error");
		}
	}
}



the Incompatible Error always occurs when i try to do:

audioFormat = audioInputStream.getFormat();



Apparently you cannot use a javax.sound.sampled.audioformat with a javax.media.format.audioformat.
however every tutorial says this is how you have to do it?! I am very confused by now and any help is greatly appreciated!

Is This A Good Question/Topic? 0
  • +

Replies To: java.media problem

#2 painkiller102  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 15
  • View blog
  • Posts: 281
  • Joined: 27-February 08

Re: java.media problem

Posted 27 June 2009 - 11:56 AM

Correction. i Just solved the problem. It seems both packages contained "AudioFormat" and that the program was calling on the javax.media format and not the javax.sound format! Sorry for my oversight!
Was This Post Helpful? 0
  • +
  • -

#3 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: java.media problem

Posted 27 June 2009 - 11:59 AM

Just wanted to make sure anyway that you realise you need a special plugin I think in order to play mp3 with the java media framework.
Was This Post Helpful? 0
  • +
  • -

#4 painkiller102  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 15
  • View blog
  • Posts: 281
  • Joined: 27-February 08

Re: java.media problem

Posted 27 June 2009 - 12:13 PM

View Postnick2price, on 27 Jun, 2009 - 10:59 AM, said:

Just wanted to make sure anyway that you realise you need a special plugin I think in order to play mp3 with the java media framework.


yea, i downloaded the right JMF platform to play .mp3's and got it to work, however i could not get the code to retrieve song duration, etc. I got the above code to work, however obviously it only works on MIDIS and wav's and what not. So i am kind of stuck in the middle on what to do by this point
Was This Post Helpful? 0
  • +
  • -

#5 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: java.media problem

Posted 27 June 2009 - 12:22 PM

Have you seen this simple code to play mp3's?
import javax.media.*;
import java.io.*;
import java.net.URL;
 
class mp3 extends Thread
{
 
private URL url;
private MediaLocator mediaLocator;
private Player playMP3;
 
public mp3(String mp3)
{
try{
   this.url = new URL(mp3);
   }catch(java.net.MalformedURLException e)
	  {System.out.println(e.getMessage());}
}
 
public void run()
{

try{
   mediaLocator = new MediaLocator(url);	 
   playMP3 = Manager.createPlayer(mediaLocator);
	}catch(java.io.IOException e)
	  {System.out.println(e.getMessage());
	}catch(javax.media.NoPlayerException e)
	  {System.out.println(e.getMessage());}

playMP3.addControllerListener(new ControllerListener()
  {
  public void controllerUpdate(ControllerEvent e)
	 {
	 if (e instanceof EndOfMediaEvent)
		 {
		 playMP3.stop();
		 playMP3.close();
		 }
	 }
  }
 );
 playMP3.realize();
 playMP3.start();
 } 
}

public class playmp3{
  public static void main(String[] args)
  {
  mp3 t = new mp3("file:///C://JavaApplications//cd.mp3");
  t.start();
  }
}



And to get duration, code is along the lines of
mediaPlayer.getDuration().getSeconds() 

This post has been edited by nick2price: 27 June 2009 - 12:24 PM

Was This Post Helpful? 1
  • +
  • -

#6 painkiller102  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 15
  • View blog
  • Posts: 281
  • Joined: 27-February 08

Re: java.media problem

Posted 27 June 2009 - 12:48 PM

no i have not! i have seen probably 4 different ways to do it now! Thanks for the example. I hope it will work better than the others!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1