Carregando e tocando um arquivo de áudio em um applet
Veja nesta dica o que fazer para carregar e tocar um arquivo de áudio em um applet.
Carregando e tocando um arquivo de áudio em um applet
Esta dica mostra um método para carregar e tocar um arquivo de áudio em um applet. Uma coisa interessante que você pode fazer com essa dica é tentar criar o seu próprio tocador de músicas.
import java.applet.*;<B><SPAN >import </SPAN></B><SPAN >java.awt.*;</SPAN><B><SPAN >import </SPAN></B><SPAN >java.awt.event.*;</SPAN><B><SPAN >import </SPAN></B><SPAN >javax.swing.*;</SPAN><B><SPAN >public class </SPAN></B><SPAN >LoadAudioAndPlay </SPAN><B><SPAN >extends </SPAN></B><SPAN >JApplet {</SPAN><B><SPAN >private </SPAN></B><SPAN >AudioClip sound1, sound2, currentSound; </SPAN><B><SPAN >private </SPAN></B><SPAN >JButton playSound, loopSound, stopSound;</SPAN><B><SPAN >private </SPAN></B><SPAN >JComboBox chooseSound;</SPAN><SPAN >//Carrega a imagem quando o applet é iniciado</SPAN><B><SPAN >public void </SPAN></B><SPAN >init()</SPAN><SPAN >{</SPAN><SPAN >Container c = getContentPane();</SPAN><SPAN >c.setLayout( </SPAN><B><SPAN >new </SPAN></B><SPAN >FlowLayout() );</SPAN><SPAN >String choices[] = { </SPAN><SPAN >"Welcome"</SPAN><SPAN >, </SPAN><SPAN >"Hi" </SPAN><SPAN >};</SPAN><SPAN >chooseSound = </SPAN><B><SPAN >new </SPAN></B><SPAN >JComboBox( choices );</SPAN><SPAN >chooseSound.addItemListener(</SPAN><B><SPAN >new </SPAN></B><SPAN >ItemListener() {</SPAN><B><SPAN >public void </SPAN></B><SPAN >itemStateChanged( ItemEvent e )</SPAN><SPAN >{</SPAN><SPAN >currentSound.stop();</SPAN><SPAN >currentSound =</SPAN><SPAN >chooseSound.getSelectedIndex() == </SPAN><SPAN >0 </SPAN><SPAN >?</SPAN><SPAN >sound1 : sound2;</SPAN><SPAN >}</SPAN><SPAN >}</SPAN><SPAN >);</SPAN><SPAN >c.add( chooseSound );</SPAN><SPAN >ButtonHandler handler = </SPAN><B><SPAN >new </SPAN></B><SPAN >ButtonHandler();</SPAN><SPAN >playSound = </SPAN><B><SPAN >new </SPAN></B><SPAN >JButton( </SPAN><SPAN >"Play" </SPAN><SPAN >);</SPAN><SPAN >playSound.addActionListener( handler );</SPAN><SPAN >c.add( playSound );</SPAN><SPAN >loopSound = </SPAN><B><SPAN >new </SPAN></B><SPAN >JButton( </SPAN><SPAN >"Repetir" </SPAN><SPAN >);</SPAN><SPAN >loopSound.addActionListener( handler );</SPAN><SPAN >c.add( loopSound );</SPAN><SPAN >stopSound = </SPAN><B><SPAN >new </SPAN></B><SPAN >JButton( </SPAN><SPAN >"Stop" </SPAN><SPAN >);</SPAN><SPAN >stopSound.addActionListener( handler );</SPAN><SPAN >c.add( stopSound );</SPAN><SPAN >sound1 = getAudioClip(</SPAN><SPAN >getDocumentBase(), </SPAN><SPAN >"inicio.wav" </SPAN><SPAN >);</SPAN><SPAN >sound2 = getAudioClip(</SPAN><SPAN >getDocumentBase(), </SPAN><SPAN >"hi.au" </SPAN><SPAN >);</SPAN><SPAN >currentSound = sound1;</SPAN><SPAN >}</SPAN><SPAN >// para a música quando o usuário troca de página </SPAN><B><SPAN >public void </SPAN></B><SPAN >stop()</SPAN><SPAN >{</SPAN><SPAN >currentSound.stop();</SPAN><SPAN >}</SPAN><B><SPAN >private class </SPAN></B><SPAN >ButtonHandler </SPAN><B><SPAN >implements </SPAN></B><SPAN >ActionListener {</SPAN><B><SPAN >public void </SPAN></B><SPAN >actionPerformed( ActionEvent e )</SPAN><SPAN >{</SPAN><B><SPAN >if </SPAN></B><SPAN >( e.getSource() == playSound ) c</SPAN><SPAN >currentSound.play();</SPAN><B><SPAN >else if </SPAN></B><SPAN >( e.getSource() == loopSound ) </SPAN><SPAN >currentSound.loop();</SPAN><B><SPAN >else if </SPAN></B><SPAN >( e.getSource() == stopSound ) </SPAN><SPAN >currentSound.stop();</SPAN><SPAN >}</SPAN><SPAN >}</SPAN><SPAN >}</SPAN>
Artigos relacionados
-
Artigo
-
Artigo
-
Artigo
-
Artigo
-
Artigo