Package

Source Code of PlayChar

import java.io.InputStream;

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


// from http://www.dreamincode.net/forums/topic/58448-how-to-play-mp3-files/
public class PlayChar {

  private Clip clip;
   

  public PlayChar(InputStream audioFile) {
    init(audioFile);
  }

  private void init(InputStream audioFile) {
    try {
      AudioInputStream soundStream = AudioSystem
          .getAudioInputStream(audioFile);
      AudioFormat audioFormat = soundStream.getFormat();
      // define line information based on line type,
      // encoding and frame sizes of audio file
      DataLine.Info dataLineInfo = new DataLine.Info(Clip.class,
          AudioSystem.getTargetFormats(
              AudioFormat.Encoding.ULAW, audioFormat),
          audioFormat.getFrameSize(), audioFormat.getFrameSize() * 2);

      // make sure sound system supports data line
      if (!AudioSystem.isLineSupported(dataLineInfo)) {
        System.err.println("Unsupported Clip File!");
        throw new IllegalArgumentException("Erro ao abrir arquivo de midia. ");
        //return;
      }
      // get clip line resource
      clip = (Clip) AudioSystem.getLine(dataLineInfo);
      // open audio clip and get required system resources
      clip.open(soundStream);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  public void play() {
    clip.start();
  }

   
 
}
TOP

Related Classes of PlayChar

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.