2011-07-17 43 views
0

我的Java經驗很少,我似乎無法找到任何解釋我如何解決我的問題;幾個小時我一直在嘗試不同的東西。在Java事件中訪問全局對象

我正在使用Phidg​​ets RFID Java庫(http://www.phidgets.com/programming_resources.php)和JLayer,目的是根據傳感器範圍內的哪個RFID標籤播放不同的mp3文件。一旦RFID標籤不在範圍內,就需要停止播放。

的mp3音樂類:

// Import the JLayer classes 
import javazoom.jl.player.*; 

// Import the Java classes 
import java.io.*; 

public class mp3 { 

    private Player player; 
    private InputStream is; 

    /** Creates a new instance of MP3Player */ 
    public mp3() 
    { 
     // 
    } 

    public void play(String filename) 
    { 
     try 
     { 
      // Create an InputStream to the file 
      is = new FileInputStream(filename); 
     } 
     catch(Exception e) 
     { 
      e.printStackTrace(); 
     } 

     try 
     { 
      player = new Player(is); 
      PlayerThread pt = new PlayerThread(); 
      pt.start(); 
     } 
     catch(Exception e) 
     { 
      e.printStackTrace(); 
     } 
    } 

    public void stop() 
    { 
     player.close(); 
    } 

    class PlayerThread extends Thread 
    { 
     public void run() 
     { 
      try 
      { 
       player.play(); 
      } 
      catch(Exception e) 
      { 
       e.printStackTrace(); 
      } 
     } 
    } 
} 

和階級,一切事情發生:

import com.phidgets.*; 
import com.phidgets.event.*; 

public class ParrotDJ 
{   

    public static final void main(String args[]) throws Exception { 

      RFIDPhidget rfid; 

      mp3 song = new mp3(); 

    System.out.println(Phidget.getLibraryVersion()); 

    rfid = new RFIDPhidget(); 
    rfid.addAttachListener(new AttachListener() { 
     public void attached(AttachEvent ae) 
     { 
      try 
      { 
       ((RFIDPhidget)ae.getSource()).setAntennaOn(true); 
       ((RFIDPhidget)ae.getSource()).setLEDOn(true); 
      } 
      catch (PhidgetException ex) { } 
      System.out.println("attachment of " + ae); 
     } 
    }); 
    rfid.addDetachListener(new DetachListener() { 
     public void detached(DetachEvent ae) { 
      System.out.println("detachment of " + ae); 
     } 
    }); 
    rfid.addErrorListener(new ErrorListener() { 
     public void error(ErrorEvent ee) { 
      System.out.println("error event for " + ee); 
     } 
    }); 

    rfid.addTagGainListener(new TagGainListener() 
    { 

     public void tagGained(TagGainEvent oe) 
     { 
      //System.out.println(oe); 
          if(oe.getValue().equals("0107ee6ed5") || oe.getValue().equals("0107ee75d5")) 
          { 
           System.out.println("Amanda Palmer - Leeds United"); 
           song.play("leedsunited.mp3"); 

          }else if(oe.getValue().equals("0107ee82c7") || oe.getValue().equals("0107ee89f0")) 
          { 
           System.out.println("Paolo Nutini - 10/10"); 
           song.play("1010.mp3"); 

          }else if(oe.getValue().equals("0107ee8644") || oe.getValue().equals("0107ee6ff2")) 
          { 
           System.out.println("Mozart - Eine Kleine Nachtmusik"); 
           song.play("einekleinenachtmusik.mp3"); 
          } 
     } 

    }); 
    rfid.addTagLossListener(new TagLossListener() 
    { 
     public void tagLost(TagLossEvent oe) 
     { 
      //System.out.println(oe); 
          System.out.println("Stop"); 
          song.stop(); 

     } 
    }); 
    rfid.addOutputChangeListener(new OutputChangeListener() 
    { 
     public void outputChanged(OutputChangeEvent oe) 
     { 
      System.out.println(oe); 
     } 
    }); 

    rfid.openAny(); 
    System.out.println("waiting for RFID attachment..."); 
    rfid.waitForAttachment(1000); 

    System.out.println("Serial: " + rfid.getSerialNumber()); 
    System.out.println("Outputs: " + rfid.getOutputCount()); 

    System.out.println("Outputting events. Input to stop."); 
    System.in.read(); 
    System.out.print("closing..."); 
    rfid.close(); 
    rfid = null; 
    System.out.println(" ok"); 
    if (false) { 
     System.out.println("wait for finalization..."); 
     System.gc(); 
    } 
} 
} 

我想有一個合理的解決方案,我只是努力讓我的頭圍繞事件驅動型的東西,以及Java的面向對象。我查了一下建設者模式,但是我現在無法掌握如何將這種情況應用於這種情況。

在此先感謝。

+0

請問更具體的問題 – zacheusz

回答

0

我不確定特定問題是否清楚,除非按照總結。但根據我的猜測,看起來您在訪問內部類方法中的類中定義的對象時遇到問題。 主要問題是由於Java不知道字段的狀態,所以字段不能被「最終」訪問。如果田作了最後,你應該能夠訪問事件方法

0

外部類的字段如果你想從whitin匿名類的方法訪問本地變量作爲一個在這裏:

rfid.addTagGainListener(new TagGainListener() { 

      public void tagGained(TagGainEvent oe) { 

其中新的TagGainListener(){}創建一個匿名類。

你需要聲明變量final,只要你不想修改它,你就不會有任何問題。所以修改後的代碼是:

import com.phidgets.*; 
import com.phidgets.event.*; 

public class ParrotDJ { 

    public static final void main(String args[]) throws Exception { 

     RFIDPhidget rfid; 

     // you need to make it final to access to it from anonymous classes as there rfid.addTagGainListener(new TagGainListener() { 
     final mp3 song = new mp3(); 

     System.out.println(Phidget.getLibraryVersion()); 

     rfid = new RFIDPhidget(); 

     rfid.addAttachListener(new AttachListener() { 
      public void attached(AttachEvent ae) { 
       try { 
        ((RFIDPhidget) ae.getSource()).setAntennaOn(true); 
        ((RFIDPhidget) ae.getSource()).setLEDOn(true); 
       } catch (PhidgetException ex) { 
       } 
       System.out.println("attachment of " + ae); 
      } 
     }); 

     rfid.addDetachListener(new DetachListener() { 
      public void detached(DetachEvent ae) { 
       System.out.println("detachment of " + ae); 
      } 
     }); 

     rfid.addErrorListener(new ErrorListener() { 
      public void error(ErrorEvent ee) { 
       System.out.println("error event for " + ee); 
      } 
     }); 

     rfid.addTagGainListener(new TagGainListener() { 

      public void tagGained(TagGainEvent oe) { 
       //System.out.println(oe); 
       if (oe.getValue().equals("0107ee6ed5") || oe.getValue().equals("0107ee75d5")) { 
        System.out.println("Amanda Palmer - Leeds United"); 
        song.play("leedsunited.mp3"); 

       } else if (oe.getValue().equals("0107ee82c7") || oe.getValue().equals("0107ee89f0")) { 
        System.out.println("Paolo Nutini - 10/10"); 
        song.play("1010.mp3"); 

       } else if (oe.getValue().equals("0107ee8644") || oe.getValue().equals("0107ee6ff2")) { 
        System.out.println("Mozart - Eine Kleine Nachtmusik"); 
        song.play("einekleinenachtmusik.mp3"); 
       } 
      } 

     }); 
     rfid.addTagLossListener(new TagLossListener() { 
      public void tagLost(TagLossEvent oe) { 
       //System.out.println(oe); 
       System.out.println("Stop"); 
       song.stop(); 

      } 
     }); 
     rfid.addOutputChangeListener(new OutputChangeListener() { 
      public void outputChanged(OutputChangeEvent oe) { 
       System.out.println(oe); 
      } 
     }); 

     rfid.openAny(); 
     System.out.println("waiting for RFID attachment..."); 
     rfid.waitForAttachment(1000); 

     System.out.println("Serial: " + rfid.getSerialNumber()); 
     System.out.println("Outputs: " + rfid.getOutputCount()); 

     System.out.println("Outputting events. Input to stop."); 
     System.in.read(); 
     System.out.print("closing..."); 
     rfid.close(); 
     rfid = null; 
     System.out.println(" ok"); 
     if (false) { 
      System.out.println("wait for finalization..."); 
      System.gc(); 
     } 
    } 
} 
+0

謝謝jaime,這正是我所需要的,並且完全解決了我的問題:) – Amy