اذهب إلى المحتوى
  • 0

استثناء FileNotFoundException عند تشغيل ملف صوتي في Java

لينا الزعبي

السؤال

import java.awt.*;

import java.awt.event.*;

import java.io.*;

import javax.sound.sampled.*;

import javax.swing.*;



public class Drapeaux extends JPanel implements ActionListener {

        private int flag = 0; // Indicateur pour savoir quel drapeau doit être affiché (0 = Liban, 1 = Palestine, 2 = Arabie Saoudite)

        private Clip clip; // Objet pour jouer la chanson

        private boolean showMan = false; // Indicateur pour savoir si l'homme doit être affiché



        public Drapeaux() {

                try {

                        // Charger le fichier audio

                        File file = new File(System.getProperty("user.home") + File.separator + "Desktop" + File.separator + "أغنيه الأرض لنا والقدس لنا بدون موسيقى وبدون ايقاع (256 kbps) (shabakngy.com).mp3");

                        AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(file);

                        clip = AudioSystem.getClip();

                        clip.open(audioInputStream);

                   
        } catch (Exception e) {

                        e.printStackTrace();

                   
        }

           
    }



        public void paintComponent(Graphics g) {

                super.paintComponent(g);

                int width = getWidth();

                int height = getHeight();



                if (flag == 1) {

                        // Dessiner le drapeau de la Palestine

                        g.setColor(Color.BLACK);

                        g.fillRect(0, 0, width, height / 3);

                        g.setColor(Color.WHITE);

                        g.fillRect(0, height / 3, width, height / 3);

                        g.setColor(Color.GREEN);

                        g.fillRect(0, height * 2 / 3, width, height / 3);

                        g.setColor(Color.RED);

                        int[] xPoints = {0, width / 4, 0};

                        int[] yPoints = {0, height / 2, height};

                        g.fillPolygon(xPoints, yPoints, 3);

                   
        } else if (flag == 2) {

                        // Dessiner le drapeau de l'Arabie Saoudite

                        g.setColor(new Color(0x006C35));

                        g.fillRect(0, 0, width, height);



                        // Dessiner l'épée

                        g.setColor(Color.WHITE);

                        int swordY = height * 2 / 3;

                        int swordWidth = width * 2 / 3;

                        int swordHeight = height / 40;

                        int swordX = (width - swordWidth) / 2;

                        g.fillRect(swordX, swordY, swordWidth, swordHeight);



                        // Dessiner l'inscription

                        Font font = new Font("Thuluth", Font.PLAIN, height / 4);

                        g.setFont(font);

                        FontMetrics metrics = g.getFontMetrics(font);

                        String text = "لا إله إلا الله محمد رسول الله";

                        int textX = (width - metrics.stringWidth(text)) / 2;

                        int textY = swordY - metrics.getHeight() + metrics.getAscent();

                        g.drawString(text, textX, textY);

                   
        } else {

                       // Dessiner le drapeau du Liban

                       g.setColor(Color.RED);

                       g.fillRect(0 ,0 ,width ,height);
             

                       g.setColor(Color.WHITE);
             

                       g.fillRect(0 ,height/3 ,width ,height/3);
             

                       g.setColor(Color.GREEN);
             

                       int treeHeight=height/3;
             

                       int treeWidth=treeHeight/2;
             

                       int treeX=(width-treeWidth)/2;
             

                       int treeY=(height-treeHeight)/2;
             

                       g.fillOval(treeX ,treeY ,treeWidth ,treeHeight);
             

                   
        }



                if (showMan) {

                      // Dessiner le corps

                      g.setColor(Color.BLACK);

                      g.drawLine(width / 2 ,height/4 ,width/2 ,height*3/4);



                      // Dessiner les jambes

                      g.drawLine(width/2 ,height*3/4 ,width*3/8 ,height);
             

                      g.drawLine(width/2 ,height*3/4 ,width*5/8 ,height);
             



                      // Dessiner les bras

                      g.drawLine(width/2 ,height/2 ,width*3/8 ,height*1/4);
             

                      g.drawLine(width/2 ,height/2 ,width*5/8 ,height*1/4);
             



                      // Dessiner la tête

                      g.drawOval(width*7/16,height/8,width/8,height/8);
             

                   
        }

           
    }



        public void actionPerformed(ActionEvent e) {

                if (e.getActionCommand().equals("Afficher la Palestine")) {

                       flag =(flag ==1 ) ? flag :1 ;
             

                       repaint();
             

                   
        } else if (e.getActionCommand().equals("Afficher l'Arabie Saoudite")) {

                       flag =(flag ==2 ) ? flag :2 ;
             

                       repaint();
             

                   
        } else if (e.getActionCommand().equals("Jouer la chanson")) {

                       if(clip != null) {

                               clip.setFramePosition(0);
                 

                               clip.start();
                 

                           
            }

                   
        } else if(e.getActionCommand().equals("Afficher l'homme")) {

                       showMan=!showMan;

                   
        } else if(e.getActionCommand().equals("Afficher le Liban")) {

                       flag=0;

                   
        }

               repaint();
         

           
    }

       public static void main(String[] args) {

               JFrame frame= new JFrame("Drapeaux");
         

               frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         

               frame.setSize(400 ,300);


               Drapeaux drapeaux= new Drapeaux();
         

               frame.add(drapeaux);
         

               JPanel buttonsPanel= new JPanel();
         

               frame.add(buttonsPanel , BorderLayout.SOUTH);
         


               JButton button1= new JButton("Afficher la Palestine");
         

               button1.addActionListener(drapeaux);
         

               buttonsPanel.add(button1);
         

               JButton button2= new JButton("Afficher l'Arabie Saoudite");
         

               button2.addActionListener(drapeaux);
         

               buttonsPanel.add(button2);

               JButton button3= new JButton("Jouer la chanson");
         

               button3.addActionListener(drapeaux);
         

               buttonsPanel.add(button3);
         

               JButton button4= new JButton("Afficher l'homme");
         

               button4.addActionListener(drapeaux);
         

               buttonsPanel.add(button4);



               JButton button5= new JButton("Afficher le Liban");
         

               button5.addActionListener(drapeaux);
         

               buttonsPanel.add(button5);



               frame.setVisible(true);

           
    }

}

الذي فوق هو الكود

والذي في الصورة هو ال error

IMG-20230525-WA0134.jpeg

تم التعديل في بواسطة Mustafa Suleiman
تعديل عنوان السؤال ونص السؤال
رابط هذا التعليق
شارك على الشبكات الإجتماعية

Recommended Posts

  • 0

يوجد استثناء FileNotFoundException في رسالة الخطأ وهذا يعني أن الملف الذي تحاولي قرائته للتشغيل الصوتي غير موجود في المسار المحدد، ويجب التأكد من أن الملف الصوتي المطلوب موجود في المسار الصحيح.

أي تحققي من المسار الذي تم تحديده للملف في الكود التالي، والأفضل تسمية الملف باسم بسيط باللغة الإنجليزية.

File file = new File(System.getProperty("user.home") + File.separator + "Desktop" + File.separator + "أغنيه الأرض لنا والقدس لنا بدون موسيقى وبدون ايقاع (256 kbps) (shabakngy.com).mp3");

وإذا كان الملف غير موجود في المسار الصحيح، عليك بتحديد المسار الصحيح للملف الصوتي أو بنقل الملف إلى المسار الحالي المحدد في الكود.

رابط هذا التعليق
شارك على الشبكات الإجتماعية

  • 0
بتاريخ 6 دقائق مضت قال Mustafa Suleiman:

يوجد استثناء FileNotFoundException في رسالة الخطأ وهذا يعني أن الملف الذي تحاولي قرائته للتشغيل الصوتي غير موجود في المسار المحدد، ويجب التأكد من أن الملف الصوتي المطلوب موجود في المسار الصحيح.

أي تحققي من المسار الذي تم تحديده للملف في الكود التالي، والأفضل تسمية الملف باسم بسيط باللغة الإنجليزية.

File file = new File(System.getProperty("user.home") + File.separator + "Desktop" + File.separator + "أغنيه الأرض لنا والقدس لنا بدون موسيقى وبدون ايقاع (256 kbps) (shabakngy.com).mp3");

وإذا كان الملف غير موجود في المسار الصحيح، عليك بتحديد المسار الصحيح للملف الصوتي أو بنقل الملف إلى المسار الحالي المحدد في الكود.

طيب صغرت الإسم وهو موجود على ال deaktop وكتبت هكذا بالكود، ومازال ال error. 

وفي الصورة موجود ان الأغنية على ال desktop 

16850477135233588245141039863005.jpg

رابط هذا التعليق
شارك على الشبكات الإجتماعية

  • 0

عليك بكتابة الصيغة الخاصة بالملف وهي .mp3 وسأشرح لك الكود:

  • System.getProperty("user.home"): يستخدم للحصول على مسار مجلد المستخدم الرئيسي. هذا الجزء يعيد سلسلة النص التي تمثل مسار المجلد الرئيسي للمستخدم.
  • File.separator: هو فاصل الدليل الذي يستخدم لتجنب مشاكل التوافق بين أنظمة التشغيل المختلفة. يتم استخدامه هنا لتحديد فاصل الدليل في المسار.
  • "Desktop": يشير إلى اسم المجلد المستهدف، وهو مجلد سطح المكتب.
  • "al kouds.mp3": اسم الملف الصوتي الذي يتم تحديده في المسار.

وما يحدث هو دمج جميع هذه الأجزاء لإنشاء المسار الكامل إلى الملف الصوتي، والمسار النهائي يعتمد على مسار المجلد الرئيسي للمستخدم واسم المجلد المستهدف واسم الملف الصوتي.

رابط هذا التعليق
شارك على الشبكات الإجتماعية

  • 0

java.io.FileNotFoundException: C:\Users\User\Desktop\al kouds.mp3 (The system cannot find the file specified)
    at java.io.FileInputStream.open0(Native Method)
    at java.io.FileInputStream.open(FileInputStream.java:195)
    at java.io.FileInputStream.<init>(FileInputStream.java:138)
    at com.sun.media.sound.WaveFloatFileReader.getAudioInputStream(WaveFloatFileReader.java:168)
    at javax.sound.sampled.AudioSystem.getAudioInputStream(AudioSystem.java:1181)
    at Drapeaux.<init>(Drapeaux.java:26)
    at Drapeaux.main(Drapeaux.java:129)
هذا هو ال error

 

Drapeaux.java

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.sound.sampled.*;
import javax.swing.*;

public class Drapeaux extends JPanel implements ActionListener {
    private int flag = 0; // Indicateur pour savoir quel drapeau doit être affiché (0 = Liban, 1 = Palestine, 2 = Arabie Saoudite)
    private Clip clip; // Objet pour jouer la chanson
    private boolean showMan = false; // Indicateur pour savoir si l'homme doit être affiché

    public Drapeaux() {
        try {
            // Charger le fichier audio
            File file = new File(System.getProperty("user.home") + File.separator + "Desktop" + File.separator + "al kouds.mp3");
            AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(file);
            clip = AudioSystem.getClip();
            clip.open(audioInputStream);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        int width = getWidth();
        int height = getHeight();

        if (flag == 1) {
            // Dessiner le drapeau de la Palestine
            g.setColor(Color.BLACK);
            g.fillRect(0, 0, width, height / 3);
            g.setColor(Color.WHITE);
            g.fillRect(0, height / 3, width, height / 3);
            g.setColor(Color.GREEN);
            g.fillRect(0, height * 2 / 3, width, height / 3);
            g.setColor(Color.RED);
            int[] xPoints = {0, width / 4, 0};
            int[] yPoints = {0, height / 2, height};
            g.fillPolygon(xPoints, yPoints, 3);
        } else if (flag == 2) {
            // Dessiner le drapeau de l'Arabie Saoudite
            g.setColor(new Color(0x006C35));
            g.fillRect(0, 0, width, height);

            // Dessiner l'épée
            g.setColor(Color.WHITE);
            int swordY = height * 2 / 3;
            int swordWidth = width * 2 / 3;
            int swordHeight = height / 40;
            int swordX = (width - swordWidth) / 2;
            g.fillRect(swordX, swordY, swordWidth, swordHeight);

            // Dessiner l'inscription
            Font font = new Font("Thuluth", Font.PLAIN, height / 4);
            g.setFont(font);
            FontMetrics metrics = g.getFontMetrics(font);
            String text = "لا إله إلا الله محمد رسول الله";
            int textX = (width - metrics.stringWidth(text)) / 2;
            int textY = swordY - metrics.getHeight() + metrics.getAscent();
            g.drawString(text, textX, textY);
        } else {
           // Dessiner le drapeau du Liban
           g.setColor(Color.RED);
           g.fillRect(0 ,0 ,width ,height); 
           g.setColor(Color.WHITE); 
           g.fillRect(0 ,height/3 ,width ,height/3); 
           g.setColor(Color.GREEN); 
           int treeHeight=height/3; 
           int treeWidth=treeHeight/2; 
           int treeX=(width-treeWidth)/2; 
           int treeY=(height-treeHeight)/2; 
           g.fillOval(treeX ,treeY ,treeWidth ,treeHeight); 
        }

        if (showMan) {
          // Dessiner le corps
          g.setColor(Color.BLACK);
          g.drawLine(width / 2 ,height/4 ,width/2 ,height*3/4);

          // Dessiner les jambes
          g.drawLine(width/2 ,height*3/4 ,width*3/8 ,height); 
          g.drawLine(width/2 ,height*3/4 ,width*5/8 ,height); 

          // Dessiner les bras
          g.drawLine(width/2 ,height/2 ,width*3/8 ,height*1/4); 
          g.drawLine(width/2 ,height/2 ,width*5/8 ,height*1/4); 

          // Dessiner la tête
          g.drawOval(width*7/16,height/8,width/8,height/8); 
        }
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals("Afficher la Palestine")) {
           flag =(flag ==1 ) ? flag :1 ; 
           repaint(); 
        } else if (e.getActionCommand().equals("Afficher l'Arabie Saoudite")) {
           flag =(flag ==2 ) ? flag :2 ; 
           repaint(); 
        } else if (e.getActionCommand().equals("Jouer la chanson")) {
           if(clip != null){
               clip.setFramePosition(0); 
               clip.start(); 
           }
       } else if(e.getActionCommand().equals("Afficher l'homme")){
           showMan=!showMan; 
       } else if(e.getActionCommand().equals("Afficher le Liban")){
           flag=0; 
       }
       repaint(); 
   }

   public static void main(String[] args){
       JFrame frame= new JFrame("Drapeaux"); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.setSize(400 ,300); 

       Drapeaux drapeaux= new Drapeaux(); 
       frame.add(drapeaux); 

       JPanel buttonsPanel= new JPanel(); 
       frame.add(buttonsPanel , BorderLayout.SOUTH); 

       JButton button1= new JButton("Afficher la Palestine"); 
       button1.addActionListener(drapeaux); 
       buttonsPanel.add(button1); 

       JButton button2= new JButton("Afficher l'Arabie Saoudite"); 
       button2.addActionListener(drapeaux); 
       buttonsPanel.add(button2); 

       JButton button3= new JButton("Jouer la chanson"); 
       button3.addActionListener(drapeaux); 
       buttonsPanel.add(button3); 

       JButton button4= new JButton("Afficher l'homme"); 
       button4.addActionListener(drapeaux); 
       buttonsPanel.add(button4);

       JButton button5= new JButton("Afficher le Liban"); 
       button5.addActionListener(drapeaux); 
       buttonsPanel.add(button5);

       frame.setVisible(true); 

   }
}
بعد التعديل

 

رابط هذا التعليق
شارك على الشبكات الإجتماعية

  • 0

المشكلة ما زالت أنه لا يتم العثور على الملف الصوتي في المسار :

 C:\Users\User\Desktop\al kouds.mp3 

لذلك عليك بالتأكد من كتابة الاسم الملف بشكل صحيح والتأكد من المسافات في اسم الملف.

وهل اسم المستخدم لديك هو User؟ فلاحظي المسار حيث أنه يبحث داخل المستخدم User، أرجو منك التوجه إلى مجلد Users في قرص الـ C وتأكدي من وجود اسم مستخدم باسم User فأنا مثلاً لدي مستخدم باسم dido4 وبداخله مجلد Desktop.

Screenshot2023-05-26202509.png.0a23c460f31769e974b831cac999e861.png

ففي حالة لم يكن هناك مجلد باسم Userحاولي كتابة المسار بشكل مباشر للملف بالشكل التالي، وتأكدي من المسافة في اسم الملف والأفضل تجنب وضع أي مسافات في اسم الملف أنصحك بتغيير اسمه.

File file = new File("C:\\Users\\اسم_المستخدم\\Desktop\\al kouds.mp3");

 

رابط هذا التعليق
شارك على الشبكات الإجتماعية

انضم إلى النقاش

يمكنك أن تنشر الآن وتسجل لاحقًا. إذا كان لديك حساب، فسجل الدخول الآن لتنشر باسم حسابك.

زائر
أجب على هذا السؤال...

×   لقد أضفت محتوى بخط أو تنسيق مختلف.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   جرى استعادة المحتوى السابق..   امسح المحرر

×   You cannot paste images directly. Upload or insert images from URL.

  • إعلانات

  • تابعنا على



×
×
  • أضف...