Java Advanced Utils





  • Play MP3 Songs in Java
  • Mute System with java code
  • UnMute System with java code
  • Reading the file with the Line Number  


  • Play MP3 Songs in Java
              Hi this utility talks about how to play your favorite MP3 songs using simple java  code. Once you are familiar with the code , this code can be used with the Swing application we can prepare MP3 music player and same code snippet can be used in the web application and create MP3 player with attracted stop,pause and play buttons.

 Code snippet to play MP3 Song

package com;

import java.io.File;
import java.io.IOException;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.SourceDataLine;

public class PlayMySong {

/**
 * @param args
 */
Object lock = new Object();
static boolean slow=false;
public static void main(String[] args) {
PlayMySong player = new PlayMySong();
player.playMySong("D:/SO/music.mp3");
}
public void playMySong(String songName){
AudioInputStream din = null;
try {
File file = new File(songName);
AudioInputStream in = AudioSystem.getAudioInputStream(file);
AudioFormat baseFormat = in.getFormat();
AudioFormat decodedFormat = new AudioFormat(
AudioFormat.Encoding.PCM_SIGNED,
baseFormat.getSampleRate(), 16, baseFormat.getChannels(),
baseFormat.getChannels() * 2, baseFormat.getSampleRate(),
false);
din = AudioSystem.getAudioInputStream(decodedFormat, in);
DataLine.Info info = new DataLine.Info(SourceDataLine.class, decodedFormat);
SourceDataLine line = (SourceDataLine) AudioSystem.getLine(info);
if(line != null) {
line.open(decodedFormat);
byte[] data = new byte[4096];
// Start
line.start();
int nBytesRead;
synchronized (lock) {
while ((nBytesRead = din.read(data, 0, data.length)) != -1) {
while (slow) {
if(line.isRunning()) {
line.stop();
}
/*try {
lock.wait();
}
catch(InterruptedException e) {
}*/
}
if(!line.isRunning()) {
line.start();
}
line.write(data, 0, nBytesRead);
}
}
// Stop
line.drain();
line.stop();
line.close();
din.close();
}
}
catch(Exception e) {
e.printStackTrace();
}
finally {
if(din != null) {
try { din.close(); } catch(IOException e) { }
}
}
}
}
                


  • Reading the file with the Line Number
         This utility describes about .. how to get a desired line from a file and also how many lines in the file.

Class : LineNumberReader


Line Number Reader Class is from the java.io package. By using this class we will be able to get the entire line by giving line number and also we will be able to get the total number of lines does file has.


package com;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.LineNumberInputStream;
import java.io.LineNumberReader;

public class FileLinesCount {

/**
 * @param args
 */
public static void main(String[] args) {
// TODO Auto-generated method stub
try{
FileInputStream fis = new FileInputStream("D:/TestStatic.java");
BufferedReader br = new BufferedReader(new InputStreamReader(fis));

LineNumberReader lnr = new LineNumberReader(br);
int lineNumber = 0;
while(lnr.readLine()!=null ){
//System.out.println(lnr.getLineNumber());
lineNumber = lnr.getLineNumber();
}
System.out.println("total lines file has :"+lineNumber);
}catch(Exception e){
e.printStackTrace();
}
}

}

Input file : TestStatic.java
public class TestStatic{
public static void main(String ar[]){
System.out.println("hi");
}
}

Output
-------
total lines file has :5

And  read a line of text with the desired line number , I have modified the same code and with one extra if condition to match line number in the while loop


package com;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.LineNumberReader;

public class FileLinesCount {

/**
 * @param args
 */
public static void main(String[] args) {
// TODO Auto-generated method stub
try{
FileInputStream fis = new FileInputStream("D:/TestStatic.java");
BufferedReader br = new BufferedReader(new InputStreamReader(fis));

LineNumberReader lnr = new LineNumberReader(br);


int lineNumber = 0;
int desiredLine =2;
String desiredLineOfText=null;
while(lnr.readLine()!=null ){
//System.out.println(lnr.getLineNumber());
lineNumber = lnr.getLineNumber();
if(lineNumber==desiredLine){
desiredLineOfText = lnr.readLine();
}
}
System.out.println("total lines file has :"+lineNumber);
System.out.println("desired text :"+desiredLineOfText);

}catch(Exception e){
e.printStackTrace();
}
}

}

output
------
total lines file has :5
desired text : System.out.println("hi");