This site requires JavaScript, please enable it in your browser!
Greenfoot back
ronald
ronald wrote ...

2021/2/17

Background Sound

1
2
ronald ronald

2021/2/17

#
java.io.FileNotFoundException: 01 - Moonlight.wav (Le fichier spécifié est introuvable) at java.base/java.io.FileInputStream.open0(Native Method) at java.base/java.io.FileInputStream.open(FileInputStream.java:219) at java.base/java.io.FileInputStream.<init>(FileInputStream.java:157) at java.desktop/com.sun.media.sound.SunFileReader.getAudioInputStream(SunFileReader.java:117) at java.desktop/javax.sound.sampled.AudioSystem.getAudioInputStream(AudioSystem.java:1062) at Background.act(Background.java:55) at greenfoot.core.Simulation.actWorld(Simulation.java:573) at greenfoot.core.Simulation.runOneLoop(Simulation.java:506) at greenfoot.core.Simulation.runContent(Simulation.java:193) at greenfoot.core.Simulation.run(Simulation.java:183) java.lang.NullPointerException at Background.act(Background.java:66) at greenfoot.core.Simulation.actWorld(Simulation.java:573) at greenfoot.core.Simulation.runOneLoop(Simulation.java:506) at greenfoot.core.Simulation.runContent(Simulation.java:193) at greenfoot.core.Simulation.run(Simulation.java:183) Here I erase and start again the error
ronald ronald

2021/2/17

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
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.Clip;
import javax.sound.sampled.UnsupportedAudioFileException;

/**
 * Write a description of class MyWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Background extends World
{

    /**
     * Constructor for objects of class MyWorld.
     * 
     */

    Actor btn01;
    
    private Actor valueBar;
    private int value = 50;
    private int maxValue = 100;
    
    static final Color TRANS = new Color(0,0,0,0);
    
    public Background()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(900, 600, 1); 

        valueBar = new SimpleActor();
        updateValueDisplay();

        addObject(valueBar,500,500);
        addObject(btn01 = getNewButton("BUTTON 01"),200,500);

        prepare();
    }
   
    public void act() 
    {
        // Add your action code here.
        File file = new File ("01 - Moonlight.wav");

        AudioInputStream audioInputStream = null;

        try 
        {
            audioInputStream = AudioSystem.getAudioInputStream(file);       
        } 
        catch (UnsupportedAudioFileException uafe) 
        {   
            uafe.printStackTrace();
        } 
        catch (IOException ioe) 
        {       
            ioe.printStackTrace();
        }

        AudioFormat format = audioInputStream.getFormat();     
        long frames = audioInputStream.getFrameLength();
        double durationInSeconds = (frames+0.0) / format.getFrameRate();

        int i = 0;
                
        if(Greenfoot.mouseClicked(btn01)) 
        {
            //valueBar.setVisible(true);
            try 
            {
                Clip clip = AudioSystem.getClip();
                clip.open(AudioSystem.getAudioInputStream(file));
                clip.start();

                while (i <= 100)
                {
                    Thread.sleep((long) (durationInSeconds*1000/100));
                    adjustValue(i-value);                    
                    i++;         
                }
                //System.out.println(durationInSeconds1);
            } 
            catch (Exception e)
            {
                e.printStackTrace();  
            }
        }

    }

    public void adjustValue(int amount)
    {
        value+=amount;
        if(value<0) value = 0;
        if(value>maxValue) value = maxValue;
        updateValueDisplay();
    }

    private void updateValueDisplay()
    {
        int wide = 200;
        int high = 20;

        GreenfootImage fullImg = new GreenfootImage(wide,high);
        fullImg.setColor(Color.GREEN);
        fullImg.fill();

        GreenfootImage colorBar = new GreenfootImage(wide,high);
        int percentage = wide*value/maxValue;
        colorBar.drawImage(fullImg,percentage-wide,0);

        GreenfootImage img = new GreenfootImage(wide+4,high+4);
        img.setColor(Color.WHITE);
        img.fill();
        img.setColor(Color.BLACK);
        img.drawRect(0,0,wide+3,high+3);
        img.drawImage(colorBar,2,2);
        valueBar.setImage(img);
    }    

    protected Actor getNewButton(String caption)
    {
        GreenfootImage base = new GreenfootImage(200,30);
        base.fill();
        base.setColor(Color.BLUE);
        base.fillRect(3,3,194,24);

        GreenfootImage text = new GreenfootImage(caption,24,Color.BLACK, TRANS);
        base.drawImage(text,100-text.getWidth()/2,15-text.getHeight()/2);
        base.setTransparency(128);

        Actor button = new SimpleActor();
        button.setImage(base);
        return button;
    }
    

    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
    }
}
danpost danpost

2021/2/17

#
Check file name matching. Maybe rename file without hyphens or spaces.
ronald ronald

2021/2/17

#
it does not work yet it is well written I even removed the dashes and spaces, it doesn't change anything
ronald ronald

2021/2/17

#
I am thinking that the Sounds and File directories are not the same thing
danpost danpost

2021/2/17

#
ronald wrote...
I am thinking that the Sounds and File directories are not the same thing
Quite sure that is a good conclusion. Move file or include path ( "sounds/01 - Moonlight.wav").
ronald ronald

2021/2/17

#
I hear the music but the progress bar does not move but no error
danpost danpost

2021/2/18

#
ronald wrote...
I hear the music but the progress bar does not move but no error
Add to end of while loop:
repaint();
ronald ronald

2021/2/18

#
yes I thought about repaint () but I put it wrong otherwise it works nickel the progress bar ends well at the end of the song thanks danpost
ronald ronald

2021/2/18

#
if I add 5 songs, I will not repeat the act () code 5 times. At the moment I am trying to figure out how to do it so as not to repeat.
danpost danpost

2021/2/18

#
ronald wrote...
if I add 5 songs, I will not repeat the act () code 5 times. At the moment I am trying to figure out how to do it so as not to repeat.
Place String filenames in an array and add an index counter.
ronald ronald

2021/2/18

#
ok a thought comes to my mind it can be 3 or 4 or 5 songs, how the code can know the number of songs inside the folder where I have to change each time the file names in the table I am thinking
danpost danpost

2021/2/18

#
ronald wrote...
how the code can know the number of songs inside the folder
int numArrayElements = arrayName.length;
ronald ronald

2021/2/18

#
thank you again danpost I will think about all this
You need to login to post a reply.
1
2