简体   繁体   中英

how to attach my mp3 file with generated jar file?

in this code i select mp3 path is "D:/camera.wav". when i make jar file of this code, it play in only my system. please tell me how to add camera.wav file to my project and give that url to my project. because i want same music should play for all systems, even though they don't have that music.

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; 
public class maintest 
{ 
    public static void main(String[] args) 
    { 
    //float v=1; 
    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("D:/camera.wav").toURI().toURL())); 
        //player.getGainControl().setLevel(v); 
        player.start(); 
    } 
    catch(Exception ex){ 
        ex.printStackTrace(); 
    } 
} 
}

JMF is .. ancient. it hasn't been maintained/updated for years, so especially if you want to have some "modern" code, JMF is not the best option.

personally, I once created (al be it a simple one) a small mp3 player, using the JLayer library which you can find here it has plenty of documentation and examples so it shouldn't take too long to get you going.

You need the MP3 Plugin . Or if that doesn't work, this plugin is also a good resource.

You can also use JavaFX for playing MP3's. If you use Netbeans just create a new project and select JavaFX project instaed of a normal Java project

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM