java 怎么放音乐??
4个回答
展开全部
这是我根据我的朋友的Java发声的代码写的按键发声的类
可以编译执行后会弹出窗口 然后按字母或其他什么键(太多了,自己去看)可以发出声音。。。
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* MusicPlayerWindows.java
*
* Created on 2009-12-22, 19:03:45
*/
import java.awt.event.KeyEvent;
import javax.sound.midi.*;
import javax.swing.*;
import java.awt.*;
/**
*
* @author Administrator
*/
public class MusicPlayerWindows extends javax.swing.JFrame {
private static final long serialVersionUID = 1L;
private MusicPanel mp = new MusicPanel();
private Sequencer sequencer = null;
private Sequence seq = null;
private Track track = null;
private static int code = 0;
/** Creates new form MusicPlayerWindows */
public MusicPlayerWindows() {
this.initMusic();
initComponents();
this.setContentPane(mp);
this.setVisible(true);
}
/** 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")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
this.setTitle("MusicPlayer");
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(java.awt.event.KeyEvent evt) {
formKeyPressed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void formKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_formKeyPressed
// sound(Sequence.PPQ,1,1,1,127);
int keyCode = evt.getKeyCode();
if (keyCode == KeyEvent.VK_F1) {
sound(Sequence.PPQ,1,1,1,60);
}
if (keyCode == KeyEvent.VK_F2) {
sound(Sequence.PPQ,1,1,1,61);
}
if (keyCode == KeyEvent.VK_F3) {
sound(Sequence.PPQ,1,1,1,62);
}
if (keyCode == KeyEvent.VK_F4) {
sound(Sequence.PPQ,1,1,1,63);
}
if (keyCode == KeyEvent.VK_F5) {
sound(Sequence.PPQ,1,1,1,64);
}
if (keyCode == KeyEvent.VK_F6) {
sound(Sequence.PPQ,1,1,1,65);
}
if (keyCode == KeyEvent.VK_F7) {
sound(Sequence.PPQ,1,1,1,66);
}
if (keyCode == KeyEvent.VK_F8) {
sound(Sequence.PPQ,1,1,1,67);
}
if (keyCode == KeyEvent.VK_F9) {
sound(Sequence.PPQ,1,1,1,68);
}
if (keyCode == KeyEvent.VK_F10) {
sound(Sequence.PPQ,1,1,1,69);
}
if (keyCode == KeyEvent.VK_F11) {
sound(Sequence.PPQ,1,1,1,70);
}
if (keyCode == KeyEvent.VK_F12) {
sound(Sequence.PPQ,1,1,1,71);
}
if (keyCode == KeyEvent.VK_BACK_QUOTE ) {
sound(Sequence.PPQ,1,1,1,72);
}
if (keyCode == KeyEvent.VK_1) {
sound(Sequence.PPQ,1,1,1,73);
}
if (keyCode == KeyEvent.VK_2) {
sound(Sequence.PPQ,1,1,1,74);
}
if (keyCode == KeyEvent.VK_3) {
sound(Sequence.PPQ,1,1,1,75);
}
if (keyCode == KeyEvent.VK_4) {
sound(Sequence.PPQ,1,1,1,76);
}
if (keyCode == KeyEvent.VK_5) {
sound(Sequence.PPQ,1,1,1,77);
}
if (keyCode == KeyEvent.VK_6) {
sound(Sequence.PPQ,1,1,1,78);
}
if (keyCode == KeyEvent.VK_7) {
sound(Sequence.PPQ,1,1,1,79);
}
if (keyCode == KeyEvent.VK_8) {
sound(Sequence.PPQ,1,1,1,80);
}
if (keyCode == KeyEvent.VK_9) {
sound(Sequence.PPQ,1,1,1,81);
}
if (keyCode == KeyEvent.VK_0) {
sound(Sequence.PPQ,1,1,1,82);
}
if (keyCode == KeyEvent.VK_MINUS) {
sound(Sequence.PPQ,1,1,1,83);
}
if (keyCode == KeyEvent.VK_EQUALS) {
sound(Sequence.PPQ,1,1,1,84);
}
if (keyCode == KeyEvent.VK_Q) {
sound(Sequence.PPQ,1,1,1,85);
}
if (keyCode == KeyEvent.VK_W) {
sound(Sequence.PPQ,1,1,1,86);
}
if (keyCode == KeyEvent.VK_E) {
sound(Sequence.PPQ,1,1,1,87);
}
if (keyCode == KeyEvent.VK_R) {
sound(Sequence.PPQ,1,1,1,88);
}
if (keyCode == KeyEvent.VK_T) {
sound(Sequence.PPQ,1,1,1,89);
}
if (keyCode == KeyEvent.VK_Y) {
sound(Sequence.PPQ,1,1,1,90);
}
if (keyCode == KeyEvent.VK_U) {
sound(Sequence.PPQ,1,1,1,91);
}
if (keyCode == KeyEvent.VK_I) {
sound(Sequence.PPQ,1,1,1,92);
}
if (keyCode == KeyEvent.VK_O) {
sound(Sequence.PPQ,1,1,1,93);
}
if (keyCode == KeyEvent.VK_P) {
sound(Sequence.PPQ,1,1,1,94);
}
if (keyCode == KeyEvent.VK_OPEN_BRACKET) {
sound(Sequence.PPQ,1,1,1,95);
}
if (keyCode == KeyEvent.VK_CLOSE_BRACKET) {
sound(Sequence.PPQ,1,1,1,96);
}
if (keyCode == KeyEvent.VK_A) {
sound(Sequence.PPQ,1,1,1,97);
}
if (keyCode == KeyEvent.VK_S) {
sound(Sequence.PPQ,1,1,1,98);
}
if (keyCode == KeyEvent.VK_D) {
sound(Sequence.PPQ,1,1,1,99);
}
if (keyCode == KeyEvent.VK_F) {
sound(Sequence.PPQ,1,1,1,100);
}
if (keyCode == KeyEvent.VK_G) {
sound(Sequence.PPQ,1,1,1,101);
}
if (keyCode == KeyEvent.VK_H) {
sound(Sequence.PPQ,1,1,1,102);
}
if (keyCode == KeyEvent.VK_J) {
sound(Sequence.PPQ,1,1,1,103);
}
if (keyCode == KeyEvent.VK_K) {
sound(Sequence.PPQ,1,1,1,104);
}
if (keyCode == KeyEvent.VK_L) {
sound(Sequence.PPQ,1,1,1,105);
}
if (keyCode == KeyEvent.VK_SEMICOLON) {
sound(Sequence.PPQ,1,1,1,106);
}
if (keyCode == KeyEvent.VK_QUOTE) {
sound(Sequence.PPQ,1,1,1,107);
}
if (keyCode == KeyEvent.VK_Z) {
sound(Sequence.PPQ,1,1,1,108);
}
if (keyCode == KeyEvent.VK_X) {
sound(Sequence.PPQ,1,1,1,109);
}
if (keyCode == KeyEvent.VK_C) {
sound(Sequence.PPQ,1,1,1,110);
}
if (keyCode == KeyEvent.VK_V) {
sound(Sequence.PPQ,1,1,1,111);
}
if (keyCode == KeyEvent.VK_B) {
sound(Sequence.PPQ,1,1,1,112);
}
if (keyCode == KeyEvent.VK_N) {
sound(Sequence.PPQ,1,1,1,113);
}
if (keyCode == KeyEvent.VK_M) {
sound(Sequence.PPQ,1,1,1,114);
}
if (keyCode == KeyEvent.VK_COMMA) {
sound(Sequence.PPQ,1,1,1,115);
}
if (keyCode == KeyEvent.VK_PERIOD) {
sound(Sequence.PPQ,1,1,1,116);
}
if (keyCode == KeyEvent.VK_SLASH) {
sound(Sequence.PPQ,1,1,1,117);
}
}//GEN-LAST:event_formKeyPressed
private void sound(float divisionType,int chan,int resolution,int numTracks,int one){
try{
seq = new Sequence(divisionType, resolution, numTracks);
track = seq.createTrack();
track.add(makeEvent(144, chan, one, 100, 0));
track.add(makeEvent(176, chan, 127, 0, 0));
track.add(makeEvent(128, chan, one, 100, 2));
sequencer.setSequence(seq);
sequencer.setTempoInBPM(100);
sequencer.start();
}catch(Exception e){
e.printStackTrace();
}
}
private MidiEvent makeEvent(int comd, int chan, int one, int two,
int tick) {
MidiEvent event = null;
ShortMessage a = new ShortMessage();
try {
a.setMessage(comd, chan, one, two);
event = new MidiEvent(a, tick);
} catch (InvalidMidiDataException e) {
e.printStackTrace();
}
return event;
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new MusicPlayerWindows().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
// End of variables declaration//GEN-END:variables
private class MusicPanel extends JPanel implements ControllerEventListener {
private static final long serialVersionUID = 1L;
private boolean msg = false;
public void controlChange(ShortMessage event) {
msg=true;
this.repaint();
}
public void paintComponent(Graphics g) {
g.setColor(Color.WHITE);
g.fillRect(0, 0, this.getWidth(), this.getHeight());
if (msg) {
g.setColor(new Color((int) (Math.random() * 250), (int) (Math.random() * 250), (int) (Math.random() * 250)));
g.fillOval((int) (Math.random() * this.getWidth())-50, (int) (Math.random() * this.getHeight())-50, 100, 100);
g.setColor(new Color((int) (Math.random() * 250), (int) (Math.random() * 250), (int) (Math.random() * 250)));
g.fillOval((int) (Math.random() * this.getWidth())-50, (int) (Math.random() * this.getHeight())-50, 100, 100);
g.setColor(new Color((int) (Math.random() * 250), (int) (Math.random() * 250), (int) (Math.random() * 250)));
g.drawOval((int) (Math.random() * this.getWidth())-50, (int) (Math.random() * this.getHeight())-50, 100, 100);
g.setColor(new Color((int) (Math.random() * 250), (int) (Math.random() * 250), (int) (Math.random() * 250)));
g.fillOval((int) (Math.random() * this.getWidth())-50, (int) (Math.random() * this.getHeight())-50, 100, 100);
g.setColor(new Color((int) (Math.random() * 250), (int) (Math.random() * 250), (int) (Math.random() * 250)));
g.drawOval((int) (Math.random() * this.getWidth())-50, (int) (Math.random() * this.getHeight())-50, 100, 100);
g.setColor(new Color((int) (Math.random() * 250), (int) (Math.random() * 250), (int) (Math.random() * 250)));
g.fillOval((int) (Math.random() * this.getWidth())-50, (int) (Math.random() * this.getHeight())-50, 100, 100);
g.setColor(new Color((int) (Math.random() * 250), (int) (Math.random() * 250), (int) (Math.random() * 250)));
g.drawOval((int) (Math.random() * this.getWidth())-50, (int) (Math.random() * this.getHeight())-50, 100, 100);
g.setColor(new Color((int) (Math.random() * 250), (int) (Math.random() * 250), (int) (Math.random() * 250)));
g.fillOval((int) (Math.random() * this.getWidth())-50, (int) (Math.random() * this.getHeight())-50, 100, 100);
g.setColor(new Color((int) (Math.random() * 250), (int) (Math.random() * 250), (int) (Math.random() * 250)));
g.drawOval((int) (Math.random() * this.getWidth())-50, (int) (Math.random() * this.getHeight())-50, 100, 100);
g.setColor(new Color((int) (Math.random() * 250), (int) (Math.random() * 250), (int) (Math.random() * 250)));
g.fillOval((int) (Math.random() * this.getWidth())-50, (int) (Math.random() * this.getHeight())-50, 100, 100);
msg = false;
}
}
}
private void initMusic(){
try{
sequencer = MidiSystem.getSequencer();
int[] eventsIWant = {127};
sequencer.addControllerEventListener(mp, eventsIWant);
sequencer.open();
}catch(Exception e){
e.printStackTrace();
}
}
}
可以编译执行后会弹出窗口 然后按字母或其他什么键(太多了,自己去看)可以发出声音。。。
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* MusicPlayerWindows.java
*
* Created on 2009-12-22, 19:03:45
*/
import java.awt.event.KeyEvent;
import javax.sound.midi.*;
import javax.swing.*;
import java.awt.*;
/**
*
* @author Administrator
*/
public class MusicPlayerWindows extends javax.swing.JFrame {
private static final long serialVersionUID = 1L;
private MusicPanel mp = new MusicPanel();
private Sequencer sequencer = null;
private Sequence seq = null;
private Track track = null;
private static int code = 0;
/** Creates new form MusicPlayerWindows */
public MusicPlayerWindows() {
this.initMusic();
initComponents();
this.setContentPane(mp);
this.setVisible(true);
}
/** 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")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
this.setTitle("MusicPlayer");
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(java.awt.event.KeyEvent evt) {
formKeyPressed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void formKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_formKeyPressed
// sound(Sequence.PPQ,1,1,1,127);
int keyCode = evt.getKeyCode();
if (keyCode == KeyEvent.VK_F1) {
sound(Sequence.PPQ,1,1,1,60);
}
if (keyCode == KeyEvent.VK_F2) {
sound(Sequence.PPQ,1,1,1,61);
}
if (keyCode == KeyEvent.VK_F3) {
sound(Sequence.PPQ,1,1,1,62);
}
if (keyCode == KeyEvent.VK_F4) {
sound(Sequence.PPQ,1,1,1,63);
}
if (keyCode == KeyEvent.VK_F5) {
sound(Sequence.PPQ,1,1,1,64);
}
if (keyCode == KeyEvent.VK_F6) {
sound(Sequence.PPQ,1,1,1,65);
}
if (keyCode == KeyEvent.VK_F7) {
sound(Sequence.PPQ,1,1,1,66);
}
if (keyCode == KeyEvent.VK_F8) {
sound(Sequence.PPQ,1,1,1,67);
}
if (keyCode == KeyEvent.VK_F9) {
sound(Sequence.PPQ,1,1,1,68);
}
if (keyCode == KeyEvent.VK_F10) {
sound(Sequence.PPQ,1,1,1,69);
}
if (keyCode == KeyEvent.VK_F11) {
sound(Sequence.PPQ,1,1,1,70);
}
if (keyCode == KeyEvent.VK_F12) {
sound(Sequence.PPQ,1,1,1,71);
}
if (keyCode == KeyEvent.VK_BACK_QUOTE ) {
sound(Sequence.PPQ,1,1,1,72);
}
if (keyCode == KeyEvent.VK_1) {
sound(Sequence.PPQ,1,1,1,73);
}
if (keyCode == KeyEvent.VK_2) {
sound(Sequence.PPQ,1,1,1,74);
}
if (keyCode == KeyEvent.VK_3) {
sound(Sequence.PPQ,1,1,1,75);
}
if (keyCode == KeyEvent.VK_4) {
sound(Sequence.PPQ,1,1,1,76);
}
if (keyCode == KeyEvent.VK_5) {
sound(Sequence.PPQ,1,1,1,77);
}
if (keyCode == KeyEvent.VK_6) {
sound(Sequence.PPQ,1,1,1,78);
}
if (keyCode == KeyEvent.VK_7) {
sound(Sequence.PPQ,1,1,1,79);
}
if (keyCode == KeyEvent.VK_8) {
sound(Sequence.PPQ,1,1,1,80);
}
if (keyCode == KeyEvent.VK_9) {
sound(Sequence.PPQ,1,1,1,81);
}
if (keyCode == KeyEvent.VK_0) {
sound(Sequence.PPQ,1,1,1,82);
}
if (keyCode == KeyEvent.VK_MINUS) {
sound(Sequence.PPQ,1,1,1,83);
}
if (keyCode == KeyEvent.VK_EQUALS) {
sound(Sequence.PPQ,1,1,1,84);
}
if (keyCode == KeyEvent.VK_Q) {
sound(Sequence.PPQ,1,1,1,85);
}
if (keyCode == KeyEvent.VK_W) {
sound(Sequence.PPQ,1,1,1,86);
}
if (keyCode == KeyEvent.VK_E) {
sound(Sequence.PPQ,1,1,1,87);
}
if (keyCode == KeyEvent.VK_R) {
sound(Sequence.PPQ,1,1,1,88);
}
if (keyCode == KeyEvent.VK_T) {
sound(Sequence.PPQ,1,1,1,89);
}
if (keyCode == KeyEvent.VK_Y) {
sound(Sequence.PPQ,1,1,1,90);
}
if (keyCode == KeyEvent.VK_U) {
sound(Sequence.PPQ,1,1,1,91);
}
if (keyCode == KeyEvent.VK_I) {
sound(Sequence.PPQ,1,1,1,92);
}
if (keyCode == KeyEvent.VK_O) {
sound(Sequence.PPQ,1,1,1,93);
}
if (keyCode == KeyEvent.VK_P) {
sound(Sequence.PPQ,1,1,1,94);
}
if (keyCode == KeyEvent.VK_OPEN_BRACKET) {
sound(Sequence.PPQ,1,1,1,95);
}
if (keyCode == KeyEvent.VK_CLOSE_BRACKET) {
sound(Sequence.PPQ,1,1,1,96);
}
if (keyCode == KeyEvent.VK_A) {
sound(Sequence.PPQ,1,1,1,97);
}
if (keyCode == KeyEvent.VK_S) {
sound(Sequence.PPQ,1,1,1,98);
}
if (keyCode == KeyEvent.VK_D) {
sound(Sequence.PPQ,1,1,1,99);
}
if (keyCode == KeyEvent.VK_F) {
sound(Sequence.PPQ,1,1,1,100);
}
if (keyCode == KeyEvent.VK_G) {
sound(Sequence.PPQ,1,1,1,101);
}
if (keyCode == KeyEvent.VK_H) {
sound(Sequence.PPQ,1,1,1,102);
}
if (keyCode == KeyEvent.VK_J) {
sound(Sequence.PPQ,1,1,1,103);
}
if (keyCode == KeyEvent.VK_K) {
sound(Sequence.PPQ,1,1,1,104);
}
if (keyCode == KeyEvent.VK_L) {
sound(Sequence.PPQ,1,1,1,105);
}
if (keyCode == KeyEvent.VK_SEMICOLON) {
sound(Sequence.PPQ,1,1,1,106);
}
if (keyCode == KeyEvent.VK_QUOTE) {
sound(Sequence.PPQ,1,1,1,107);
}
if (keyCode == KeyEvent.VK_Z) {
sound(Sequence.PPQ,1,1,1,108);
}
if (keyCode == KeyEvent.VK_X) {
sound(Sequence.PPQ,1,1,1,109);
}
if (keyCode == KeyEvent.VK_C) {
sound(Sequence.PPQ,1,1,1,110);
}
if (keyCode == KeyEvent.VK_V) {
sound(Sequence.PPQ,1,1,1,111);
}
if (keyCode == KeyEvent.VK_B) {
sound(Sequence.PPQ,1,1,1,112);
}
if (keyCode == KeyEvent.VK_N) {
sound(Sequence.PPQ,1,1,1,113);
}
if (keyCode == KeyEvent.VK_M) {
sound(Sequence.PPQ,1,1,1,114);
}
if (keyCode == KeyEvent.VK_COMMA) {
sound(Sequence.PPQ,1,1,1,115);
}
if (keyCode == KeyEvent.VK_PERIOD) {
sound(Sequence.PPQ,1,1,1,116);
}
if (keyCode == KeyEvent.VK_SLASH) {
sound(Sequence.PPQ,1,1,1,117);
}
}//GEN-LAST:event_formKeyPressed
private void sound(float divisionType,int chan,int resolution,int numTracks,int one){
try{
seq = new Sequence(divisionType, resolution, numTracks);
track = seq.createTrack();
track.add(makeEvent(144, chan, one, 100, 0));
track.add(makeEvent(176, chan, 127, 0, 0));
track.add(makeEvent(128, chan, one, 100, 2));
sequencer.setSequence(seq);
sequencer.setTempoInBPM(100);
sequencer.start();
}catch(Exception e){
e.printStackTrace();
}
}
private MidiEvent makeEvent(int comd, int chan, int one, int two,
int tick) {
MidiEvent event = null;
ShortMessage a = new ShortMessage();
try {
a.setMessage(comd, chan, one, two);
event = new MidiEvent(a, tick);
} catch (InvalidMidiDataException e) {
e.printStackTrace();
}
return event;
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new MusicPlayerWindows().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
// End of variables declaration//GEN-END:variables
private class MusicPanel extends JPanel implements ControllerEventListener {
private static final long serialVersionUID = 1L;
private boolean msg = false;
public void controlChange(ShortMessage event) {
msg=true;
this.repaint();
}
public void paintComponent(Graphics g) {
g.setColor(Color.WHITE);
g.fillRect(0, 0, this.getWidth(), this.getHeight());
if (msg) {
g.setColor(new Color((int) (Math.random() * 250), (int) (Math.random() * 250), (int) (Math.random() * 250)));
g.fillOval((int) (Math.random() * this.getWidth())-50, (int) (Math.random() * this.getHeight())-50, 100, 100);
g.setColor(new Color((int) (Math.random() * 250), (int) (Math.random() * 250), (int) (Math.random() * 250)));
g.fillOval((int) (Math.random() * this.getWidth())-50, (int) (Math.random() * this.getHeight())-50, 100, 100);
g.setColor(new Color((int) (Math.random() * 250), (int) (Math.random() * 250), (int) (Math.random() * 250)));
g.drawOval((int) (Math.random() * this.getWidth())-50, (int) (Math.random() * this.getHeight())-50, 100, 100);
g.setColor(new Color((int) (Math.random() * 250), (int) (Math.random() * 250), (int) (Math.random() * 250)));
g.fillOval((int) (Math.random() * this.getWidth())-50, (int) (Math.random() * this.getHeight())-50, 100, 100);
g.setColor(new Color((int) (Math.random() * 250), (int) (Math.random() * 250), (int) (Math.random() * 250)));
g.drawOval((int) (Math.random() * this.getWidth())-50, (int) (Math.random() * this.getHeight())-50, 100, 100);
g.setColor(new Color((int) (Math.random() * 250), (int) (Math.random() * 250), (int) (Math.random() * 250)));
g.fillOval((int) (Math.random() * this.getWidth())-50, (int) (Math.random() * this.getHeight())-50, 100, 100);
g.setColor(new Color((int) (Math.random() * 250), (int) (Math.random() * 250), (int) (Math.random() * 250)));
g.drawOval((int) (Math.random() * this.getWidth())-50, (int) (Math.random() * this.getHeight())-50, 100, 100);
g.setColor(new Color((int) (Math.random() * 250), (int) (Math.random() * 250), (int) (Math.random() * 250)));
g.fillOval((int) (Math.random() * this.getWidth())-50, (int) (Math.random() * this.getHeight())-50, 100, 100);
g.setColor(new Color((int) (Math.random() * 250), (int) (Math.random() * 250), (int) (Math.random() * 250)));
g.drawOval((int) (Math.random() * this.getWidth())-50, (int) (Math.random() * this.getHeight())-50, 100, 100);
g.setColor(new Color((int) (Math.random() * 250), (int) (Math.random() * 250), (int) (Math.random() * 250)));
g.fillOval((int) (Math.random() * this.getWidth())-50, (int) (Math.random() * this.getHeight())-50, 100, 100);
msg = false;
}
}
}
private void initMusic(){
try{
sequencer = MidiSystem.getSequencer();
int[] eventsIWant = {127};
sequencer.addControllerEventListener(mp, eventsIWant);
sequencer.open();
}catch(Exception e){
e.printStackTrace();
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
java是一种软件的总称,就像塞班软件。你需要安装用java语言编写的音乐软件,然后才能播放。但好像没有用java编写的自带音乐软件吧
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
安全卫士360上有个手机的播放软件可以下载,叫TT——player,貌似
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
给你个例子
public class testsong extends JFrame implements ActionListener{
AudioStream as;
FileInputStream fileau;
XYLayout xYLayout1 = new XYLayout();
JButton jButton1 = new JButton();
// AudioPlayer.player.stop(as);
testsong(){
try {
jbInit();
} catch (Exception ex) {
ex.printStackTrace();
}
}
public static void main(String[] args) {
testsong t = new testsong();
t.setVisible(true);
t.setSize(200, 200);
}
private void jbInit() throws Exception {
this.getContentPane().setLayout(xYLayout1);
jButton1.setText("jButton1");
jButton1.addActionListener(this);
this.getContentPane().add(jButton1, new XYConstraints(175, 46, 151, 113));
}
public void actionPerformed(ActionEvent e) {
try {
fileau = new FileInputStream("quite.midi");
} catch (FileNotFoundException ex) {
System.out.println(ex);
}
try {
as = new AudioStream(fileau);
AudioPlayer.player.start(as);
} catch (IOException ex1) {
System.out.println(ex1);
}
}
}
public class testsong extends JFrame implements ActionListener{
AudioStream as;
FileInputStream fileau;
XYLayout xYLayout1 = new XYLayout();
JButton jButton1 = new JButton();
// AudioPlayer.player.stop(as);
testsong(){
try {
jbInit();
} catch (Exception ex) {
ex.printStackTrace();
}
}
public static void main(String[] args) {
testsong t = new testsong();
t.setVisible(true);
t.setSize(200, 200);
}
private void jbInit() throws Exception {
this.getContentPane().setLayout(xYLayout1);
jButton1.setText("jButton1");
jButton1.addActionListener(this);
this.getContentPane().add(jButton1, new XYConstraints(175, 46, 151, 113));
}
public void actionPerformed(ActionEvent e) {
try {
fileau = new FileInputStream("quite.midi");
} catch (FileNotFoundException ex) {
System.out.println(ex);
}
try {
as = new AudioStream(fileau);
AudioPlayer.player.start(as);
} catch (IOException ex1) {
System.out.println(ex1);
}
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询