Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: irate/client/ExternalPlayer.java


1   // Copyright 2003 Anthony Jones
2   
3   package irate.client;
4   
5   import java.io.File;
6   import java.io.FileNotFoundException;
7   import java.io.IOException;
8   
9   public class ExternalPlayer implements Player {
10  
11    private String name;
12    private String path;
13    private final int ACTION_PAUSE = 0;
14    private final int ACTION_PLAY = 1;
15    private final int ACTION_CLOSE = 2;
16    private int action;
17    private boolean paused;
18    private Process process;
19    private long playTime;
20    
21    public ExternalPlayer(String name, String path) throws FileNotFoundException {
22      this(name, new String[] { path });
23    }
24  
25    public ExternalPlayer(String name, String[] paths) throws FileNotFoundException {
26      this.name = name;
27      for (int i = 0; i < paths.length; i++) {
28        if (new File(paths[i]).exists()) {
29          this.path = paths[i];
30          return;
31        }
32      }
33      StringBuffer msg = new StringBuffer();
34      for (int i = 0; i < paths.length; i++) {
35        if (i != 0)
36          msg.append(" ");
37        msg.append(paths[i]);
38      }
39      throw new FileNotFoundException(msg.toString());
40    }
41  
42    public String getName() {
43      return name;
44    }
45  
46    public void setPaused(boolean paused) {
47      this.paused = paused;
48      if (paused) {
49        action = ACTION_PAUSE;
50        process.destroy();
51      }
52    }
53  
54    public boolean isPaused() {
55      return paused;
56    }
57  
58    public String[] formatResumeArgument()
59    {
60      return null;
61    }
62  
63    /**
64     * Get the number of milliseconds of play time so far for the song
65     * currently being played.
66     */
67    protected long getPlayTime()
68    {
69      return playTime;
70    }
71  
72    public void play(File file) throws PlayerException {
73      playTime = 0;
74      do {
75        try {
76          action = ACTION_PLAY;
77    String[] resumeArg = formatResumeArgument();
78    String[] args;
79    if (resumeArg == null)
80      args = new String[2];
81    else {
82      args = new String[resumeArg.length+2];
83      System.arraycopy(resumeArg, 0, args, 1, resumeArg.length);
84          }
85    args[0] = path;
86    args[args.length-1] = file.getPath();
87          process = Runtime.getRuntime().exec(args);
88        }
89        catch (IOException e) {
90          e.printStackTrace();
91          throw new PlayerException(e.toString());
92        }
93        long start = System.currentTimeMillis();
94        try {
95          process.waitFor();
96          if (!paused && process.exitValue() != 0) 
97            throw new PlayerException("extern player returned " + process.exitValue());
98        }
99        catch (InterruptedException e) {
100         e.printStackTrace();
101       }
102       if (paused) {
103   long timePlayed = System.currentTimeMillis() - start;
104   playTime += timePlayed;
105       }
106       while (paused) { 
107         try {
108           Thread.sleep(100);
109         }
110         catch (InterruptedException ie) {
111           ie.printStackTrace();
112         }
113       }
114       if (action == ACTION_CLOSE) 
115         throw new PlayerException("extern player closed");
116     } while (action != ACTION_PLAY);
117   }
118 
119   public void close() {
120     if (process != null) {
121       action = ACTION_CLOSE;
122       process.destroy();
123     }
124   }
125 }