Results 1 to 5 of 5
  1. #1
    palie is offline Member
    Join Date
    Jan 2012
    Posts
    6
    Rep Power
    0

    Default How to insert music such mp3 to java program

    i have problem to insert the music to my coding..Actually it is a snake game.
    can anyone give me the better coding to insert music..

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default Re: How to insert music such mp3 to java program

    Take a look at the static methods of the Applet class. See anything you might be able to use?

    JavaFX 2.x has some stuff in the javafx.scene.media package, but I don't know how much work it might be to incorporate that in an application not written for JavaFX. At the very least, it would require you to download the JavaFX SDK (bundled with the JDK from 7u3 onwards).

    And then there's JMF, but I've never looked at the API so I can't judge how useful it might be to you.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default Re: How to insert music such mp3 to java program

    You can use AudioInputStream to play a .WAV or .MIDI sound file

    Uses of Class javax.sound.sampled.AudioInputStream (Java 2 Platform SE 5.0)

    There is an example here which shows you how to loop the sound file:
    audio - Looping Java AudioInputStream - Stack Overflow

  4. #4
    palie is offline Member
    Join Date
    Jan 2012
    Posts
    6
    Rep Power
    0

    Default Re: How to insert music such mp3 to java program

    by the way..thank you for your help.im new to java..
    i already try but it cant work..

    now i put my coding and can somebody try to put music into that program..

    import java.awt.*;
    import java.applet.*;


    public class snake extends Applet implements Runnable
    {
    Image dot[]=new Image[600]; //declare image dot
    Image back;
    Image offI;
    Graphics offG;

    int x[]= new int[600];
    int y[]= new int[600];
    int rtemp=1;
    int game=1;
    int level; //declare level
    int z;
    int n;
    int count=0; //declare count
    int score=0; //declare score
    int add=1;

    Button u= new Button("Insert your name");

    Button b= new Button("Budak Baru");
    Button i= new Button("Cekap Sikit");
    Button p= new Button("Cekap Banyak");
    Button X= new Button("Melampau");






    String stemp;
    String s;
    String t;

    boolean go[]=new boolean[600];
    boolean left=false;
    boolean right=false;
    boolean up=false;
    boolean down=false;
    boolean started=false ;
    boolean me=false;

    Thread setTime;

    public void init()
    {
    add(u);
    add(b);
    add(i);
    add(p);
    add(X);
    setBackground(Color.green); //background color
    back = getImage(getCodeBase(), "");

    for (z=0 ; z < 600 ; z++){dot[z] = getImage(getCodeBase(), "dot.gif"); }
    }



    public void update(Graphics g)
    {
    Dimension d = this.size();
    if(offI == null)
    {
    offI = createImage(d.width, d.height);
    offG = offI.getGraphics();
    }
    offG.clearRect(0, 0, d.width, d.height);
    paint(offG);
    g.drawImage(offI, 0, 0, null);
    }

    public void paint(Graphics g)
    {
    g.drawImage(back,0,0, this);
    g.setColor(Color.black);

    if(started)
    {
    g.setFont(new Font("Verdana", 1, 12));
    t = "Score "+score+"";
    g.drawString(t, 200, 490);
    }

    if(game==1)
    {
    g.setFont(new Font("Verdana", 1, 13));
    s = "Tahap Anda";
    g.drawString(s, 200, 30);

    u.move(190,230);


    b.move(200, 50); //kedudukan button level
    i.move(200, 90);
    p.move(195, 130);
    X.move(200, 170);
    }

    if((game==2)||(game==3))
    {
    if(!started)
    {
    g.setFont(new Font("Verdana", 1, 11));
    t = "Use the key board arrows to move!";
    g.drawString(t, 150,300);
    }
    for (z=0 ; z <= n ; z++){ g.drawImage(dot[z],x[z],y[z],this); }
    me=true;
    }

    if(!me)
    {
    g.setFont(new Font("Verdana", 1, 11));
    t = "by NABIL & FADHLI";
    g.drawString(t, 300, 495);
    }

    if(game==3)
    {
    g.setFont(new Font("Verdana", 1, 13));
    s="ANDA KALAH..CUBA LAGI";
    g.drawString(s, 200, 60);
    }

    }

    public void run()
    {
    for(z=4 ;z <600 ; z++) { go[z]=false;}
    for(z=0 ; z<4 ; z++) { go[z]=true;x[z]=91;y[z]=91;}
    n=3;
    game=2;
    score=0;

    u.move(70,-100);

    b.move(70, -100); //kedudukan button selepas click
    i.move(70, -100);
    p.move(70, -100);
    X.move(70, -100);
    left=false;
    right=false;
    up=false;
    down=false;
    locateRandom(4);

    while(true)
    {
    if (game==2)
    {
    if ((x[0]==x[n])&&(y[0]==y[n])){go[n]=true;locateRandom((n+1));score+=add; }
    for(z = 599 ; z > 0 ; z--)
    {
    if (go[z])
    {
    x[z] = x[(z-1)]; y[z] = y[(z-1)];
    if ((z>4)&&(x[0]==x[z])&&(y[0]==y[z])){ game=3; }
    }
    }
    if(left){ x[0]-=10; }
    if(right){ x[0]+=10; }
    if(up){ y[0]-=10; }
    if(down){ y[0]+=10; }
    }

    //saiz border
    if(y[0]>500){y[0]=500;game=3;}
    if(y[0]<1){y[0]=1;game=3;}
    if(x[0]>500){x[0]=500;game=3;}
    if(x[0]<1){x[0]=1;game=3;}

    if (game==3)
    {
    if (count <(1500/level)) { count++; } else { count=0;game=1;repaint();setTime.stop(); }
    }

    repaint();
    try{setTime.sleep(level);}
    catch(InterruptedException e){}
    }
    }

    public void locateRandom(int turn)
    {
    rtemp=(int)(Math.random()*50); //locate for dot
    x[turn]=((rtemp*10)+1) ;
    rtemp=(int)(Math.random()*50);
    y[turn]=((rtemp*10)+1);
    n++;
    }

    public boolean keyDown(Event e, int key)
    {
    if ((key == Event.LEFT) &&(!right)){left = true; up = false; down = false;if(!started)started=true;}
    if ((key == Event.RIGHT) && (!left)){right = true; up = false; down = false;if(!started)started=true;}
    if ((key == Event.UP) && (!down)){ up = true; right = false; left = false;if(!started)started=true;}
    if ((key == Event.DOWN) && (!up)){down = true; right = false; left = false;if(!started)started=true;}
    return true;
    }

    public boolean action(Event event, Object obj)
    {
    stemp = (String) obj;

    if(stemp.equals("Budak Baru"))
    {
    add=2;
    level=100;
    setTime = new Thread(this);
    setTime.start();
    return true;
    }

    if(stemp.equals("Cekap Sikit"))
    {
    add=5;
    level=70;
    setTime = new Thread(this);
    setTime.start();
    return true;
    }

    if(stemp.equals("Cekap Banyak"))
    {
    add=10;
    level=40;
    setTime = new Thread(this);
    setTime.start();
    return true;
    }

    if(stemp.equals("Melampau"))
    {
    add=20;
    level=20;
    setTime = new Thread(this);
    setTime.start();
    return true;
    }


    return false;
    }


    }


    hopefully you can help me to solve it..thank you

  5. #5
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default Re: How to insert music such mp3 to java program

    Please edit your post to wrap your code in CODE tags so that it is formatted.

    Please also indent your code so that it is easier to read.

    Also, please only post code that is relevent to your problem.

    If you had any error messages - please post the error message only the code related to the error.

    No one will help you when you throw a dose of headache at them.

Similar Threads

  1. Java, Playing Music?
    By Alerhau in forum New To Java
    Replies: 3
    Last Post: 08-18-2011, 09:19 PM
  2. playing music while program runs
    By liluma in forum New To Java
    Replies: 2
    Last Post: 05-04-2011, 06:16 PM
  3. Replies: 0
    Last Post: 12-06-2010, 03:52 PM
  4. Music in Java Program
    By AWPtic in forum New To Java
    Replies: 1
    Last Post: 04-06-2009, 07:35 PM
  5. Replies: 3
    Last Post: 02-28-2009, 10:17 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •