一応、再生できることを確認しております。
ただバグ取り等しておりませんので、ご了承の上参考になれば幸いです。
ワークスペースの当ソースフォルダー内のbinフォルダーに再生予定ファイルを格納してください。
また、KSwordOfHaste様より、ご回答されております様、wavファイルのみでしか試しておりません。
java
1import java.applet.AudioClip;
2import java.awt.EventQueue;
3import java.awt.event.ActionEvent;
4import java.awt.event.ActionListener;
5import java.io.File;
6import java.net.MalformedURLException;
7
8import javax.swing.JButton;
9import javax.swing.JCheckBox;
10import javax.swing.JFileChooser;
11import javax.swing.JFrame;
12import javax.swing.JPanel;
13import javax.swing.JTextField;
14import javax.swing.JToolBar;
15import javax.swing.border.EmptyBorder;
16
17public class Playscreen extends JFrame {
18
19 private JPanel contentPane;
20 private JTextField textField;
21 private static AudioClip ac;// ファイルを格納する変数
22 private static File file;// 取得ファイル名を格納する変数
23
24
25 public static void main(String[] args) throws MalformedURLException {
26 ac = java.applet.Applet.newAudioClip(Playscreen.class.getResource("再生ファイル名"));// binフォルダーに再生ファイルを格納する
27 EventQueue.invokeLater(new Runnable() {
28 public void run() {
29 try {
30 Playscreen frame = new Playscreen();
31 frame.setVisible(true);
32
33 } catch (Exception e) {
34 e.printStackTrace();
35 }
36 }
37 });
38 }
39
40 // GUI作成
41 public Playscreen() {
42 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
43 setBounds(100, 100, 532, 411);
44 contentPane = new JPanel();
45 contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
46 setContentPane(contentPane);
47 contentPane.setLayout(null);
48
49 JButton playButton = new JButton("PLAY");// 再生ボタン作成、ボタン押下イベント作成
50 playButton.addActionListener(new ActionListener() {
51 public void actionPerformed(ActionEvent e) {
52 textField.setText("fanfare.wav");
53 ac.play();
54 }
55 });
56 playButton.setBounds(35, 256, 155, 68);// ボタン配置
57 contentPane.add(playButton);
58
59 JButton stopButton = new JButton("STOP");// 停止ボタン作成、ボタン押下イベント作成
60 stopButton.addActionListener(new ActionListener() {
61 public void actionPerformed(ActionEvent e) {
62 textField.setText("");
63 ac.stop();
64 }
65 });
66 stopButton.setBounds(214, 256, 155, 68);// ボタン配置
67 contentPane.add(stopButton);
68
69 JCheckBox chckbxNewCheckBox = new JCheckBox("REPEAT");
70 chckbxNewCheckBox.setBounds(388, 280, 103, 21);
71 contentPane.add(chckbxNewCheckBox);
72
73 textField = new JTextField();
74 textField.setBounds(35, 84, 456, 68);
75 contentPane.add(textField);
76 textField.setColumns(10);
77
78 JToolBar toolBar = new JToolBar();
79 toolBar.setBounds(0, 0, 516, 21);
80 contentPane.add(toolBar);
81
82
83 // 表示のみ、実装予定として、選択したファイルを再生したい
84 JButton fileButton = new JButton("ファイル");
85 fileButton.addActionListener(new ActionListener() {
86 public void actionPerformed(ActionEvent e) {
87 System.out.println("ファイルボタンがクリックされました。");
88 JFileChooser filechooser = new JFileChooser();
89
90 int selected = filechooser.showOpenDialog(contentPane);
91 if (selected == JFileChooser.APPROVE_OPTION){
92 file = filechooser.getSelectedFile();
93 System.out.println("選択されたファイルは" + file);
94
95 }else if (selected == JFileChooser.CANCEL_OPTION){
96 System.out.println("キャンセルされました。");
97 }else if (selected == JFileChooser.ERROR_OPTION){
98 System.out.println("エラーまたは取り消しがありました。");
99 }
100 }
101 });
102 toolBar.add(fileButton);
103 }
104}
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/06/15 07:51