V
vitafit
Gast
Guten Tag.
Ich habe dieses einfache Beispiel im Internet gefunden, um einen Ton bzw. ein Lied abzuspielen.
Jedoch würde ich den Quellcode kürzen wollen, um nur das Lied abzuspielen und nicht den Benutzer fragen, ob er das Lied starten, stoppen oder um Hilfe bitten will.
Hier ist der Code, der auch funktioniert:
Wenn ich das Programm starte und in die Console 1 eingebe, startet das Lied auch, wenn ich jedoch den Code auf den Teil der nur für das Abspielen ist begrenze, funktioniert es nicht mehr.
Es wird kein Ton abgespielt und in der Console erscheint in Eclipse in der Leiste <terminated>.
Ich hoffe ihr könnt mir weiterhelfen.
Ich habe dieses einfache Beispiel im Internet gefunden, um einen Ton bzw. ein Lied abzuspielen.
Jedoch würde ich den Quellcode kürzen wollen, um nur das Lied abzuspielen und nicht den Benutzer fragen, ob er das Lied starten, stoppen oder um Hilfe bitten will.
Hier ist der Code, der auch funktioniert:
Java:
import java.io.*; // for File
import java.util.*; // for Scanner
import javax.sound.sampled.*; // allows you to use the sound classes
public class GameMusic
{
// SET UP
static File file;
static AudioInputStream stream;
static Clip music;
/* NOTE: "throws Exception" is important for setup. Your program will NOT compile if
* this is not included. */
public static void main(String[] args) throws Exception
{
// FILE - part of set-up
file = new File("techno.wav");
// miscellaneous test stuff
Scanner in = new Scanner(System.in);
boolean exit = false;
do
{
System.out.print("What to do? (0 = help) ");
int choice = in.nextInt();
switch (choice)
{
case 0: help(); break;
case 1: start(); break;
case 2: stop(); break;
case 3: exit = true; break;
default: System.out.println("Invalid choice.");
}
}
while (!exit);
}
public static void help()
{
System.out.println("0 - help");
System.out.println("1 - start");
System.out.println("2 - stop");
System.out.println("3 - exit");
}
/* NOTE: "throws Exception" is important for starting. Your program will NOT compile if
* this is not included. */
public static void start() throws Exception
{
// SET UP
stream = AudioSystem.getAudioInputStream(file);
music = AudioSystem.getClip();
// PLAY
music.open(stream);
music.start();
music.loop(Clip.LOOP_CONTINUOUSLY);
}
public static void stop()
{
// STOP
music.stop();
music.close();
}
}
Es wird kein Ton abgespielt und in der Console erscheint in Eclipse in der Leiste <terminated>.
Java:
import java.io.*; // for File
import java.util.*; // for Scanner
import javax.sound.sampled.*; // allows you to use the sound classes
public class SoundClipTest
{
/* NOTE: "throws Exception" is important for setup. Your program will NOT compile if
* this is not included. */
public static void main(String[] args) throws Exception
{
System.out.println("Musik wird abgespielt");
try
{
File file = new File("techno.wav");
AudioInputStream stream = AudioSystem.getAudioInputStream(file);
Clip music = AudioSystem.getClip();
music.open(stream);
music.loop(Clip.LOOP_CONTINUOUSLY);
music.start();
}
catch (Exception e) {}
}
}
Ich hoffe ihr könnt mir weiterhelfen.