import javax.sound.midi.*;
import javax.swing.*;
import java.awt.event.*;

public class BPlay implements ActionListener {
	public static void main(String[] args) {
		BPlay stereo = new BPlay();
		stereo.play();
	}
	public void play() {
		JFrame myfirstFrame = new JFrame();
		JButton pl = new JButton("Click me to listen to something");
		

            myfirstFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
   		pl.addActionListener(this);

		myfirstFrame.getContentPane().add(pl);
		myfirstFrame.setSize(300, 300);
		myfirstFrame.setVisible(true);

	}
	public void actionPerformed(ActionEvent event) {
		try {
			Sequencer mfpl = MidiSystem.getSequencer();
			mfpl.open();
			Sequence mfs = new Sequence(Sequence.PPQ, 4);

			Track mft = mfs.createTrack();
				
			ShortMessage a = new ShortMessage();
			a.setMessage(144, 1, 72, 100);
			MidiEvent non = new MidiEvent(a, 1);
			mft.add(non);

			ShortMessage b = new ShortMessage();
			b.setMessage(128, 1, 72, 100);
			MidiEvent noff = new MidiEvent(b, 1);
			mft.add(noff);

			mfpl.setSequence(mfs);

			mfpl.start();
		}
		catch (Exception ex) {
			ex.printStackTrace();
		}
	}
}

This is my son's first app, how did you like it?

Recommended Answers

All 4 Replies

You are free to change the underlined numbers -

import javax.sound.midi.*;
import javax.swing.*;
import java.awt.event.*;

public class BPlay implements ActionListener {
	public static void main(String[] args) {
		BPlay stereo = new BPlay();
		stereo.play();
	}
	public void play() {
		JFrame myfirstFrame = new JFrame();
		JButton pl = new JButton("Click me to listen to something");
		

            myfirstFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
   		pl.addActionListener(this);

		myfirstFrame.getContentPane().add(pl);
		myfirstFrame.setSize(300, 300);
		myfirstFrame.setVisible(true);

	}
	public void actionPerformed(ActionEvent event) {
		try {
			Sequencer mfpl = MidiSystem.getSequencer();
			mfpl.open();
			Sequence mfs = new Sequence(Sequence.PPQ, 4);

			Track mft = mfs.createTrack();
				
			ShortMessage a = new ShortMessage();
			a.setMessage(144, 1, [U]72[/U], 100);
			MidiEvent non = new MidiEvent(a, [U]1[/U]);
			mft.add(non);

			ShortMessage b = new ShortMessage();
			b.setMessage(128, 1, [U]72[/U], 100);
			MidiEvent noff = new MidiEvent(b, [U]1[/U]);
			mft.add(noff);

			mfpl.setSequence(mfs);

			mfpl.start();
		}
		catch (Exception ex) {
			ex.printStackTrace();
		}
	}
}

Not sure what you are expecting from us to hear. Program is functional. However there are things that could b changed, like using more define names for parameters and method, splitting application in more classes to make it more clear what component is doing what.
So please, when you create post make sure you clearly state what you expect from us to advice on.

Member Avatar for iamthwee

1. How old is your son?
2. What are you expecting him to learn?

1. How old is your son?
2. What are you expecting him to learn?

1. ten

2. he knows (basic) - python, java, c#, html, css

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.