Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I am making a study guide in java to help me learn Japanese, but I have been having a hard time getting it to speak Japanese to me. Espeak did not work, so I used this code to get a .mp3 file and installed mpg123 to play the file. This works fine it the terminal, but it can't get it to play in java.

My code:

String command="mpg123 "+dir+"/Xubuntu\\ Apps/Study\\ guide/Japanese/sounds/あう.mp3";
        System.out.println(command);
        try {Runtime.getRuntime().exec(command);} catch (IOException e) {e.printStackTrace();}

The output

mpg123 /home/myuser/Documents/Xubuntu\ Apps/Study\ guide/Japanese/sounds/あう.mp3

I hear no sound, but when I copy/paste the command in the terminal, it works fine.

Why isn't playing in java?

share|improve this question
    
Why it should be in Java? –  Braiam Nov 2 '13 at 19:00
    
@Braiam The rest of the study guide is already written in java. I am just trying to add functionality –  TAAPSogeking Nov 2 '13 at 19:04
add comment

1 Answer

If you would like to use an Embed code for playing the song then you can use this code ,:)

Code for playing a MP3 song in Java ,

import javax.media.*;
import java.net.*;
import java.io.*;
import java.util.*;

class AudioPlay   
{
 public static void main(String args[]) throws Exception
 {

 // Take the path of the audio file from command line

 File f=new File(args[0]);

 // Create a Player object that realizes the audio

 final Player p=Manager.createRealizedPlayer(f.toURI().toURL());

  // Start the music

  p.start();

  // Create a Scanner object for taking input from cmd

  Scanner s=new Scanner(System.in);
  // Read a line and store it in st

  String st=s.nextLine();
   // If user types 's', stop the audio

   if(st.equals("s"))

   {

   p.stop();

   }

 }

}

Explanation:

Player is an interface in the javax.media package. As you cannot create an object for an interface directly some class implementing it is written and it's object is used. Manager is such a class that does the thing.

There are several states in which a Player will be. They are

UNREALIZED: Meaning, the player knows nothing about what it has to do (about the media)

REALIZING: Meaning, the player is realizing it's world (determining resources) necessary to do the job (playing the file)

REALIZED: Meaning, the player gets its resources to play the file and will also have some info about the type of the media etc.

PREFETCHING: It's getting ready now, it is knowing how could it play the file.

PREFETCHED: Now, it knows how to play the media. It can fire now.

START: Start the music, starts playing the file.

We have created a realized player, it does not mean that the UNREALIZED, REALIZING steps are skipped instead they are automatically done. The method createRealizedPlayer(URL url) takes a URL which points to the media file. For getting the URL object from a File object, you can used toURL() method in the java.io.File class, as it is deprecated we've called toURI() method which returns java.net.URI object and then the toURL() method in it.The user also needs to stop the music, so input is taken from the user.

Output however cannot be shown, you'll have to try it, remember that when executing the program give the complete path of the audio file otherwise ArrayIndexOutOfBounds exception will arise. The command might look like this

java AudioPlay "Path/to/file.mp3"

Credit Goes to Java Demo

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.