1,105,380 Community Members

Music in my game?

Member Avatar
minime010
Newbie Poster
11 posts since Jan 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I have a problem with this...

import javax.swing.*;
import sun.audio.*;
import java.awt.event.*;
import java.io.*;

public class PikaPlatformer
{

   //-----------------------------------------------------------------
   //  Creates and displays the application frame.
   //-----------------------------------------------------------------
   public static void main (String[] args)
   {
      JFrame frame = new JFrame ("Direction");
      frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

      frame.getContentPane().add (new PikaPlatformerPanel());

      frame.pack();
      frame.setVisible(true);
   }

}

How would I insert sound into this I just want music to play in my game. Any suggestions?( this is just the jFrame)

Member Avatar
Taywin
Posting Maven
2,632 posts since Apr 2010
Reputation Points: 134 [?]
Q&As Helped to Solve: 378 [?]
Skill Endorsements: 17 [?]
 
0
 

You could use sun java library to add the music... Look at this link about how to do it... The example shouldn't be deprecated yet.

Member Avatar
stultuske
Posting Expert
5,329 posts since Jan 2007
Reputation Points: 938 [?]
Q&As Helped to Solve: 776 [?]
Skill Endorsements: 35 [?]
Featured
 
0
 

depends on what "sound". what kind of file are we talking about? .midi? .wav? .mp3?
the first few can be played with using nothing but core java, but if you want (for instance) to play .mp3 files you'll need an additional library to get it to work.

Member Avatar
minime010
Newbie Poster
11 posts since Jan 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I have the wav file because its the easiest. I do have a different version, but it seems kind of messed up. i use eclipse and a lot answers aren't compatible with the program. What should my code look like?

Member Avatar
stultuske
Posting Expert
5,329 posts since Jan 2007
Reputation Points: 938 [?]
Q&As Helped to Solve: 776 [?]
Skill Endorsements: 35 [?]
Featured
 
0
 

I have ... no idea what you are talking about. the IDE you use (you could even use notepad) doesn't matter, it doesn't change the code.
but, seriously: Google rules. just a search for "play wav with java" gave this as first result:

Link

Member Avatar
minime010
Newbie Poster
11 posts since Jan 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

But I don't want a button, I just want my wav file to play right away.

Member Avatar
minime010
Newbie Poster
11 posts since Jan 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

and before, I meant that I have a different version of the program.

Member Avatar
minime010
Newbie Poster
11 posts since Jan 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

New code:

import javax.swing.*;

import sun.audio.*;
import java.awt.event.*;
import java.io.*;
import java.awt.*;

public class PikaPlatformer
{
   public static void main (String[] args)
   {
       AePlayWave aw = new AePlayWave( "C:\\Documents and Settings\\JonathanConnelly\\Desktop\\Pokemon_Red_Blue_Yellow_Battle_music.wav" );
       aw.start();     

      JFrame frame = new JFrame ("Direction");
      frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);



      frame.getContentPane().add (new PikaPlatformerPanel());

      frame.pack();
      frame.setVisible(true);
   }            
}

Shouldn't the AePlayWave work? Is my .wav file too big?

Member Avatar
Taywin
Posting Maven
2,632 posts since Apr 2010
Reputation Points: 134 [?]
Q&As Helped to Solve: 378 [?]
Skill Endorsements: 17 [?]
 
0
 

"C:\Documents and Settings\JonathanConnelly\Desktop\Pokemon_Red_Blue_Yellow_Battle_music.wav"

Not sure if white spaces in the folder name are ligit? You may need to escape that... By the way, I think you may need to look into pathSeparator() from File class...

Member Avatar
minime010
Newbie Poster
11 posts since Jan 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Can someone just tell me exactly what to put, and where it should be put?
Oh, I do have separate content that my class accesses.

Member Avatar
Taywin
Posting Maven
2,632 posts since Apr 2010
Reputation Points: 134 [?]
Q&As Helped to Solve: 378 [?]
Skill Endorsements: 17 [?]
 
0
 

Hmm... Try this then... Not sure if it matters...

AePlayWave aw = new AePlayWave( "C:\\Documents\ and\ Settings\\JonathanConnelly\\Desktop\\Pokemon_Red_Blue_Yellow_Battle_music.wav" );
Member Avatar
minime010
Newbie Poster
11 posts since Jan 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Where would that go? Documents and settings is the full name of a folder. When you put the extra slashes, I got an error.

Member Avatar
Taywin
Posting Maven
2,632 posts since Apr 2010
Reputation Points: 134 [?]
Q&As Helped to Solve: 378 [?]
Skill Endorsements: 17 [?]
 
0
 

OK, so my guess is wrong that it requires to escape white spaces (I don't run Java on Windows box :(). How about you try to run a smaller .wav file first? What is your current .wav file size by the way?

Member Avatar
stultuske
Posting Expert
5,329 posts since Jan 2007
Reputation Points: 938 [?]
Q&As Helped to Solve: 776 [?]
Skill Endorsements: 35 [?]
Featured
 
0
 

whether or not you want it to run after clicking on a button or at startup does not change the code of playing the music file at all.
you want it to play in the background the whole time?
run it first from early within the main. start an additional thread that monitors whether or not your sound finished playing (or any other way) and if done, re-start it.

Member Avatar
minime010
Newbie Poster
11 posts since Jan 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I have tried running it from the main. And The file is big. Very big. I'll try a smaller file when I can, but that wont be till at least thursday.

Member Avatar
minime010
Newbie Poster
11 posts since Jan 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

whether or not you want it to run after clicking on a button or at startup does not change the code of playing the music file at all. you want it to play in the background the whole time? run it first from early within the main. start an additional thread that monitors whether or not your sound finished playing (or any other way) and if done, re-start it.

Could you give me instructions? I'm not super experienced.

Member Avatar
minime010
Newbie Poster
11 posts since Jan 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

JFrame here

import java.net.URL;
import javax.swing.*;
import javax.sound.sampled.*;
public class PikaPlatformer
{

   //-----------------------------------------------------------------
   //  Creates and displays the application frame.
   //-----------------------------------------------------------------
   public static void main (String[] args)throws Exception
   {
      URL url = new URL(
      "http://tindeck.com/download/f8d086eb0dadd3745cb7c6dfa027fd89/ihmz/[tindeck.com]%20-%20minime010%20-%20Pokemon_Red_Blue_Yellow_Battle_music.wav");
      Clip clip = AudioSystem.getClip();
      AudioInputStream ais = AudioSystem.
      getAudioInputStream( url );
      clip.open(ais);
      clip.loop(5);
      JFrame frame = new JFrame ("Go Pikachu, go!!!");
      frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add (new PikaPlatformerPanel());
      frame.pack();
      frame.setVisible(true);
   }

}

Program here

//********************************************************************
//  Pikachu Platformer Game
//  Jonathan Connelly
//********************************************************************

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import javax.sound.sampled.*;


public class PikaPlatformerPanel extends JPanel
{
   private final int WIDTH = 1433, HEIGHT = 100;
   private int JUMP = 5;  // increment for image movement
   private Clip clip;
   private final int IMAGE_SIZE = 40;

   private ImageIcon up, down, right, left, back, currentImage, attack, atkmoving;
   private int x, y;

   public static void main(String args[]){
     new PikaPlatformerPanel();}
   //-----------------------------------------------------------------
   //  Constructor: Sets up this panel and loads the images.
   //-----------------------------------------------------------------
   public PikaPlatformerPanel()
   {
      addKeyListener (new DirectionListener());



      x = 1;
      y = 50;
      attack = new ImageIcon ("Thundershock.png");
      up = new ImageIcon ("New position.png");
      down = new ImageIcon ("Pikachu1.png");
      left = new ImageIcon ("PikachuLeft.png");
      right = new ImageIcon ("PikaRunning.png");
      back = new ImageIcon ("PikaPlatBackground.PNG");
      currentImage = right;
      setBackground (Color.black);
      setPreferredSize (new Dimension(WIDTH, HEIGHT));
      setFocusable(true);
      }


   //-----------------------------------------------------------------
   //  Draws the image in the current location.
   //-----------------------------------------------------------------

   public void paintComponent (Graphics page)
   {
      super.paintComponent (page);
      back.paintIcon (this, page, 1, 1);
      currentImage.paintIcon (this, page, x, y);
   }
   private class DirectionListener implements KeyListener
   {
          //--------------------------------------------------------------
          //  Responds to the user pressing arrow keys by adjusting the
          //  image and image location accordingly.
          //--------------------------------------------------------------
          public void keyPressed (KeyEvent event)
          {
             switch (event.getKeyCode())
             {
                case KeyEvent.VK_SPACE:


                case KeyEvent.VK_LEFT:
                   currentImage = left;
                   x -= JUMP;
                   break;
                case KeyEvent.VK_RIGHT:
                   currentImage = right;
                   x += JUMP;
                   break;
                case KeyEvent.VK_UP:
                       currentImage = up;
                       break;
                case KeyEvent.VK_W:
                       currentImage = up;
                       break;
             }
           if (x >= WIDTH-IMAGE_SIZE)
                 {   
                    JUMP= 0;
                    if (currentImage.equals(left))
                    {
                        currentImage= right;
                        JUMP= 5;
                    }
                    if (currentImage.equals(right))
                    {
                        currentImage= left;
                    }
                 }
           if (x <= 1428)
                {    
                JUMP= 0;
                if (currentImage.equals(left))
                    {
                        currentImage= right;
                    }
                else if (currentImage.equals(right))
                    {
                        JUMP= 5;
                        currentImage= right;
                    }
                    repaint();
                }         

             }

        public void keyReleased(KeyEvent arg0) 
        {
            // TODO Auto-generated method stub

        }

        public void keyTyped(KeyEvent arg0) 
        {
            // TODO Auto-generated method stub

        }



        }

}

Solved

Question Answered as of 1 Year Ago by Taywin and stultuske
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: