-2

I have about 20 different sound, and i want it to be played for example every 1 second. every sound just have a duration about 0.7 second. I don't even know how to import mp3 to java either. How can i do this in java?

UPDATE

 try
 {
      in = new FileInputStream(new File("C:\\Users\\Toshiba\\Downloads\\Music\\adios.wav"));

      AudioStream as = new AudioStream(in);
      AudioPlayer.player.start(as);

      Thread.sleep(1*1000);

  } catch (Exception e)
  {
      JOptionPane.showMessageDialog(null, e);
  }

i got new problem. i can't stop it once it start

Altiano Gerung
  • 824
  • 2
  • 15
  • 28
  • Try to write code that does that. – Maroun May 25 '14 at 09:33
  • I would use [JavaFX](http://www.oracle.com/technetwork/java/javase/overview/javafx-overview-2158620.html) as it has a MediaPlayer class that can play mp3 whereas default Java has little or no support for mp3. Use the TimerTask task to play sounds at intervals. – Cobbles May 25 '14 at 09:35
  • AudioPlayer is internal proprietary api which may be removed in the future so it is safer to use a different library. If you don't want to change to Java then you can use this library: http://www.javazoom.net/javalayer/javalayer.html – Cobbles May 25 '14 at 14:33
  • can you send me some example of how to use that library? – Altiano Gerung May 26 '14 at 06:35
  • nope that question is about play with 1 sound. my question is play many sounds with a delay from one to another. – Altiano Gerung May 26 '14 at 11:58

2 Answers2

0

You need to use javax.sound.sampled library, of which documentation can be found here also, because you want to play sound every x seconds, you should use

Thread.sleep(x*1000);

after you play the sound. (x is the number of seconds you want the computer to wait.)

If you provide more code, I'll be more specific.

padawan
  • 1,295
  • 1
  • 17
  • 45
0

Use JavaFX. It comes with netbeans

TimerTask task = new TimerTask() {
        @Override
        public void run() {
            //MediaPlayer player;
            //Media audioFile = new Media(new File("PATH_TO_SOUND"));
            //player = new MediaPlayer(audioFile);
            //player.play();

            FileInputStream in = new FileInputStream(new File("C:\\Users\\Toshiba\\Downloads\\Music\\adios.wav"));
            AudioStream as = new AudioStream(in);
            AudioPlayer.player.start(as);
        }
    };
Timer timer;
timer.scheduleAtFixedRate(task, 0, 1000); //start immediately, 1000ms period

Imports:

import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
Cobbles
  • 1,748
  • 17
  • 33
  • `timer.scheduleAtFixedRate(task, 0, 1000)` gives me an error. it says cannot find symbol – Altiano Gerung May 25 '14 at 10:06
  • sorry, i already fixed that, before i import it from javax library then i change to java library and it's solved. but it still gives me an error, can you show me what library need to be imported? – Altiano Gerung May 25 '14 at 10:20
  • Which IDE are you using? I started making an app with normal Java Swing and I couldn't play mp3s but JavaFX is a separate GUI toolkit. I use netbeans which comes with JavaFX so I suggest you create a new JavaFX project and then that code should work. – Cobbles May 25 '14 at 14:22
  • I have updated the answer with the relevant imports. – Cobbles May 25 '14 at 14:22
  • Which symbol can it not find? – Cobbles May 25 '14 at 14:24
  • i create my project not using javafx, just java. `Timer` cant be found – Altiano Gerung May 26 '14 at 06:29
  • Timer is from the [java.util.Timer library](http://docs.oracle.com/javase/6/docs/api/java/util/Timer.html) so you should be able to use it without JavaFX.. – Cobbles May 26 '14 at 09:56
  • do you think this code is working. there is no `File` parameter in `Media` Contructor. well i guess this question is taking too much time, i consider to close it. – Altiano Gerung May 26 '14 at 12:27
  • As I said, the Media class is from the JavaFX javafx.scene.media library. I have added your non-javafx version to my answer. try that :) But you will need to add a way to stop the sound playing after the start if it is stuck in a loop, but I have not used the AudioPlayer classes before. – Cobbles May 26 '14 at 17:02