play audio file - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

play audio file

saran185saran185 Posts: 23Member
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...
[code]

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();
}}}}
[/code]

Comments

  • krishbalodakrishbaloda Posts: 45Member
    what error msg. ur r getting???
  • saran185saran185 Posts: 23Member
    error msg is that constructor not found...
    [code]
    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);

    [color=Orange]MP3 mp3 = new MP3(filename);[/color]
    mp3.play();

    [/code]

  • krishbalodakrishbaloda Posts: 45Member
    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.
  • saran185saran185 Posts: 23Member
    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..
    [code]
    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();


    }


    }


    }

    }

    }

    [/code]
  • saran185saran185 Posts: 23Member
    hi...i got the output..thanks for helping ..:-)..
    now if i input the txt file the audio file is played...:-)
Sign In or Register to comment.