A ideia é: Começar a tocar uma música assim que apertar em OK (no JOptionPane), e quando clicar na imagem, ele parar e voltar à tela principal
Erro: não é possível clicar na tela antes do som acabar, nem fechar a janela, mesmo usando threads.
Codigo da Classe que toca o audio:
/*
- To change this license header, choose License Headers in Project Properties.
- To change this template file, choose Tools | Templates
- and open the template in the editor.
*/package Telas;
import java.io.FileInputStream; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.JOptionPane; import javazoom.jl.player.Player;/**
*
@author CaioVieira */ public class Tocar { FileInputStream in; Player mp3;public Tocar(){
try { in = new FileInputStream("/root/NetBeansProjects/AppTeleporte/src/sons/Praia.mp3"); System.out.println("achou"); mp3 = new Player(in); mp3.play(); System.out.println("chegou aq"); JOptionPane.showMessageDialog(null, ""); } catch (Exception ex) { Logger.getLogger(Praia.class.getName()).log(Level.SEVERE, null, ex); }}
}
Codigo da tela onde aparece a imagem:
/*
- To change this license header, choose License Headers in Project Properties.
- To change this template file, choose Tools | Templates
- and open the template in the editor.
*/
package Telas;import java.io.FileInputStream; import java.*; import java.io.FileNotFoundException; import java.io.IOException; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.JOptionPane; import javazoom.jl.decoder.JavaLayerException; import javazoom.jl.player.Player;//
/**
*
@author Caio Vieira
*/
public class Praia extends javax.swing.JFrame {/**
- Creates new form Praia
*/public Praia() throws JavaLayerException {
initComponents(); this.setVisible(true); JOptionPane.showMessageDialog(null, "Aperte Ok para começar a Música.\nAperte na imagem para parar."); td = new Thread((Runnable) new Tocar()); td.start();}
Thread td ;static FileInputStream in; static Player mp3; public void tocarThread(){ try {in = new FileInputStream("/root/NetBeansProjects/AppTeleporte/src/sons/Praia.mp3"); System.out.println("achou"); mp3 = new Player(in); mp3.play(); System.out.println("chegou aq"); JOptionPane.showMessageDialog(null, ""); } catch (Exception ex) { Logger.getLogger(Praia.class.getName()).log(Level.SEVERE, null, ex); }}
/**
This method is called from within the constructor to initialize the form.
WARNING: Do NOT modify this code. The content of this method is always
regenerated by the Form Editor. */ @SuppressWarnings(“unchecked”) // private void initComponents() {jLabel1 = new
javax.swing.JLabel();setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/Praia.jpg"))); // NOI18N jLabel1.addMouseListener(new java.awt.event.MouseAdapter() { public void mouseClicked(java.awt.event.MouseEvent evt) { jLabel1MouseClicked(evt); } });javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 1908, Short.MAX_VALUE) .addContainerGap()) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(jLabel1) .addGap(0, 0, Short.MAX_VALUE)) );pack();
}//private void jLabel1MouseClicked(java.awt.event.MouseEvent evt) { td.stop(); this.setVisible(false); Principal a = new Principal(); a.setVisible(true); a.setDefaultCloseOperation(DISPOSE_ON_CLOSE); }/**
@param args the command line arguments
/
public static void main(String args[]) {
/ Set the Nimbus look and feel /
//
/ If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
- For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if (“Nimbus”.equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Praia.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Praia.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Praia.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Praia.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
///* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { try { new Praia().setVisible(true); } catch (JavaLayerException ex) { Logger.getLogger(Praia.class.getName()).log(Level.SEVERE, null, ex); } } }); }// Variables declaration - do not modify private javax.swing.JLabel jLabel1; // End of variables declaration }