Member Avatar for Ralphael

I am trying to play an mp3 file using the JMF java library and an mp3 codec with the following code but i am getting an error and don know the source

package source;


import java.io.File;

import javax.media.Format;
import javax.media.Manager;
import javax.media.MediaLocator;
import javax.media.Player;
import javax.media.PlugInManager;
import javax.media.format.AudioFormat;
import javax.media.*;

public class AudioTest {
    public static void main(String[] args) {
        Format input1 = new AudioFormat(AudioFormat.MPEGLAYER3);
        Format input2 = new AudioFormat(AudioFormat.MPEG);
        Format output = new AudioFormat(AudioFormat.LINEAR);
        PlugInManager.addPlugIn(
            "com.sun.media.codec.audio.mp3.JavaDecoder",
            new Format[]{input1, input2},
            new Format[]{output},
            PlugInManager.CODEC
        );
        try{
            Player player = Manager.createPlayer(new MediaLocator(new File("C:\\Users\\Adetola-Ralph\\test\\f5\\1.mp3").toURI().toURL()));
            player.start();
        }
        catch(Exception ex){
            ex.printStackTrace();
        }
    }
}

*this is the error i am encountering

Failed to configure: com.sun.media.PlaybackEngine@1e7c42fe
Bad header in the media: Couldn't detect stream type

Error: Unable to realize com.sun.media.PlaybackEngine@1e7c42fe*

im using this method i hope it helps:

public void sound(String nm) {
        AudioStream BGM;
        try {
            InputStream test = new FileInputStream(nm + ".wav");
            BGM = new AudioStream(test);
            AudioPlayer.player.start(BGM);
            //MD = BGM.getData();
            //loop = new ContinuousAudioDataStream(MD);
        } catch (IOException ex) {
            JOptionPane.showMessageDialog(null, "File can't be found");
        }
    }

and insert this where you want to play when its been clicked...

sound("Full name of the song");

if I look at this link, I guess you're not the only person having this problem. personally, I've hardly used JFM, but I have written an mp3 player in Java (once). Are you only trying to play mp3 files? Is JMF a pre-requisite or rather an option?
if so, try the JLayer library offered by JavaZoom. In my experience, it's a lot easier to work with compared to JMF, unlike JMF it's still being updated and it works perfectly with mp3 files.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.