How to set the volume in this simple example?
I saw different approaches before but I don't know how to adapt them for my simple snippet.
Related Questions:
Changing Volume with JLayer
Changing volume in Java when using JLayer
Code listing:
package view.sound;
import java.io.FileInputStream;
import javazoom.jl.player.Player;
public class Audio extends Thread {
String fileLocation = "src/EgyptianTavernFullofGuitarists_1.mp3";
boolean loop = true;
Player player;
#Override
public void run() {
try {
do {
FileInputStream buff = new FileInputStream(fileLocation);
player = new Player(buff);
player.play();
} while (loop);
} catch (Exception ioe) {
}
}
public void close(){
loop = false;
player.close();
this.interrupt();
}
}
Related
I was trying to create a basic music player in Java using JLayer library, but it doesn't seem to work. Nothing is played when I open a file (.mp3) through JFileChooser. Below is the code for my application. Please tell me what's wrong in it.
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import javax.swing.JFileChooser;
import javazoom.jl.player.Player;
import javazoom.jl.decoder.JavaLayerException;
class MusicPlayer
{
public void Player()
{
JFileChooser chooser = new JFileChooser();
if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
try
{
File track = chooser.getSelectedFile();
FileInputStream trackstream = new FileInputStream(track);
BufferedInputStream bufferedtrack = new BufferedInputStream(trackstream);
try
{
Player player = new Player(bufferedtrack);
}
catch(JavaLayerException e)
{
System.out.println("Can't open file!");
}
}
catch(FileNotFoundException e)
{
}
}
else {
}
}
}
public class PlayerApp {
public static void main(String[] args) {
new MusicPlayer().Player();
}
}
You did miss out player.play() of the Player to start playing the requested file.
try
{
Player player = new Player(bufferedtrack);
player.play(); // add this line
}
catch(JavaLayerException e)
{
System.out.println("Can't open file!");
}
Also, method names are commonly written in camel-case (first letter lowercase). I recommend to rename your method void Player() to something like void loadDialogAndPlayFile().
You forgot to call the player.play(); method after Player player = new Player(bufferedtrack);
I'm new in Java (still learning) all the other works have gone well but only this animation gives my headache and the coffee won't even help=(!
I should make an animation of Javaman (10 gif pictures named as T1, T2,...T10) I should use Thread, MediaTracker-class and addImage-method. Then I should specify the speed of the animation by using sleep-method (I used join-method if that's right??).
(MY JAVA CODE GOES LIKE THIS)
import java.applet.Applet;
import java.awt.*;
public class Animaatio extends Applet implements Runnable {
Image[] images = null;
MediaTracker tracker = null;
int current = 0;
Thread animThread;
#Override
public void init() {
// Creating a new media tracker, to track loading images
tracker = new MediaTracker(this);
// Creating an array of ten images
images = new Image[10];
// Downloading the images
for (int i = 0; i < 10; i++) {
// Loading the images
images[i] = getImage(getCodeBase(), (i + 1) + "T.gif");
tracker.addImage(images[i], 0);
}
try {
tracker.waitForAll();
} catch (InterruptedException e) {
}
}
#Override
public void start() {
if (animThread == null) {
animThread = new Thread(this);
animThread.start();
}
try {
animThread.join();
} catch (InterruptedException e) {
}
}
#Override
public void paint(Graphics g) {
g.drawImage(images[current++], 0, 0, this);
}
#Override
public void run() {
while (true) {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
}
}
}
}
The problem is that I don't see any animation just an empty applet viewer which just keeps running. Problem might be caused if the images are storaged in the wrong place? If someone could wreally help me, I would be really thankful for my knight=).
The last time I asked how to use background audio correctly, I was told I had to use a thread. Now, I have done that, yet I still have the same problem: It does not run the rest of the program until the song completes, yet I need it to play in the background! help anyone? (Sorry for no comments in the code. If you all really feel there needs to be some to help you out I will update it with comments).
import javax.swing.JOptionPane;
import javax.swing.JDialog;
import java.io.*;
import javax.sound.sampled.*;
public class PokemonDemo3 implements LineListener
{
private boolean done = false;
public void update(LineEvent event)
{
if(event.getType() == LineEvent.Type.STOP || event.getType() == LineEvent.Type.CLOSE)
{
done = true;
}
}
public void waitonfinish() throws InterruptedException
{
while(!done)
{
Thread.sleep(1000);
}
}
public static void playSound(final String url)
{
try
{
Clip clip = AudioSystem.getClip();
AudioInputStream inputStream = AudioSystem.getAudioInputStream(new File(url));
PokemonDemo3 control = new PokemonDemo3();
clip.addLineListener(control);
clip.open(inputStream);
clip.start();
control.waitonfinish();
}
catch (Exception e)
{
System.err.println(e.getMessage());
}
}
public static void main(String [] args)
{
String holder;
int response;
String[] options = {"Boy", "Girl"};
Pokemon intro = new Pokemon();
playSound("/Users/2018658/Desktop/pokemon.wav");
}
I have a uk.co.mmscomputing applet for scanning through web application. It works fine with Cannon DR-9080C scanner. Recently I purchased Kodak i2600 scanner and it is not working with my applet. The scanner scans all pages in the tray when i click Acquire for the first time but when I load more pages and click Acquire again, I get "Java Stopped Working" error.
I tried setting up uk.co.mmscomputing again but it was not helpful.
package uk.co.mmscomputing.application.imageviewer;
import java.io.*;
import java.awt.image.*;
import javax.swing.*;
import uk.co.mmscomputing.device.scanner.*;
import uk.co.mmscomputing.device.twain.*;
public class ScannerTab extends ImageTab implements ScannerListener{
static private int no=1;
public ScannerTab(java.util.Properties properties){
super(properties);
}
#Override
protected void setButtonPanel(JPanel gui){
super.setButtonPanel(gui);
try{
Scanner scanner=Scanner.getDevice(); // get a device and set GUI panel up
if(scanner!=null){
gui.add(scanner.getScanGUI());
scanner.addListener(this);
if(scanner instanceof TwainScanner){
TwainIdentity[] list=((TwainScanner)scanner).getIdentities();
for(int i=0;i<list.length;i++){
System.out.println(list[i].toString());
}
}
}
}catch(Exception e){
System.out.println("9\b"+getClass().getName()+".setButtonPanel:\n\t"+e);
}
}
public void update(ScannerIOMetadata.Type type, final ScannerIOMetadata metadata){
if(type.equals(ScannerIOMetadata.ACQUIRED)){ // acquired BufferedImage
final BufferedImage image=metadata.getImage();// make reference copy here to avoid race condition
new Thread(){
#Override
public void run(){
try{
addImage("scan_"+(no++),image);
}catch(Exception e){
System.out.println("9\b"+getClass().getName()+".update:\n\t"+e);
System.err.println(getClass().getName()+".update:\n\t"+e);
e.printStackTrace();
}
}
}.start();
}else if(type.equals(ScannerIOMetadata.FILE)){ // acquired image as file (twain only for the time being)
final File file=metadata.getFile(); // make reference copy here to avoid race condition
new Thread(){
#Override
public void run(){
try{
open(file.getPath());
}catch(Exception e){
System.out.println("9\b"+getClass().getName()+".update:\n\t"+e);
System.err.println(getClass().getName()+".update:\n\t"+e);
e.printStackTrace();
}finally{
if(!file.delete()){
System.out.println("9\b"+getClass().getName()+".update:\n\tCould not delete: "+file.getPath());
System.err.println(getClass().getName()+".update:\n\tCould not delete: "+file.getPath());
}
}
}
}.start();
}else if(type.equals(ScannerIOMetadata.NEGOTIATE)){
negotiate(metadata);
}else if(type.equals(ScannerIOMetadata.STATECHANGE)){
System.out.println("Scanner State "+metadata.getStateStr());
if(metadata instanceof TwainIOMetadata){ // TWAIN only !
if(metadata.isState(TwainConstants.STATE_TRANSFERREADY)){ // state = 6
TwainSource source = ((TwainIOMetadata)metadata).getSource();
try{
TwainImageInfo imageInfo=new TwainImageInfo(source);
imageInfo.get();
System.out.println(imageInfo.toString());
}catch(Exception e){
System.out.println("3\b"+getClass().getName()+".update:\n\tCannot retrieve image information.\n\t"+e);
}
try{
TwainImageLayout imageLayout=new TwainImageLayout(source);
imageLayout.get();
System.out.println(imageLayout.toString());
}catch(Exception e){
System.out.println("3\b"+getClass().getName()+".update:\n\tCannot retrieve image layout.\n\t"+e);
}
}else if(metadata.isState(TwainConstants.STATE_TRANSFERRING)){ // state = 7
TwainSource source = ((TwainIOMetadata)metadata).getSource();
try{
int[] tweis=new int[0x1240-0x1200];
for(int i=0;i<tweis.length;i++){tweis[i]=0x1200+i;}
TwainExtImageInfo imageInfo=new TwainExtImageInfo(source,tweis);
imageInfo.get();
System.out.println(imageInfo.toString());
}catch(Exception e){
System.out.println("3\b"+getClass().getName()+".update:\n\tCannot retrieve extra image information.\n\t"+e);
}
}
}
}else if(type.equals(ScannerIOMetadata.INFO)){
System.out.println(metadata.getInfo());
}else if(type.equals(ScannerIOMetadata.EXCEPTION)){
System.out.println("9\b"+metadata.getException().getMessage());
metadata.getException().printStackTrace();
}
}
private void negotiate(ScannerIOMetadata metadata){
ScannerDevice sd=metadata.getDevice(); // SANE & TWAIN
try{
sd.setShowUserInterface(false);
System.out.println("9\b"+e.getMessage());
}
if(metadata instanceof TwainIOMetadata){ // TWAIN only!
TwainSource source=((TwainIOMetadata)metadata).getSource();
try{
TwainCapability[] caps=source.getCapabilities(); // print out all the capabilities
for(int i=0;i<caps.length;i++){
System.out.println(caps[i].toString());
}
}catch(Exception e){
System.out.println("9\b"+e.getMessage());
}
}
}
}
I've searched and read another topics but i still can't solve my problems. I had one java main class and one java jframe. I wanna add jslider to change volume in my mp3 player. What should i do?
my_player2_func
class my_player2_func{
static Player player;
static void play() {
try {
FileInputStream fe = new FileInputStream(my_player2_main.str);
player = new Player(fe);
} catch (Exception ex) {
System.out.println(ex);
}
new Thread(new Runnable() {
#Override
public void run() {
try {
player.play();
} catch (Exception ex) {
System.out.println(ex);
}
}
}).start();
}
static void stop() {
if (player != null)
player.close();
} }
my_player2_main
public my_player2_main() {
initComponents();
}
static String str;
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
my_player2_func.play();
}
private void jSlider1StateChanged(javax.swing.event.ChangeEvent evt) {//what should i do here?}
JLayer is a decoder. By its very definition it does not know anything about volume, thats the job of the audio hardware that plays the sound.
Your problem stems from the fact that you are using the Player and defaul JavaSoundAudioDevice classes, which were meant as short, simple examples how to play an MP3, not as building blocks for a full fledged audio player.
You will have to copy or modify the source of JavaSoundAudioDevice (part of JLayer source) and hack it to support volume control. Et voila you can control volume.