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)

Recommended Answers

All 16 Replies

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.

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.

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?

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

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

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

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?

"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...

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.

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" );

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

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?

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.

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.

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.

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

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.