Play MP3 files usng Java API

0 votes

How can we make Java play an MP3 file for us ?

import sun.audio.*;
import java.io.*;

class tester {
 public static void main(String args[]) throws Exception {
  InputStream in=new FileInputStream("tester.mp3");
  AudioStream as=new AudioStream(in);
  AudioPlayer.player.start(as);
 }
}


This is the code I am trying to run but I get the below mentioned error :
enter image description here
May 10, 2018 in Java by Parth
• 4,630 points
5,667 views

1 answer to this question.

0 votes

I don't believe the java AudioStream class supports mp3 natively. According to this link it only supports AIFF, AU and WAV. You'll have to look into using another library to load and play an mp3 file.

String file="SOME//SONGFILE//PATH";
       FileInputStream fis     = new FileInputStream(file);
       BufferedInputStream bis = new BufferedInputStream(fis);
       player = new Player(bis);

       player.play();

The jar folders you must have: mp3plugin.jar and jlayer.jar (do google)

This site describes how to play a mp3 file in Java

answered May 10, 2018 by Rishabh
• 3,580 points

Related Questions In Java

0 votes
2 answers

How to play .mp3 and .wav in Java?

We can use Java API to play ...READ MORE

answered Dec 7, 2018 in Java by Sushmita
• 6,890 points
993 views
0 votes
2 answers

How to read the files using Java?

You can use collections. try (Stream<Path> filePathStream=Files.walk(Paths.get("/home/you/Desktop"))) { ...READ MORE

answered Jul 10, 2018 in Java by Sushmita
• 6,890 points
187 views
0 votes
1 answer

How to read text files from the Classpath in Java?

InputStream in = this.getClass().getClassLoader().getResourceAsStream("TextFile.txt"); InputStream in = this.getClass().getResourceAsStream("/TextFile.txt"); package ...READ MORE

answered May 8, 2018 in Java by Akrati
• 3,170 points
1,312 views
0 votes
2 answers

How can we add the local JAR files to the Maven Project in Java?

Firstly I would like to give credit ...READ MORE

answered Nov 5, 2018 in Java by Sushmita
• 6,890 points
2,115 views
0 votes
1 answer

How do I fix a NullPointerException?

When you declare a reference variable (i.e. ...READ MORE

answered Apr 13, 2018 in Java by Parth
• 4,630 points
232 views
0 votes
2 answers

Does the finally block always execute in Java?

public static void main(String[] args) { ...READ MORE

answered Aug 8, 2018 in Java by Sushmita
• 6,890 points
608 views
0 votes
1 answer

Need for finalize() in Java

finalize() is a method called by the ...READ MORE

answered May 8, 2018 in Java by code.reaper12
• 3,470 points
152 views
0 votes
1 answer

What is ArrayIndexOutOfBoundsException ?

If You check System.out.print(names.length); you will get 3 ...READ MORE

answered May 11, 2018 in Java by sharth
• 3,370 points
190 views
0 votes
1 answer

Decompiling Java Class Files

There are a few decompilers out there... ...READ MORE

answered Jun 1, 2018 in Java by Rishabh
• 3,580 points
197 views
0 votes
2 answers

Integer to String conversion in java

We can do this in 2 ways: String ...READ MORE

answered Jul 27, 2018 in Java by samarth295
• 2,200 points
211 views