Java

Moderators: zibadian
Number of threads: 7818
Number of posts: 18218

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
play audio file Posted by saran185 on 4 Feb 2011 at 2:58 AM
hi...

I have ".txt" files and ".mp3" files in java/bin.
When i open txt file the audio file must be played.
I have attached my code below..but i get error on compling..so can anyone pls help me in fixing the bugs..
thanks...

import java.io.BufferedInputStream;
import java.io.*;
import java.io.FileInputStream;

import javazoom.jl.player.Player;


public class MP3 {
    private String filename;
    private Player player; 

    // constructor that takes the name of an MP3 file
    public MP3(String filename) {
        this.filename = filename;
    }

    public void close() { if (player != null) player.close(); }

    // play the MP3 file to the sound card
    public void play() {
        try {
            FileInputStream fis     = new FileInputStream(filename);
            BufferedInputStream bis = new BufferedInputStream(fis);
            player = new Player(bis);
        }
        catch (Exception e) {
            System.out.println("Problem playing file " + filename);
            System.out.println(e);
        }

        // run in new thread to play in background
        new Thread() {
            public void run() {
                try { player.play(); }
                catch (Exception e) { System.out.println(e); }
            }
        }.start();




    }

  
    // test client
    public static void main(String[] args) {
        
				String targetExtension=".mp3";  
    if(args.length >= 1 )
      	{//1  
        String ext =args[0].substring(args[0].indexOf("."));  
        System.out.println(ext);  
        if(ext.equalsIgnoreCase(".txt")) 
		  {//2  
          File f = new File(args[0]);  
          //if the file exists  
          // then change the filename  
          if(f.exists()) 
		     {//3  
           args[0]=args[0].replace(ext,targetExtension);  
           System.out.println(" "+args[0]);  
           File filename = new File(args[0]);  
           f.renameTo(filename);  
		   
		   MP3 mp3 = new MP3(filename);
        mp3.play(); mp3.close();
}}}}

Report
Re: play audio file Posted by krishbaloda on 4 Feb 2011 at 4:18 AM
what error msg. ur r getting???
Report
Re: play audio file Posted by saran185 on 4 Feb 2011 at 4:58 AM
error msg is that constructor not found...
if(f.exists()) 
		     {//3  
           args[0]=args[0].replace(ext,targetExtension);  
           System.out.println(" "+args[0]);  
           File filename = new File(args[0]);  
           f.renameTo(filename);  
		   
		   MP3 mp3 = new MP3(filename);
        mp3.play();



Report
Re: play audio file Posted by krishbaloda on 4 Feb 2011 at 6:06 AM
filename is an object of java.io.File. ur constructor accepts string as u have defined in ur code

public MP3(String filename) {
this.filename = filename;
}

make this correction.
Report
Re: play audio file Posted by saran185 on 6 Feb 2011 at 2:17 AM
hi..
I changed the "filename " to string.
now i dont get any errors.
But i get only the partial output.If i mention the txt file as input.. it changes the filename to "....mp3" but does not play the mp3 file..
import java.io.BufferedInputStream;
import java.io.*;
import java.io.FileInputStream;

import javazoom.jl.player.Player;


public class MP3 {
   private String filename;
    private Player player; 

    // constructor that takes the name of an MP3 file
    public MP3(String filename) {
        this.filename = filename;
    }

    public void close() { if (player != null) player.close(); }

    // play the MP3 file to the sound card
    public void play() {
        try {
            FileInputStream fis     = new FileInputStream(filename);
            BufferedInputStream bis = new BufferedInputStream(fis);
            player = new Player(bis);
        }
        catch (Exception e) {
            System.out.println("Problem playing file " + filename);
            System.out.println(e);
        }

        // run in new thread to play in background
        new Thread() {
            public void run() {
                try { player.play(); }
                catch (Exception e) { System.out.println(e); }
            }
        }.start();




    }

  
    // test client
    public static void main(String[] args) {
        
				String targetExtension=".mp3";  
    if(args.length >= 1 )
      	{//1  
        String ext =args[0].substring(args[0].indexOf("."));  
        System.out.println(ext);  
        if(ext.equalsIgnoreCase(".txt")) 
		  {//2  
          File f = new File(args[0]);  
          //if the file exists  
          // then change the filename  
          if(f.exists()) 
		     {//3  
           args[0]=args[0].replace(ext,targetExtension);  
           System.out.println(" "+args[0]);  
           //File filename = new File(args[0]);  
           // f.renameTo(filename);  
		   String filename = args[0];
		   
		   MP3 mp3 = new MP3(filename);
        mp3.play();

        
        
        mp3.close();

        
               }
			   
           
            }
		
	       
		}

    }

}


Report
Re: play audio file Posted by saran185 on 6 Feb 2011 at 3:03 AM
hi...i got the output..thanks for helping ....
now if i input the txt file the audio file is played...



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.