Visualizzazione dei risultati da 1 a 3 su 3

Discussione: applet sound

  1. #1
    Utente di HTML.it
    Registrato dal
    Dec 2008
    Messaggi
    6

    applet sound

    salve a tutti...ho gia provato a cercare nel forum ma non ho trovato niente di funzionante...devo realizzare un semplice player audio fatto con una apllet..hoprovato svariate volte ma non mi riesce farlo suonare...ho provato con due codici diversi ma non mi riesce farne partire nemmeno uno dei due...sapete aiutarmi???

    codice 1:
    -----------------------------------------------------------------------------------
    import java.applet.AudioClip;
    import java.awt.event.ItemListener;
    import java.awt.event.ItemEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import java.awt.FlowLayout;
    import javax.swing.JApplet;
    import javax.swing.JButton;
    import javax.swing.JComboBox;

    public class NewClass extends JApplet
    {
    private AudioClip sound1, sound2, currentSound;
    private JButton play, loop, stop;
    private JComboBox sound;

    @Override
    public void init()
    {
    setLayout (new FlowLayout());

    String[] choices = {"Suono 1", "Suono 2"};

    sound = new JComboBox (choices);

    sound.addItemListener
    (
    new ItemListener()
    {
    public void itemStateChanged (ItemEvent event)
    {
    currentSound.stop();
    currentSound = sound.getSelectedIndex() == 0 ? sound1 : sound2;
    }
    }
    );
    add (sound);

    ButtonHandler handler = new ButtonHandler();

    play = new JButton ("Play");
    play.addActionListener (handler);
    add (play);

    loop = new JButton ("Loop");
    loop.addActionListener (handler);
    add (loop);

    stop = new JButton ("Stop");
    stop.addActionListener (handler);
    add (stop);

    sound1 = getAudioClip (getDocumentBase(), "dr.wav");
    sound2 = getAudioClip (getDocumentBase(), "miami69.wav");
    currentSound = sound1;
    }

    public void stop()
    {
    currentSound.stop();
    }

    private class ButtonHandler implements ActionListener
    {
    public void actionPerformed (ActionEvent event)
    {
    if (event.getSource() == play)
    currentSound.play();
    else if (event.getSource() == loop)
    currentSound.loop();
    else if (event.getSource() == stop)
    currentSound.stop();
    }
    }
    }
    -------------------------------------------------------------------

    codice 2:

    import java.applet.AudioClip;
    import java.awt.Button;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JApplet;


    public class player extends JApplet implements ActionListener{

    AudioClip corr;
    Button b1= new Button("play");
    Button b2= new Button("stop");

    @Override
    public void init() {
    setLayout(null);
    add(b1);
    add(b2);
    b1.addActionListener(this);
    b2.addActionListener(this);
    b1.setBounds(0, 0, 50, 20);
    b2.setBounds(0, 21, 50, 20);
    corr=getAudioClip(getCodeBase(),"dr.wav");

    }

    @Override
    public void start(){
    System.out.println(getCodeBase());

    }

    public void actionPerformed(ActionEvent e) {
    if(e.getSource()==b1){
    if(corr==null)System.out.println("non vado");
    else{
    System.out.println("vado");
    corr.play();
    corr.loop();
    }
    }
    if(e.getSource()==b2){
    if(corr==null)System.out.println("cazzo");
    else{
    corr.stop();
    }
    }
    }


    }

  2. #2
    Utente di HTML.it
    Registrato dal
    Aug 2002
    Messaggi
    8,013

    Re: applet sound

    Non mi sono soffermato sulla classe player, comunque, questa funziona
    codice:
    import java.applet.AudioClip;
    import java.awt.event.ItemListener;
    import java.awt.event.ItemEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import java.awt.FlowLayout;
    import javax.swing.JApplet;
    import javax.swing.JButton;
    import javax.swing.JComboBox;
    
    public class SoundJApplet extends JApplet
    {
    	private AudioClip sound1, sound2, currentSound;
    	private JButton play, loop, stop;
    	private JComboBox sound;
    
        @Override
    	public void init()
    	{
    		setLayout (new FlowLayout());
    
    		String[] choices = {"Suono 1", "Suono 2"};
    
    		sound = new JComboBox (choices);
    
    		sound.addItemListener
    		(
    			new ItemListener()
    			{
    				public void itemStateChanged (ItemEvent event)
    				{
    					currentSound.stop();
    					currentSound = sound.getSelectedIndex() == 0 ? sound1 : sound2;
    				}
    			}
    		);
    		add (sound);
    
    		ButtonHandler handler = new ButtonHandler();
    
    		play = new JButton ("Play");
    		play.addActionListener (handler);
    		add (play);
    
    		loop = new JButton ("Loop");
    		loop.addActionListener (handler);
    		add (loop);
    
    		stop = new JButton ("Stop");
    		stop.addActionListener (handler);
    		add (stop);
    
    		sound1 = getAudioClip (getDocumentBase(), "dr.wav");
    		sound2 = getAudioClip (getDocumentBase(), "miami69.wav");
    		currentSound = sound1;
    	}
    
    	public void stop()
    	{
    		currentSound.stop();
    	}
    
    	private class ButtonHandler implements ActionListener
    	{
    		public void actionPerformed (ActionEvent event)
    		{
    			if (event.getSource().equals(play))
    				currentSound.play();
    			else if (event.getSource().equals(loop))
    				currentSound.loop();
    			else if (event.getSource().equals(stop))
    				currentSound.stop();
    		}
    	}
    }
    tanto per intenderci, l'unica cosa che ho cambiato nel tuo codice è l'implementazione di ButtonHandler... gli oggetti si comparano con equals
    <´¯)(¯`¤._)(¯`»ANDREA«´¯)(_.¤´¯)(¯`>
    "The answer to your question is: welcome to tomorrow"

  3. #3
    Utente di HTML.it L'avatar di andbin
    Registrato dal
    Jan 2006
    residenza
    Italy
    Messaggi
    18,284

    Re: Re: applet sound

    Originariamente inviato da Andrea1979
    gli oggetti si comparano con equals
    Sì ... equals() se implementato appropriatamente in una classe, permette di confrontare il "contenuto" degli oggetti. Ma nel caso dei listener è tipico e corretto confrontare i reference per verificare quale è la "sorgente" dell'evento!!!

    Il problema non credo che sia nel codice (non l'ho analizzato in dettaglio né provato) ma credo che sia nel formato di quei file audio. Il tutorial dice che AudioClip supporta solo il formato AU 8 bit, µ-law, 8000 Hz, one-channel. Ora ... non so dire se nelle più recenti versioni di Java è stato ampliato il supporto anche per altri file es. wav (ammetto che non ho mai provato ...).
    Andrea, andbin.devSenior Java developerSCJP 5 (91%) • SCWCD 5 (94%)
    java.util.function Interfaces Cheat SheetJava Versions Cheat Sheet

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.