質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Swing

SwingはJavaに標準で付属するグラフィック関連のクラスライブラリを指します。

Eclipse

Eclipseは、IBM社で開発された統合開発環境のひとつです。2001年11月にオープンソース化されました。 たくさんのプラグインがあり自由に機能を追加をすることができるため、開発ツールにおける共通プラットフォームとして位置づけられています。 Eclipse自体は、Javaで実装されています。

Q&A

解決済

1回答

286閲覧

音声ファイルの再生ができない(例外処理でファイルが見つかりませんになってしまう)

usagilove

総合スコア9

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Swing

SwingはJavaに標準で付属するグラフィック関連のクラスライブラリを指します。

Eclipse

Eclipseは、IBM社で開発された統合開発環境のひとつです。2001年11月にオープンソース化されました。 たくさんのプラグインがあり自由に機能を追加をすることができるため、開発ツールにおける共通プラットフォームとして位置づけられています。 Eclipse自体は、Javaで実装されています。

1グッド

0クリップ

投稿2024/02/15 06:39

実現したいこと

イメージ説明
eclipseでwavファイルを再生したいのですが、実行するとcacthの処理になってしまいます。
music.javaというファイルにソースコードを書いており、同一階層に音声ファイルを置いています。

ファイルの置き場所の問題かと思い、
C:\Users\●●●\Downloads\0323.wavのような場所にファイルを置くと
java.io.FileNotFoundException: C:\Users\●●●\Downloads\0323.wav (アクセスが拒否されました。) at java.base/java.io.FileInputStream.open0(Native Method) at java.base/java.io.FileInputStream.open(FileInputStream.java:213) at java.base/java.io.FileInputStream.<init>(FileInputStream.java:152) at java.desktop/com.sun.media.sound.SunFileReader.getAudioInputStream(SunFileReader.java:117) at java.desktop/javax.sound.sampled.AudioSystem.getAudioInputStream(AudioSystem.java:1060) at music.main(music.java:30)
と、権限の問題でエラーになってしまい、ファイルの置き場所に困っています。
よろしくお願いします。

該当のソースコード

import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import javax.sound.sampled.AudioFormat; import javax.sound.sampled.AudioInputStream; import javax.sound.sampled.AudioSystem; import javax.sound.sampled.Clip; import javax.sound.sampled.DataLine; import javax.sound.sampled.LineEvent; import javax.sound.sampled.LineListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JProgressBar; class music { static Clip clip; static JFrame frame; static JButton playButton, stopButton; static JLabel counterLabel; static JProgressBar pBar; static int playCount, maxLength; public static void main(String[] args) { File file = new File("0323.wav"); if(file.exists()) { try { AudioInputStream stream = AudioSystem.getAudioInputStream(file); AudioFormat format = stream.getFormat(); DataLine.Info info = new DataLine.Info(Clip.class, format); clip = (Clip)AudioSystem.getLine(info); clip.open(stream); maxLength = clip.getFrameLength() - 1; clip.addLineListener(new LineListener() { public void update(LineEvent le) { if(le.getType() == LineEvent.Type.STOP) { if(maxLength * playCount == clip.getLongFramePosition()) { playButton.setEnabled(true); stopButton.setEnabled(false); } } } }); stream.close(); music cp2 = new music(); frame.setVisible(true); while(true) { counterLabel.setText(clip.getLongFramePosition() + "/" + maxLength); pBar.setValue((int)clip.getLongFramePosition() % (maxLength + 1)); try { Thread.sleep(500); } catch(Exception e) { e.printStackTrace(); } } } catch(Exception e) { e.printStackTrace(); } } else { System.out.println("ファイルが見つかりませんでした。"); System.exit(1); } } music() { frame = new JFrame("ClipPlayer2"); playCount = 1; playButton = new JButton("再生"); stopButton = new JButton("停止"); stopButton.setEnabled(false); ActionListener al = new ActionListener() { public void actionPerformed(ActionEvent ae) { if(ae.getSource() == playButton) { playButton.setEnabled(false); stopButton.setEnabled(true); if((long)(clip.getFrameLength() - 1) * playCount == clip.getLongFramePosition()) { clip.stop(); clip.setFramePosition(0); playCount++; } play(); } else { playButton.setEnabled(true); stopButton.setEnabled(false); clip.stop(); } } }; playButton.addActionListener(al); stopButton.addActionListener(al); counterLabel = new JLabel("0", JLabel.RIGHT); pBar = new JProgressBar(0, maxLength); frame.getContentPane().setLayout(new FlowLayout()); frame.add(playButton); frame.add(stopButton); frame.add(counterLabel); frame.add(pBar); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(350,100); frame.setLocationRelativeTo(null); } public void play() { new Thread() { public void run() { clip.start(); } }.start(); } }
heat👍を押しています

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

jimbe

2024/02/15 07:35

質問自体には関係ありませんが、 Swing のフレーム等を表示 (setVisible(true);) した後に main メソッド内で Swing のコンポーネント等を操作することは、異常動作の原因となりますのでやってはいけません。
guest

回答1

0

ベストアンサー

src フォルダは Eclipse のプロジェクトとしてのソースを置くための所ですので、実行時のフォルダ構造には関係ありません。必要なデータファイルはプロジェクト直下に置いてください。
C:/Users/~ の場合は Windows でのパーミッションの問題になると思いますが、単純に Eclipse のプロジェクト内にファイルを置きたい場合は、

java

1public class Music { 2 public static void main(String[] args) { 3 File file = new File("0323.wav"); 4 System.out.println(file.getAbsolutePath()); 5 } 6}

で (src フォルダは入っていないことが) 確認出来ます。
(Swing とは関係無いことですので、それらのコードを無くした最小のコードで試すのがコツかと思います。)


クラス名や構造を少し変更してみました。(wavファイル名もこちらに有ったものしていますのでご注意を。)

java

1import java.awt.FlowLayout; 2import java.io.File; 3import java.util.List; 4 5import javax.sound.sampled.*; 6import javax.swing.*; 7 8public class MusicFrame extends JFrame { 9 public static void main(String[] args) throws Exception { 10 File file = new File("musicbox.wav"); 11 if(!file.exists()) { 12 System.out.println("ファイルが見つかりませんでした。"); 13 System.exit(1); 14 } 15 16 try(AudioInputStream stream = AudioSystem.getAudioInputStream(file);) { 17 Clip clip = (Clip)AudioSystem.getLine(new DataLine.Info(Clip.class, stream.getFormat())); 18 clip.open(stream); 19 20 new MusicFrame(clip).setVisible(true); 21 } 22 } 23 24 MusicFrame(Clip clip) { 25 super("ClipPlayer2"); 26 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 27 setSize(350, 100); 28 setLocationRelativeTo(null); 29 30 int maxLength = clip.getFrameLength() - 1; 31 32 setLayout(new FlowLayout()); 33 34 JButton playButton = new JButton("再生"); 35 add(playButton); 36 37 JButton stopButton = new JButton("停止"); 38 stopButton.setEnabled(false); 39 add(stopButton); 40 41 JLabel counterLabel = new JLabel("0"); 42 add(counterLabel); 43 44 JProgressBar pBar = new JProgressBar(0, maxLength); 45 add(pBar); 46 47 playButton.addActionListener(e -> { 48 playButton.setEnabled(false); 49 stopButton.setEnabled(true); 50 51 if(clip.isRunning()) clip.stop(); 52 clip.setFramePosition(0); 53 clip.start(); 54 }); 55 56 stopButton.addActionListener(e -> { 57 playButton.setEnabled(true); 58 stopButton.setEnabled(false); 59 clip.stop(); 60 }); 61 62 clip.addLineListener(e -> { 63 if(e.getType() == LineEvent.Type.STOP) { 64 playButton.setEnabled(true); 65 stopButton.setEnabled(false); 66 } 67 }); 68 69 new SwingWorker<Void,Integer>() { 70 @Override 71 public Void doInBackground() throws Exception { 72 while(true) { 73 publish((int)clip.getLongFramePosition()); 74 try { 75 Thread.sleep(500); 76 } catch(Exception e) { 77 e.printStackTrace(); 78 } 79 } 80 } 81 @Override 82 protected void process(List<Integer> framePosList) { 83 int framePos = framePosList.get(framePosList.size()-1); //最後の一個 84 counterLabel.setText(framePos + "/" + maxLength); 85 pBar.setValue(framePos % (maxLength + 1)); 86 } 87 }.execute(); 88 } 89}

投稿2024/02/15 07:32

編集2024/02/15 08:42
jimbe

総合スコア12648

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

usagilove

2024/02/15 07:54

助かりました!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問