DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 2 of 2
  1. #1
    Join Date
    Nov 2006
    Posts
    13

    Sound problem in java

    i have this code:
    Code:
                             
       try {
       
        
        InputStream in = new FileInputStream("1941krote.au");  
        AudioStream as = new AudioStream(in);       
         AudioPlayer.player.start(as);           
    }catch (IOException e) {
    }
    which should play a sound file when the application is loaded up but it doesnt

    if you want to see the full code its:
    Code:
    import java.awt.*;
    import java.io.*;
    import java.awt.event.*;
    import javax.swing.*;
    import  sun.audio.*;   
    import java.util.*;
    import java.net.*;
    
    import java.applet.*;
    public class test2 extends JFrame implements KeyListener
     {
     	int right = 225;
    	int up = 225;
    	ImageIcon snake;
    	Container f2;
    	String test;
    
    
        
    	
    public test2() {
        super("FSnake");
        f2 = getContentPane();
        f2.setLayout(new FlowLayout());
        
    
            String mediaName = "1941krote.mid";
    
    
    	
    	snake = new ImageIcon("tn_ea03.jpg");
    	setVisible(true);
    	setSize(500,500);
    	addKeyListener(this);
    	}
    
    	
    public void keyReleased(KeyEvent event){
    		System.out.println( "Key pressed: %s" + 
               event.getKeyText( event.getKeyCode() ) ); 
    
    
           
    }
    
    public void keyTyped(KeyEvent event){
    		System.out.println( "Key pressed: %s" + 
               event.getKeyText( event.getKeyCode() ) ); 
    }
    
     
        
    public void paint(Graphics g){
    	super.paint(g);
    
    
    
    
    g.drawImage( snake.getImage(), up, right, null );
    }
    
    
       public void repaint() {
    	paint(getGraphics()) ;
        }
        
        public void keyPressed(KeyEvent event) {
    	System.out.println( "Key pressed: %s" + 
               event.getKeyText( event.getKeyCode() ) ); // output pressed key
         
             right = right - 50;
           
            
             repaint(); 
             
    
    }
    public static void main(String[] args){
    	
    test2 application = new test2();
                                    
       try {
       
        
        InputStream in = new FileInputStream("C:/Program Files/Xinox Software/JCreatorV3LE/MyProjects/Snake/1941krote.au");  
        AudioStream as = new AudioStream(in);       
         AudioPlayer.player.start(as);           
    }catch (IOException e) {
    }
    	
    
    
    
    }
    
    }
    Can you find out why it does not play the audio file please.

  2. #2
    Join Date
    Nov 2006
    Posts
    12
    AudioInputStream doesn't play a sound, it just gets the source of the sound.
    You need code like this

    // get source of audio
    AudioInput Stream sound = AudioSystem.getAudioInputStream("url of sound file");
    // open up channel to play sound
    Clip clip = AudioSystem.getClip();
    // open sound file
    clip.open(sound);
    // play sound once
    clip.start();
    // or loop sound where int is number of times or Clip.LOOP_CONTINUOUSLY
    clip.loop(int)

    this works with .wave .au and .aiff files.
    Last edited by Red_Jester; 11-20-2006 at 11:17 AM.

Similar Threads

  1. Replies: 3
    Last Post: 03-21-2007, 03:28 PM
  2. Adding sound to my java applet
    By ablaye in forum Java
    Replies: 1
    Last Post: 06-19-2006, 05:18 AM
  3. Replies: 2
    Last Post: 06-14-2006, 03:16 PM
  4. Java 3D to 2D applet problem
    By nataku in forum Java
    Replies: 0
    Last Post: 04-27-2006, 05:29 PM
  5. Is the Java Sound API too complex?
    By RikMeucci in forum Java
    Replies: 2
    Last Post: 12-05-2005, 06:59 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links