MediaServiceBinder.java 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /* ownCloud Android client application
  2. * Copyright (C) 2012-2013 ownCloud Inc.
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2,
  6. * as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. *
  16. */
  17. package com.owncloud.android.media;
  18. import com.owncloud.android.Log_OC;
  19. import com.owncloud.android.datamodel.OCFile;
  20. import com.owncloud.android.media.MediaService.State;
  21. import android.accounts.Account;
  22. import android.content.Intent;
  23. import android.media.MediaPlayer;
  24. import android.os.Binder;
  25. import android.widget.MediaController;
  26. /**
  27. * Binder allowing client components to perform operations on on the MediaPlayer managed by a MediaService instance.
  28. *
  29. * Provides the operations of {@link MediaController.MediaPlayerControl}, and an extra method to check if
  30. * an {@link OCFile} instance is handled by the MediaService.
  31. *
  32. * @author David A. Velasco
  33. */
  34. public class MediaServiceBinder extends Binder implements MediaController.MediaPlayerControl {
  35. private static final String TAG = MediaServiceBinder.class.getSimpleName();
  36. /**
  37. * {@link MediaService} instance to access with the binder
  38. */
  39. private MediaService mService = null;
  40. /**
  41. * Public constructor
  42. *
  43. * @param service A {@link MediaService} instance to access with the binder
  44. */
  45. public MediaServiceBinder(MediaService service) {
  46. if (service == null) {
  47. throw new IllegalArgumentException("Argument 'service' can not be null");
  48. }
  49. mService = service;
  50. }
  51. public boolean isPlaying(OCFile mFile) {
  52. return (mFile != null && mFile.equals(mService.getCurrentFile()));
  53. }
  54. @Override
  55. public boolean canPause() {
  56. return true;
  57. }
  58. @Override
  59. public boolean canSeekBackward() {
  60. return true;
  61. }
  62. @Override
  63. public boolean canSeekForward() {
  64. return true;
  65. }
  66. @Override
  67. public int getBufferPercentage() {
  68. MediaPlayer currentPlayer = mService.getPlayer();
  69. if (currentPlayer != null) {
  70. return 100;
  71. // TODO update for streamed playback; add OnBufferUpdateListener in MediaService
  72. } else {
  73. return 0;
  74. }
  75. }
  76. @Override
  77. public int getCurrentPosition() {
  78. MediaPlayer currentPlayer = mService.getPlayer();
  79. if (currentPlayer != null) {
  80. int pos = currentPlayer.getCurrentPosition();
  81. return pos;
  82. } else {
  83. return 0;
  84. }
  85. }
  86. @Override
  87. public int getDuration() {
  88. MediaPlayer currentPlayer = mService.getPlayer();
  89. if (currentPlayer != null) {
  90. int dur = currentPlayer.getDuration();
  91. return dur;
  92. } else {
  93. return 0;
  94. }
  95. }
  96. /**
  97. * Reports if the MediaService is playing a file or not.
  98. *
  99. * Considers that the file is being played when it is in preparation because the expected
  100. * client of this method is a {@link MediaController} , and we do not want that the 'play'
  101. * button is shown when the file is being prepared by the MediaService.
  102. */
  103. @Override
  104. public boolean isPlaying() {
  105. MediaService.State currentState = mService.getState();
  106. return (currentState == State.PLAYING || (currentState == State.PREPARING && mService.mPlayOnPrepared));
  107. }
  108. @Override
  109. public void pause() {
  110. Log_OC.d(TAG, "Pausing through binder...");
  111. mService.processPauseRequest();
  112. }
  113. @Override
  114. public void seekTo(int pos) {
  115. Log_OC.d(TAG, "Seeking " + pos + " through binder...");
  116. MediaPlayer currentPlayer = mService.getPlayer();
  117. MediaService.State currentState = mService.getState();
  118. if (currentPlayer != null && currentState != State.PREPARING && currentState != State.STOPPED) {
  119. currentPlayer.seekTo(pos);
  120. }
  121. }
  122. @Override
  123. public void start() {
  124. Log_OC.d(TAG, "Starting through binder...");
  125. mService.processPlayRequest(); // this will finish the service if there is no file preloaded to play
  126. }
  127. public void start(Account account, OCFile file, boolean playImmediately, int position) {
  128. Log_OC.d(TAG, "Loading and starting through binder...");
  129. Intent i = new Intent(mService, MediaService.class);
  130. i.putExtra(MediaService.EXTRA_ACCOUNT, account);
  131. i.putExtra(MediaService.EXTRA_FILE, file);
  132. i.putExtra(MediaService.EXTRA_PLAY_ON_LOAD, playImmediately);
  133. i.putExtra(MediaService.EXTRA_START_POSITION, position);
  134. i.setAction(MediaService.ACTION_PLAY_FILE);
  135. mService.startService(i);
  136. }
  137. public void registerMediaController(MediaControlView mediaController) {
  138. mService.setMediaContoller(mediaController);
  139. }
  140. public void unregisterMediaController(MediaControlView mediaController) {
  141. if (mediaController != null && mediaController == mService.getMediaController()) {
  142. mService.setMediaContoller(null);
  143. }
  144. }
  145. public boolean isInPlaybackState() {
  146. MediaService.State currentState = mService.getState();
  147. return (currentState == MediaService.State.PLAYING || currentState == MediaService.State.PAUSED);
  148. }
  149. }