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,640 points
9,422 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

Hope it helps!!

If not then its recommended to join our Java training class and learn about Java in detail.

Thank You!!

answered May 10, 2018 by Rishabh
• 3,620 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,920 points
5,999 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,920 points
1,054 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,190 points
2,776 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,920 points
11,573 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,640 points
844 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,920 points
2,006 views
0 votes
1 answer

Need for finalize() in Java

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

answered May 9, 2018 in Java by code.reaper12
• 3,500 points
645 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
909 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,620 points
1,220 views
0 votes
2 answers

Integer to String conversion in java

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

answered Jul 28, 2018 in Java by samarth295
• 2,220 points
1,059 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP