/support/phonegap-fork/android-1.5/src/plt/playlist/PlaylistPlayer.java

https://github.com/jsjohnst/moby-scheme · Java · 128 lines · 80 code · 28 blank · 20 comment · 7 complexity · 7f0b00ccd9862561919eda82dbe8ec88 MD5 · raw file

  1. package plt.playlist;
  2. import android.media.MediaPlayer;
  3. import android.media.AudioManager;
  4. import android.media.MediaPlayer.OnPreparedListener;
  5. import android.media.MediaPlayer.OnCompletionListener;
  6. import android.net.Uri;
  7. import android.os.Handler;
  8. import android.os.SystemClock;
  9. import android.app.Activity;
  10. import java.io.IOException;
  11. import java.util.List;
  12. import android.util.Log;
  13. // Given a playlist, plays music.
  14. // Needs a handler to queue its stuff. MediaPlayer is supposed to be
  15. // finicky about running in the UI thread, so all of our operations
  16. // must run from the handler.
  17. // FIXME: currently restricted to play the first song in the playlist over
  18. // and over. We need to fix this!
  19. public class PlaylistPlayer {
  20. private MediaPlayer mediaPlayer;
  21. private Handler handler;
  22. private Activity activity;
  23. private int currentSongIndex;
  24. private List<Uri> songs;
  25. private boolean isPlaying;
  26. public PlaylistPlayer(final Activity activity,
  27. Handler handler,
  28. final PlaylistRecord record) {
  29. Log.d("PlaylistPlayer", "Constructing player");
  30. final PlaylistPlayer that = this;
  31. this.activity = activity;
  32. this.handler = handler;
  33. this.handler.post(new Runnable() { public void run() {
  34. that.songs = record.getSongUris(activity);
  35. that.currentSongIndex = 0;
  36. that.isPlaying = false;
  37. }});
  38. }
  39. // The following methods will queue up a sequence of songs to play.
  40. public void play() {
  41. final PlaylistPlayer that = this;
  42. this.handler.post(new Runnable() {
  43. public void run() {
  44. that.isPlaying = true;
  45. try {
  46. if (that.mediaPlayer == null) {
  47. that.mediaPlayer = new MediaPlayer();
  48. that.mediaPlayer.setLooping(true);
  49. // that.mediaPlayer.setOnCompletionListener
  50. // (new OnCompletionListener() {
  51. // public void onCompletion(final MediaPlayer mp) {
  52. // mp.release();
  53. // that.mediaPlayer = null;
  54. // that.currentSongIndex =
  55. // (that.currentSongIndex + 1) %
  56. // that.songs.size();
  57. // if (that.isPlaying) {
  58. // that.play();
  59. // }
  60. // }
  61. // });
  62. that.mediaPlayer.setDataSource
  63. (that.activity,
  64. that.songs.get(that.currentSongIndex));
  65. that.mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
  66. that.mediaPlayer.prepare();
  67. that.mediaPlayer.start();
  68. } else {
  69. that.mediaPlayer.start();
  70. }
  71. } catch (IOException e) {
  72. e.printStackTrace();
  73. }
  74. }
  75. });
  76. }
  77. public void pause() {
  78. final PlaylistPlayer that = this;
  79. this.handler.post(new Runnable() {
  80. public void run() {
  81. that.isPlaying = false;
  82. if (that.mediaPlayer != null) {
  83. that.mediaPlayer.pause();
  84. }
  85. }
  86. });
  87. }
  88. public void stop() {
  89. final PlaylistPlayer that = this;
  90. this.handler.post(new Runnable() {
  91. public void run() {
  92. that.isPlaying = false;
  93. if (that.mediaPlayer != null) {
  94. that.mediaPlayer.release();
  95. that.mediaPlayer = null;
  96. }
  97. }
  98. });
  99. }
  100. }