From faef0888c2b564e2380061bfbb8ead2243f6a0cb Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Sun, 9 Jun 2013 13:31:45 +0200 Subject: [PATCH] Renderer volume can be changed with volume keys. --- .../nutomic/controldlna/MainActivity.java | 21 ++++++++++ .../nutomic/controldlna/RendererFragment.java | 38 +++++++++++++++++++ 2 files changed, 59 insertions(+) diff --git a/src/com/github/nutomic/controldlna/MainActivity.java b/src/com/github/nutomic/controldlna/MainActivity.java index 82e0cae..1ef8a42 100644 --- a/src/com/github/nutomic/controldlna/MainActivity.java +++ b/src/com/github/nutomic/controldlna/MainActivity.java @@ -10,6 +10,7 @@ import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.app.FragmentTransaction; import android.support.v4.view.ViewPager; +import android.view.KeyEvent; import com.actionbarsherlock.app.ActionBar; import com.actionbarsherlock.app.SherlockFragmentActivity; @@ -144,5 +145,25 @@ public class MainActivity extends SherlockFragmentActivity implements getSupportActionBar().selectTab(getSupportActionBar().getTabAt(0)); mRendererFragment.setPlaylist(playlist, start); } + + /** + * Sends volume key events to RendererFragment (which sends them to + * media renderer). + */ + @Override + public boolean dispatchKeyEvent(KeyEvent event) { + switch (event.getKeyCode()) { + case KeyEvent.KEYCODE_VOLUME_UP: + if (event.getAction() == KeyEvent.ACTION_DOWN) + mRendererFragment.changeVolume(true); + return true; + case KeyEvent.KEYCODE_VOLUME_DOWN: + if (event.getAction() == KeyEvent.ACTION_DOWN) + mRendererFragment.changeVolume(false); + return true; + default: + return super.dispatchKeyEvent(event); + } + } } diff --git a/src/com/github/nutomic/controldlna/RendererFragment.java b/src/com/github/nutomic/controldlna/RendererFragment.java index 0fb8491..df83b65 100644 --- a/src/com/github/nutomic/controldlna/RendererFragment.java +++ b/src/com/github/nutomic/controldlna/RendererFragment.java @@ -28,6 +28,8 @@ import org.teleal.cling.support.model.DIDLContent; import org.teleal.cling.support.model.PositionInfo; import org.teleal.cling.support.model.SeekMode; import org.teleal.cling.support.model.item.Item; +import org.teleal.cling.support.renderingcontrol.callback.GetVolume; +import org.teleal.cling.support.renderingcontrol.callback.SetVolume; import android.app.AlertDialog; import android.content.ComponentName; @@ -484,4 +486,40 @@ public class RendererFragment extends Fragment implements public void onStopTrackingTouch(SeekBar seekBar) { } + /** + * Increases or decreases volume. + * + * @param increase True to increase volume, false to decrease. + */ + @SuppressWarnings("rawtypes") + public void changeVolume(final boolean increase) { + if (mCurrentRenderer == null) + return; + final Service service = mCurrentRenderer.findService( + new ServiceType("schemas-upnp-org", "RenderingControl")); + mUpnpService.getControlPoint().execute(new GetVolume(service) { + + @Override + public void failure(ActionInvocation invocation, + UpnpResponse operation, String defaultMessage) { + Log.d(TAG, "Failed to get current Volume: " + defaultMessage); + } + + @Override + public void received(ActionInvocation invocation, int volume) { + int newVolume = volume + ((increase) ? 4 : -4); + if (newVolume < 0) newVolume = 0; + mUpnpService.getControlPoint().execute(new SetVolume(service, + newVolume) { + + @Override + public void failure(ActionInvocation invocation, + UpnpResponse operation, String defaultMessage) { + Log.d(TAG, "Failed to set new Volume: " + defaultMessage); + } + }); + } + }); + } + }