PageRenderTime 75ms CodeModel.GetById 0ms RepoModel.GetById 1ms app.codeStats 0ms

/src/main/java/net/airpost/relan/ctrlaltdlna/localroute/Controller.java

https://gitlab.com/unmaintained/ctrlaltdlna
Java | 201 lines | 134 code | 26 blank | 41 comment | 15 complexity | b3406c1571244a5f9465894e4e76e76e MD5 | raw file
  1. /*
  2. Copyright (c) 2013, Felix Ableitner
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are met:
  6. * Redistributions of source code must retain the above copyright
  7. notice, this list of conditions and the following disclaimer.
  8. * Redistributions in binary form must reproduce the above copyright
  9. notice, this list of conditions and the following disclaimer in the
  10. documentation and/or other materials provided with the distribution.
  11. * Neither the name of the <organization> nor the
  12. names of its contributors may be used to endorse or promote products
  13. derived from this software without specific prior written permission.
  14. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  15. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  16. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  17. DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
  18. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  19. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  20. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  21. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  23. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. package net.airpost.relan.ctrlaltdlna.localroute;
  26. import android.content.Context;
  27. import android.content.Intent;
  28. import android.media.AudioManager;
  29. import android.media.MediaPlayer;
  30. import android.os.Bundle;
  31. import android.os.SystemClock;
  32. import android.support.v7.media.MediaControlIntent;
  33. import android.support.v7.media.MediaItemStatus;
  34. import android.support.v7.media.MediaRouteProvider;
  35. import android.support.v7.media.MediaRouter.ControlRequestCallback;
  36. import android.util.Log;
  37. import java.io.IOException;
  38. /**
  39. * Receives control intents through media route and executes them on a MediaPlayer.
  40. *
  41. * @author felix
  42. *
  43. */
  44. public class Controller extends MediaRouteProvider.RouteController implements
  45. MediaPlayer.OnCompletionListener, MediaPlayer.OnPreparedListener {
  46. private static final String TAG = "Controller";
  47. private Context mContext;
  48. private AudioManager mAudio;
  49. AudioManager.OnAudioFocusChangeListener mFocusListener;
  50. private final String mRouteId;
  51. private String mItemId;
  52. private int mState;
  53. private MediaPlayer mPlayer = new MediaPlayer();
  54. public Controller(String routeId, Context context) {
  55. mContext = context;
  56. mRouteId = routeId;
  57. mAudio = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
  58. mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
  59. mPlayer.setOnPreparedListener(this);
  60. }
  61. @Override
  62. public void onRelease() {
  63. mPlayer.release();
  64. }
  65. @Override
  66. public void onSelect() {
  67. mAudio.requestAudioFocus(mFocusListener, AudioManager.STREAM_MUSIC,
  68. AudioManager.AUDIOFOCUS_GAIN);
  69. }
  70. @Override
  71. public void onUnselect() {
  72. mAudio.abandonAudioFocus(mFocusListener);
  73. }
  74. @Override
  75. public void onSetVolume(int volume) {
  76. mAudio.setStreamVolume(AudioManager.STREAM_MUSIC, volume, 0);
  77. }
  78. @Override
  79. public void onUpdateVolume(int delta) {
  80. int currentVolume = mAudio.getStreamVolume(AudioManager.STREAM_MUSIC);
  81. mAudio.setStreamVolume(AudioManager.STREAM_MUSIC, currentVolume + delta, 0);
  82. }
  83. @Override
  84. public boolean onControlRequest(Intent intent, ControlRequestCallback callback) {
  85. String sessionId = intent.getStringExtra(MediaControlIntent.EXTRA_SESSION_ID);
  86. String itemId = intent.getStringExtra(MediaControlIntent.EXTRA_ITEM_ID);
  87. if (intent.getAction().equals(MediaControlIntent.ACTION_PLAY)) {
  88. try {
  89. mPlayer.reset();
  90. mPlayer.setDataSource(mContext, intent.getData());
  91. mPlayer.prepareAsync();
  92. mItemId = intent.getDataString();
  93. mState = MediaItemStatus.PLAYBACK_STATE_BUFFERING;
  94. getStatus(mItemId, mRouteId, callback);
  95. return true;
  96. }
  97. catch (IllegalArgumentException e) {
  98. mState = MediaItemStatus.PLAYBACK_STATE_ERROR;
  99. Log.d(TAG, "Failed to start playback", e);
  100. }
  101. catch (IOException e) {
  102. mState = MediaItemStatus.PLAYBACK_STATE_ERROR;
  103. Log.d(TAG, "Failed to start playback", e);
  104. }
  105. }
  106. else if (intent.getAction().equals(MediaControlIntent.ACTION_PAUSE)) {
  107. mPlayer.pause();
  108. mState = MediaItemStatus.PLAYBACK_STATE_PAUSED;
  109. return true;
  110. }
  111. else if (intent.getAction().equals(MediaControlIntent.ACTION_RESUME)) {
  112. mPlayer.start();
  113. mState = MediaItemStatus.PLAYBACK_STATE_PLAYING;
  114. return true;
  115. }
  116. else if (intent.getAction().equals(MediaControlIntent.ACTION_STOP)) {
  117. mPlayer.stop();
  118. mState = MediaItemStatus.PLAYBACK_STATE_CANCELED;
  119. return true;
  120. }
  121. else if (intent.getAction().equals(MediaControlIntent.ACTION_SEEK)) {
  122. mPlayer.seekTo((int) intent.getLongExtra(
  123. MediaControlIntent.EXTRA_ITEM_CONTENT_POSITION, 0));
  124. getStatus(itemId, sessionId, callback);
  125. return true;
  126. }
  127. else if(intent.getAction().equals(MediaControlIntent.ACTION_GET_STATUS)) {
  128. getStatus(itemId, sessionId, callback);
  129. return true;
  130. }
  131. return false;
  132. }
  133. private void getStatus(String itemId, String sessionId, ControlRequestCallback callback) {
  134. if (callback == null)
  135. return;
  136. Bundle status = null;
  137. if (mItemId.equals(itemId)) {
  138. status = new MediaItemStatus.Builder(mState)
  139. .setContentPosition(mPlayer.getCurrentPosition())
  140. .setContentDuration(mPlayer.getDuration())
  141. .setTimestamp(SystemClock.elapsedRealtime())
  142. .build()
  143. .asBundle();
  144. status.putString(MediaControlIntent.EXTRA_SESSION_ID, mRouteId);
  145. status.putString(MediaControlIntent.EXTRA_ITEM_ID, mItemId);
  146. }
  147. else {
  148. status = new MediaItemStatus.Builder(MediaItemStatus.PLAYBACK_STATE_INVALIDATED)
  149. .build().asBundle();
  150. }
  151. callback.onResult(status);
  152. }
  153. /**
  154. * Sets state to finished.
  155. *
  156. * Note: Do not set the listener before play() is called
  157. * (or this will be called immediately).
  158. */
  159. @Override
  160. public void onCompletion(MediaPlayer mp) {
  161. mState = MediaItemStatus.PLAYBACK_STATE_FINISHED;
  162. mPlayer.setOnCompletionListener(null);
  163. }
  164. /**
  165. * Starts playback and sets completion listener.
  166. */
  167. @Override
  168. public void onPrepared(MediaPlayer mp) {
  169. mPlayer.start();
  170. mState = MediaItemStatus.PLAYBACK_STATE_PLAYING;
  171. mPlayer.setOnCompletionListener(this);
  172. }
  173. }