MediaServiceBinder.java 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /**
  2. * ownCloud Android client application
  3. *
  4. * @author David A. Velasco
  5. * Copyright (C) 2016 ownCloud Inc.
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2,
  9. * as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.owncloud.android.media;
  20. import android.accounts.Account;
  21. import android.content.Intent;
  22. import android.media.MediaPlayer;
  23. import android.os.Binder;
  24. import android.widget.MediaController;
  25. import com.owncloud.android.datamodel.OCFile;
  26. import com.owncloud.android.lib.common.utils.Log_OC;
  27. import com.owncloud.android.media.MediaService.State;
  28. /**
  29. * Binder allowing client components to perform operations on on the MediaPlayer managed by a MediaService instance.
  30. *
  31. * Provides the operations of {@link MediaController.MediaPlayerControl}, and an extra method to check if
  32. * an {@link OCFile} instance is handled by the MediaService.
  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;
  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. return currentPlayer.getCurrentPosition();
  81. } else {
  82. return 0;
  83. }
  84. }
  85. @Override
  86. public int getDuration() {
  87. MediaPlayer currentPlayer = mService.getPlayer();
  88. if (currentPlayer != null) {
  89. return currentPlayer.getDuration();
  90. } else {
  91. return 0;
  92. }
  93. }
  94. /**
  95. * Reports if the MediaService is playing a file or not.
  96. *
  97. * Considers that the file is being played when it is in preparation because the expected
  98. * client of this method is a {@link MediaController} , and we do not want that the 'play'
  99. * button is shown when the file is being prepared by the MediaService.
  100. */
  101. @Override
  102. public boolean isPlaying() {
  103. MediaService.State currentState = mService.getState();
  104. return currentState == State.PLAYING || (currentState == State.PREPARING && mService.mPlayOnPrepared);
  105. }
  106. @Override
  107. public void pause() {
  108. Log_OC.d(TAG, "Pausing through binder...");
  109. mService.processPauseRequest();
  110. }
  111. @Override
  112. public void seekTo(int pos) {
  113. Log_OC.d(TAG, "Seeking " + pos + " through binder...");
  114. MediaPlayer currentPlayer = mService.getPlayer();
  115. MediaService.State currentState = mService.getState();
  116. if (currentPlayer != null && currentState != State.PREPARING && currentState != State.STOPPED) {
  117. currentPlayer.seekTo(pos);
  118. }
  119. }
  120. @Override
  121. public void start() {
  122. Log_OC.d(TAG, "Starting through binder...");
  123. mService.processPlayRequest(); // this will finish the service if there is no file preloaded to play
  124. }
  125. public void start(Account account, OCFile file, boolean playImmediately, int position) {
  126. Log_OC.d(TAG, "Loading and starting through binder...");
  127. Intent i = new Intent(mService, MediaService.class);
  128. i.putExtra(MediaService.EXTRA_ACCOUNT, account);
  129. i.putExtra(MediaService.EXTRA_FILE, file);
  130. i.putExtra(MediaService.EXTRA_PLAY_ON_LOAD, playImmediately);
  131. i.putExtra(MediaService.EXTRA_START_POSITION, position);
  132. i.setAction(MediaService.ACTION_PLAY_FILE);
  133. mService.startService(i);
  134. }
  135. public void registerMediaController(MediaControlView mediaController) {
  136. mService.setMediaContoller(mediaController);
  137. }
  138. public void unregisterMediaController(MediaControlView mediaController) {
  139. if (mediaController != null && mediaController == mService.getMediaController()) {
  140. mService.setMediaContoller(null);
  141. }
  142. }
  143. public boolean isInPlaybackState() {
  144. MediaService.State currentState = mService.getState();
  145. return currentState == MediaService.State.PLAYING || currentState == MediaService.State.PAUSED;
  146. }
  147. @Override
  148. public int getAudioSessionId() {
  149. return 1; // not really used
  150. }
  151. }