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

2012/2/27

Reading Sound Folder

Busch2207 Busch2207

2012/2/27

#
Hi guys! I want to read out, which sounds are in the 'sound'- folder... In Greenfoot it works fine, but when I try out the exportet version, it always stops. Can someone help me, please? Here's the source code:
import greenfoot.*;
import java.io.*;

public class Music 
{
    private static GreenfootSound Music;
    private static String ActualMusic;
    private static int ActualMusicNumber;

    public static final void stopMusic()
    {
        if(Music!=null && Music.isPlaying())
            Music.stop();
    }

    public static final void playMusic(String SoundName)
    {
        if(Music!=null && Music.isPlaying())
            Music.stop();
        ActualMusic=SoundName;
        Music = new GreenfootSound(SoundName);
        Music.play();
    }

    public static final void playNextMusic()
    {
        File File_Music = new File("sounds");
        String[] Files = File_Music.list();
        if(Files.length==0)
            return;
        ActualMusicNumber = (ActualMusicNumber+1)%Files.length;
        playMusic(Files[ActualMusicNumber]);
    }

    public static final void playMusicBefore()
    {
        File File_Music = new File("sounds");
        String[] Files = File_Music.list();
        if(Files.length==0)
            return;
        ActualMusicNumber--;
        if(ActualMusicNumber<0)
            ActualMusicNumber = Files.length-1;
        playMusic(Files[ActualMusicNumber]);
    }

    public static final void playMusic()
    {
        File File_Music = new File("sounds");
        String[] Files = File_Music.list();
        if(Files.length==0)
            return;
        ActualMusicNumber = Greenfoot.getRandomNumber(Files.length)%Files.length;
        playMusic(Files[ActualMusicNumber]);
    }
}
davmac davmac

2012/2/27

#
You can't really do that. Specifically, you can't use the 'File' class from an exported app - you're just not allowed to access the local file system. Also, in an exported app, everything is in a jar file and no longer in a directory. The best bet is probably to just include a text file with a list of all the files, and read that (using techniques mentioned on the discussion forum a little while ago, see Builderboy2005's post in this discussion.
Busch2207 Busch2207

2012/2/27

#
That was my first thought, too... I was just hoping that there is an easier variable solution... But thank you for your reply! :)
You need to login to post a reply.