0

I have Code Which plays a mp3 file java.

The issue that I encounter was that,

--> The audio output was only heard when program ran under the debug mode with breakpoints, but not under Run mode.

What could be the possible way counter this issue? I have attached the code for better understanding.

 FileInputStream mp3_file=new FileInputStream("xyz.mp3");
 Player mp3=new Player(mp3_file);
 mp3.play();
 System.out.println("Over");
Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
Jeetesh Nataraj
  • 608
  • 2
  • 8
  • 20
  • 2
    So what happens when you *do* just run it? Does the app close immediately? I suspect the `Player.play()` method just *starts* it playing, rather than blocking until the track is finished. – Jon Skeet Jun 30 '11 at 06:24
  • After Sometime, Console output is "Over", with no audio output... – Jeetesh Nataraj Jun 30 '11 at 06:26

4 Answers4

1

Probably your program ends before the music has finished. Make sure that your program doesn't end until the music has finished. For example:

mp3.play();
System.out.println("Press Enter to stop");
System.in.read();  // wait until user presses Enter
Jesper
  • 202,709
  • 46
  • 318
  • 350
0

Threading with a waitFor() directive might be the way to go. That also could give you the ability to have a separate thread for the control interface that allows you to interrupt or pause.

0

Most probably your program is just exiting because the code in the main method is finished very quickly.

We don't know what is your Player but I think it is playing music on a different thread than main thread, so as long as your code stops, it works fine.

In the main method, you should stop execution of code. For that, simply you can read something from console using:

import java.util.Scanner;
...
// here we will get input and program won't quit until we press Return
new Scanner(System.in).readLine();

In addition to that, you can use something like

try{Thread.sleep(Long.MAX_VALUE);} catch(Exception e){}

to sleep main thread and wait too much time.

ahmet alp balkan
  • 42,679
  • 38
  • 138
  • 214
0

I have faced same issue in my one of the project. I have used for loop. It solve my problem.

FileInputStream mp3_file=new FileInputStream("xyz.mp3");
Player mp3=new Player(mp3_file);
 for(int i=0;i<=3; i++)
     mp3.play();

System.out.println("Over"); 

But If create executable jar file of your code and run it, it works fine..... ;)

Kamahire
  • 2,149
  • 3
  • 21
  • 50