diff options
author | Björn Stenberg <bjorn@haxx.se> | 2011-03-14 12:25:48 +0000 |
---|---|---|
committer | Björn Stenberg <bjorn@haxx.se> | 2011-03-14 12:25:48 +0000 |
commit | e50cc0e3d760d4fdb99f971070a6943ee15a9a98 (patch) | |
tree | 9d22cde7d39a2aefbc75cd970916fbc161f53cac /android | |
parent | 606bed035ed898df1d1bf4a8c7be904f76b118ff (diff) |
Listen to and follow external Android volume changes. (Based on FS#11914 by Maurus Cuelenaere)
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@29586 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'android')
-rw-r--r-- | android/src/org/rockbox/RockboxPCM.java | 52 |
1 files changed, 49 insertions, 3 deletions
diff --git a/android/src/org/rockbox/RockboxPCM.java b/android/src/org/rockbox/RockboxPCM.java index 5a8354abee..7d01da6150 100644 --- a/android/src/org/rockbox/RockboxPCM.java +++ b/android/src/org/rockbox/RockboxPCM.java @@ -23,8 +23,10 @@ package org.rockbox; import java.util.Arrays; +import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; +import android.content.IntentFilter; import android.media.AudioFormat; import android.media.AudioManager; import android.media.AudioTrack; @@ -52,8 +54,10 @@ public class RockboxPCM extends AudioTrack private AudioManager audiomanager; private int maxstreamvolume; + private int setstreamvolume = -1; private float minpcmvolume; private float pcmrange; + private RockboxService rbservice; private void LOG(CharSequence text) { @@ -72,15 +76,55 @@ public class RockboxPCM extends AudioTrack l = new PCMListener(buf_len); /* find cleaner way to get context? */ - final RockboxService rb = RockboxService.get_instance(); + rbservice = RockboxService.get_instance(); audiomanager = - (AudioManager) rb.getSystemService(Context.AUDIO_SERVICE); + (AudioManager) rbservice.getSystemService(Context.AUDIO_SERVICE); maxstreamvolume = audiomanager.getStreamMaxVolume(streamtype); minpcmvolume = getMinVolume(); pcmrange = getMaxVolume() - minpcmvolume; + + setupVolumeHandler(); } + private native void postVolumeChangedEvent(int volume); + private void setupVolumeHandler() + { + BroadcastReceiver broadcastReceiver = new BroadcastReceiver() + { + @Override + public void onReceive(Context context, Intent intent) + { + int streamType = intent.getIntExtra( + "android.media.EXTRA_VOLUME_STREAM_TYPE", -1); + int volume = intent.getIntExtra( + "android.media.EXTRA_VOLUME_STREAM_VALUE", -1); + + if (streamType == RockboxPCM.streamtype && + volume != -1 && + volume != setstreamvolume && + rbservice.isRockboxRunning()) + { + int rbvolume = ((maxstreamvolume - volume) * -99) / + maxstreamvolume; + postVolumeChangedEvent(rbvolume); + } + } + }; + + /* at startup, change the internal rockbox volume to what the global + android music stream volume is */ + int volume = audiomanager.getStreamVolume(streamtype); + int rbvolume = ((maxstreamvolume - volume) * -99) / maxstreamvolume; + postVolumeChangedEvent(rbvolume); + + /* We're relying on internal API's here, + this can break in the future! */ + rbservice.registerReceiver( + broadcastReceiver, + new IntentFilter("android.media.VOLUME_CHANGED_ACTION")); + } + private int bytes2frames(int bytes) { /* 1 sample is 2 bytes, 2 samples are 1 frame */ @@ -164,8 +208,10 @@ public class RockboxPCM extends AudioTrack } int oldstreamvolume = audiomanager.getStreamVolume(streamtype); - if (streamvolume != oldstreamvolume) + if (streamvolume != oldstreamvolume) { + setstreamvolume = streamvolume; audiomanager.setStreamVolume(streamtype, streamvolume, 0); + } } public native void pcmSamplesToByteArray(byte[] dest); |