00001 package i2bpro.player;
00002
00003 import i2bpro.Exceptions.ErrorMsg;
00004 import i2bpro.Exceptions.LayerException;
00005 import i2bpro.Exceptions.PlayerException;
00006 import i2bpro.layer.Layer;
00007 import i2bpro.playlist.PlayList;
00008 import java.io.File;
00009 import java.io.IOException;
00010 import java.net.MalformedURLException;
00011 import java.net.URL;
00012 import java.util.regex.Matcher;
00013 import java.util.regex.Pattern;
00014 import javax.media.AudioDeviceUnavailableEvent;
00015 import javax.media.ConnectionErrorEvent;
00016 import javax.media.ControllerErrorEvent;
00017 import javax.media.ControllerEvent;
00018 import javax.media.ControllerListener;
00019 import javax.media.DataLostErrorEvent;
00020 import javax.media.DataStarvedEvent;
00021 import javax.media.EndOfMediaEvent;
00022 import javax.media.InternalErrorEvent;
00023 import javax.media.Manager;
00024 import javax.media.NoPlayerException;
00025 import javax.media.PrefetchCompleteEvent;
00026 import javax.media.RealizeCompleteEvent;
00027 import javax.media.StartEvent;
00028 import javax.media.Time;
00029
00030
00031
00057 public final class Player implements ControllerListener
00058 {
00059
00060 private int state = 0;
00061
00062
00063 private int volume = 70;
00064
00065
00066 private boolean mute = false;
00067
00068
00069 private javax.media.Player player;
00070
00071
00072 private URL media = null;
00073
00074
00075 private int old_id = -1;
00076
00077
00078 private Time position = null;
00079
00080
00081 private static Player instance = new Player();
00082
00083
00084
00092 private Player() {}
00093
00094
00095
00105 public static Player getInstance() { return instance; }
00106
00107
00108
00123 public void play(int id) throws MalformedURLException, IOException, NoPlayerException
00124 {
00125 media = LoadMedia(id);
00126 StartPlay(id);
00127 }
00128
00129
00130
00142 private void StartPlay(int id) throws IOException, NoPlayerException
00143 {
00144
00145
00146 if (old_id == id)
00147 {
00148
00149
00150
00151 switch(state)
00152 {
00153 case 0 : PlayerInit(); player.realize(); break;
00154 case 1 : player.setMediaTime(new Time(0)); break;
00155 case 2 : player.setMediaTime(position);
00156 player.start(); state = 1; break;
00157 }
00158 } else {
00159
00160
00161
00162
00163
00164
00165
00166
00167 switch(state)
00168 {
00169 case 1 : stop(); player.close();
00170 case 2 : player.close();
00171 }
00172 PlayerInit();
00173 player.realize();
00174 }
00175 old_id = id;
00176 }
00177
00178
00179
00187 private void PlayerInit() throws IOException, NoPlayerException
00188 {
00189 player = Manager.createPlayer(media);
00190 player.addControllerListener(this);
00191 }
00192
00193
00194
00204 private URL LoadMedia(int id) throws MalformedURLException
00205 {
00206 return new File(PlayList.getInstance().getMeta(id, "media")).toURI().toURL();
00207 }
00208
00209
00210
00218 public void stop()
00219 {
00220 player.stop();
00221 player.close();
00222 state = 0;
00223 Layer.getInstance().PlayerStopped();
00224 }
00225
00226
00227
00237 public void pause()
00238 {
00239
00240
00241 if (state == 1)
00242 {
00243
00244 position = player.getMediaTime();
00245
00246
00247 player.stop();
00248
00249
00250 state = 2;
00251
00252
00253 Layer.getInstance().PlayerPaused();
00254 }
00255 }
00256
00257
00258
00267 public TimeInterface getPosition()
00268 {
00269 TimeInterface pos = new TimeInterface()
00270 {
00279 public int getSeconds()
00280 {
00281 int ans = 0;
00282 switch (state)
00283 {
00284 case 0: ans = 0; break;
00285 case 1: ans = (int) player.getMediaTime().getSeconds(); break;
00286 case 2: ans = (int)position.getSeconds(); break;
00287 }
00288 return ans;
00289 }
00290
00299 public String getString()
00300 {
00301 Time ans = new Time(0);
00302 switch (state)
00303 {
00304 case 1: ans = player.getMediaTime(); break;
00305 case 2: ans = position; break;
00306 }
00307 return Time2String(ans);
00308 }
00309 };
00310 return pos;
00311 }
00312
00313
00314
00327 public void setPosition(int position)
00328 {
00329
00330
00331
00332
00333 if (state == 1)
00334 player.setMediaTime(new Time((long)(position * 1.0E+9)));
00335 this.position = new Time((long)(position * 1.0E+9));
00336 }
00337
00338
00339
00350 public void setVolume(int vol)
00351 {
00352
00353
00354
00355 vol = (vol < 0) ? 0 : vol;
00356 vol = (vol > 100) ? 100 : vol;
00357 volume = vol;
00358
00359 if (state != 0)
00360 {
00361
00362
00363 float value = (float)0.0;
00364 if (volume > 0)
00365 value = (float) (Math.exp(((float)(volume)/40.0)-2.5)-0.08);
00366 player.getGainControl().setLevel(value);
00367 }
00368 if ((volume > 0) && mute)
00369 {
00370 mute = false;
00371 Layer.getInstance().toggledMute();
00372 }
00373 if ((volume == 0) && (mute == false))
00374 {
00375 mute = true;
00376 Layer.getInstance().toggledMute();
00377 }
00378 }
00379
00380
00381
00394 public int getVolume() { return volume; }
00395
00396
00397
00406 public void toggleMute()
00407 {
00408 if (mute)
00409 {
00410 Layer.getInstance().setVolume(volume);
00411 } else {
00412 int vol = volume;
00413 Layer.getInstance().setVolume(0);
00414 volume = vol;
00415 }
00416 }
00417
00418
00419
00428 public boolean isMute() { return mute; }
00429
00430
00431
00437 public synchronized void controllerUpdate(ControllerEvent event)
00438 {
00439 if (event instanceof RealizeCompleteEvent)
00440 {
00441
00442
00443 player.prefetch();
00444 }
00445 else if (event instanceof PrefetchCompleteEvent)
00446 {
00447
00448
00449 player.start();
00450 }
00451 else if (event instanceof StartEvent)
00452 {
00453
00454
00455 if (state == 0)
00456 {
00457 String[] index = PlayList.getInstance().getMeta(
00458 old_id, "index").split(":");
00459 int seconds = Integer.parseInt(index[0],10) * 60;
00460 seconds += Integer.parseInt(index[1], 10);
00461 setPosition(seconds);
00462 }
00463
00464
00465
00466 if (state < 1) setVolume(volume);
00467 state = 1;
00468 Layer.getInstance().PlayerStarted();
00469 }
00470 else if (event instanceof EndOfMediaEvent)
00471 {
00472 try
00473 {
00474
00475
00476
00477 player.close();
00478 state = 0;
00479 Layer.getInstance().PlayerStopped();
00480 Layer.getInstance().PlayerEndOfMedia();
00481 }
00482 catch (LayerException ex) { ErrorMsg.show(ex.getMessage()); }
00483 catch (MalformedURLException ex) { ErrorMsg.show(ex.getMessage()); }
00484 catch (IOException ex) { ErrorMsg.show(ex.getMessage()); }
00485 catch (NoPlayerException ex) { ErrorMsg.show(ex.getMessage()); }
00486 }
00487
00488 else if (event instanceof AudioDeviceUnavailableEvent)
00489 {
00490 try { throw new PlayerException(300); }
00491 catch (PlayerException ex) { ErrorMsg.show(ex.getMessage()); }
00492 }
00493 else if (event instanceof ConnectionErrorEvent)
00494 {
00495 try { throw new PlayerException(301); }
00496 catch (PlayerException ex) { ErrorMsg.show(ex.getMessage()); }
00497 }
00498 else if (event instanceof DataLostErrorEvent)
00499 {
00500 try { throw new PlayerException(302); }
00501 catch (PlayerException ex) { ErrorMsg.show(ex.getMessage()); }
00502 }
00503 else if (event instanceof DataStarvedEvent)
00504 {
00505 try { throw new PlayerException(303); }
00506 catch (PlayerException ex) { ErrorMsg.show(ex.getMessage()); }
00507 }
00508 else if (event instanceof InternalErrorEvent)
00509 {
00510 try { throw new PlayerException(304); }
00511 catch (PlayerException ex) { ErrorMsg.show(ex.getMessage()); }
00512 }
00513 else if (event instanceof InternalErrorEvent)
00514 {
00515 try { throw new PlayerException(305); }
00516 catch (PlayerException ex) { ErrorMsg.show(ex.getMessage()); }
00517 }
00518 else if (event instanceof ControllerErrorEvent)
00519 {
00520 Pattern pattern = Pattern.compile("message=");
00521 Matcher matcher = pattern.matcher(event.toString());
00522 String err = event.toString();
00523 if (matcher.find())
00524 err = err.substring(matcher.end(), err.length()-1);
00525 else
00526 err = "";
00527 ErrorMsg.show("Es ist folgender Fehler aufgetreten:\n\n"+err);
00528 }
00529 }
00530
00531
00532
00541 public TimeInterface getDuration()
00542 {
00543 TimeInterface duration = new TimeInterface()
00544 {
00553 public int getSeconds()
00554 {
00555 return (int)(player.getDuration().getSeconds());
00556 }
00557
00566 public String getString()
00567 {
00568 return Time2String(player.getDuration());
00569 }
00570 };
00571 return duration;
00572 }
00573
00574
00575
00586 private String Time2String(Time time)
00587 {
00588 String sTime = "";
00589 int seconds = (int)time.getSeconds();
00590 int tmp = 0;
00591 tmp = (int)(seconds / 3600);
00592 if (tmp < 10) sTime += "0";
00593 sTime += tmp + ":";
00594 seconds -= tmp * 3600;
00595 tmp = (int)(seconds / 60);
00596 if (tmp < 10) sTime += "0";
00597 sTime += tmp + ":";
00598 seconds -= tmp * 60;
00599 if (seconds < 10) sTime += "0";
00600 sTime += seconds + "";
00601 return sTime;
00602 };
00603
00604
00605
00616 public int getState() { return state; }
00617 }