- Controlling Your App’s Volume and Playback
- This lesson teaches you to
- You should also read
- Identify Which Audio Stream to Use
- Use Hardware Volume Keys to Control Your App’s Audio Volume
- Use Hardware Playback Control Keys to Control Your App’s Audio Playback
- How to Increase/Decrease Screen Brightness using Volume Keys Programmatically in Android?
- Where can we use this feature?
- Approach
- How to Use Virtual Volume Keys in Android
- How To : 9 Alternative Uses for Your Android’s Volume Keys
- How to Skip Songs on Android using Volume Keys
- Skip Songs on Android Device using Volume keys
- Prerequisites
- Granting ADB Permissions
- Steps to Skip Song Using Volume Keys
- Alternative to Skip Track
Controlling Your App’s Volume and Playback
This lesson teaches you to
You should also read
A good user experience is a predictable one. If your app plays media it’s important that your users can control the volume of your app using the hardware or software volume controls of their device, bluetooth headset, or headphones.
Similarly, where appropriate and available, the play, stop, pause, skip, and previous media playback keys should perform their respective actions on the audio stream used by your app.
Identify Which Audio Stream to Use
The first step to creating a predictable audio experience is understanding which audio stream your app will use.
Android maintains a separate audio stream for playing music, alarms, notifications, the incoming call ringer, system sounds, in-call volume, and DTMF tones. This is done primarily to allow users to control the volume of each stream independently.
Most of these streams are restricted to system events, so unless your app is a replacement alarm clock, you’ll almost certainly be playing your audio using the STREAM_MUSIC stream.
Use Hardware Volume Keys to Control Your App’s Audio Volume
By default, pressing the volume controls modify the volume of the active audio stream. If your app isn’t currently playing anything, hitting the volume keys adjusts the ringer volume.
If you’ve got a game or music app, then chances are good that when the user hits the volume keys they want to control the volume of the game or music, even if they’re currently between songs or there’s no music in the current game location.
You may be tempted to try and listen for volume key presses and modify the volume of your audio stream that way. Resist the urge. Android provides the handy setVolumeControlStream() method to direct volume key presses to the audio stream you specify.
Having identified the audio stream your application will be using, you should set it as the volume stream target. You should make this call early in your app’s lifecycle—because you only need to call it once during the activity lifecycle, you should typically call it within the onCreate() method (of the Activity or Fragment that controls your media). This ensures that whenever your app is visible, the volume controls function as the user expects.
From this point onwards, pressing the volume keys on the device affect the audio stream you specify (in this case “music”) whenever the target activity or fragment is visible.
Use Hardware Playback Control Keys to Control Your App’s Audio Playback
Media playback buttons such as play, pause, stop, skip, and previous are available on some handsets and many connected or wireless headsets. Whenever a user presses one of these hardware keys, the system broadcasts an intent with the ACTION_MEDIA_BUTTON action.
To respond to media button clicks, you need to register a BroadcastReceiver in your manifest that listens for this action broadcast as shown below.
The receiver implementation itself needs to extract which key was pressed to cause the broadcast. The Intent includes this under the EXTRA_KEY_EVENT key, while the KeyEvent class includes a list KEYCODE_MEDIA_* static constants that represents each of the possible media buttons, such as KEYCODE_MEDIA_PLAY_PAUSE and KEYCODE_MEDIA_NEXT .
The following snippet shows how to extract the media button pressed and affects the media playback accordingly.
Because multiple applications might want to listen for media button presses, you must also programmatically control when your app should receive media button press events.
The following code can be used within your app to register and de-register your media button event receiver using the AudioManager . When registered, your broadcast receiver is the exclusive receiver of all media button broadcasts.
Typically, apps should unregister most of their receivers whenever they become inactive or invisible (such as during the onStop() callback). However, it’s not that simple for media playback apps—in fact, responding to media playback buttons is most important when your application isn’t visible and therefore can’t be controlled by the on-screen UI.
A better approach is to register and unregister the media button event receiver when your application gains and loses the audio focus. This is covered in detail in the next lesson.
Источник
How to Increase/Decrease Screen Brightness using Volume Keys Programmatically in Android?
Screen brightness is one such factor that directly affects the Users as well as the Battery on a device. Android devices are Smart systems and have an inbuilt system for Auto-Brightness. But mostly this feature is unchecked by the users or set off by default. Irrespective of whether this feature is present, set on or off, or absent in any device, a developer must take this opportunity into consideration and develop an optimized application. Anything that is declared inside the application might have an effect on the outside space, i.e., if the screen brightness was changed programmatically from an application, the brightness value might stay unaltered even after exiting the application. So one must try to trace back the originals and set them before a user exits.
Where can we use this feature?
- Non-Media: Non-media (music or video) applications could use the volume buttons to increase or decrease the brightness of a screen in steps.
- Book Reader Applications: Reading books can sometimes be a moody choice, one can increase or decrease brightness directly from the application in steps.
- News Paper Applications: One can increase or decrease brightness directly from the application in steps.
Through this article, we want you to know that volume buttons could be used for increasing and decreasing the brightness of a screen. A sample GIF is given below to get an idea about what we are going to do in this article . Note that we are going to implement this project using the Kotlin language.
Approach
Step 1: Create a New Project
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Kotlin as the programming language.
Источник
How to Use Virtual Volume Keys in Android
Android smartphones have a small set of volume keys often called the volume rocker. The key on the up is used to turn the volume up and the lower key is used for turning it down. These keys are actually made of very tiny metallic push switches. Since these keys are used a lot, the metal used to spring back the keys in the off position soon wears out and the keys stop working properly.
If the volume keys in your Android smartphone are broken then you can still manage to easily turn the volume up or down using a set of virtual volume keys using the app named Virtual Volume Key. This app displays the volume keys right on the smartphone screen so that you can turn the volume up or down by touching on these small virtual buttons on the screen.
Once the Virtual Volume Key app is installed in your Android phone, you have to launch this app and turn it on. A small volume rocker is instantly displayed on your device screen. You can drag it around anywhere on your screen. But the keys are soon dimmed and docked to one edge of your screen. How dim the keys become depends on the “diaphaneity” value of the app. This is basically the alpha value for the volume rocker and makes the display more or less transparent. Similarly, how long it takes to dock to one of the edges or sides of the screen depends on the “wait time” value which is set in number of seconds.
These virtual volume keys work exactly like the physical volume keys in all kinds of situations. It is great help when the physical volume keys are broken or have worn out due to heavy and long use. You can also use this app to make the volume keys last longer in a smartphone.
Источник
How To : 9 Alternative Uses for Your Android’s Volume Keys
As touchscreen phones continue to evolve, the need for physical buttons seems less and less pronounced. While hard keys may seem like an endangered component on mobile devices, save for the Samsung Galaxy line, it’d be difficult to fathom a world where volume keys are absent.
However, the volume buttons on an Android smartphone don’t have to be limited to strictly lowering and raising sounds. You can make them do almost anything, from launching apps to scrolling through webpages.
Using your volume buttons to launch an application is a great way to save some home screen space and access your favorite app without needing to search for it. Along with launching apps, you’ll also be able to perform a number of additional tasks.
We’re all familiar with using the power key to wake the screen; it’s an everyday practice. To make things even easier, or if your power button is weak or busted, you can give your volume keys this same ability.
Scrolling is easy enough, but why not make it easier. Leave your hand where it is and use the volume rockers to scroll up and down in designated applications.
There’s no need to wake or unlock your device when the volume keys are perfectly capable of playing and skipping tracks. It’s a convenient mod that enhances the music-listening experience on your Android device.
I have always struggled with text editing on Android, as I regularly battled with placing the cursor in the exact spot I needed it. But not anymore—with a simple mod I’m able to move the cursor forward and backwards by pressing the volume up and down buttons.
You never know when a special moment will commence right before your eyes; some unique or embarrassing circumstance that you want to catch on video. In attempt to remain inconspicuous, initiate a secret video recording with the volume buttons just like a spy.
While using a PIN or passcode to lock an application isn’t anything new, locking your apps with a stealthy volume combination is. A passcode is easy to figure out if someone is looking over your shoulder, so make it harder to crack by using your volume keys.
Waking your device with a volume key is useful, but completely unlocking it and bypassing the lock screen is even more convenient. With a secret combination of your choosing, you can unlock your Android device with a sequence of volume key presses.
When you need your flashlight, you need it now, so make activating your smartphone’s torch even faster by using the volume buttons. This can be done without having to wake or unlock the device.
Got any other volume key tricks? Share them below! And for make sure to follow Gadget Hacks on Facebook, Google+, and Twitter to stay up to date with all the latest and greatest how-tos and Android news.
Keep Your Connection Secure Without a Monthly Bill. Get a lifetime subscription to VPN Unlimited for all your devices with a one-time purchase from the new Gadget Hacks Shop, and watch Hulu or Netflix without regional restrictions, increase security when browsing on public networks, and more.
Источник
How to Skip Songs on Android using Volume Keys
You all might be using the Volume keys while playing music for adjusting sounds. But what if the same keys could in fact change tracks as well. Yes, it is very much possible, thanks to a hidden API that was released alongside Android Oreo 8.1. The API, known as volume key long-press listener, makes it possible to skip song tracks just by long-pressing the Volume Keys.
Taking a cue from this API is an app that goes by the name Skip Track. It lets you skip songs even when your screen display is turned off. So you could now easily change or skip song tracks via Volume keys when your device is kept inside your pocket. Let’s see how this could be achieved. Similar to it, you could also take the help of these 5 apps to remap your hardware keys for different purposes.
Skip Songs on Android Device using Volume keys
To carry out the process of skipping soundtracks using Volume keys, you need to have the Skip Track app on your device. Once you do so, you will need to grant it the necessary permission through ADB Commands. And for that, you will have to enable USB Debugging on your device. All these are explained below.
Prerequisites
- Download Android SDK and Platform Tools on your PC. Make sure to remember the location where you are installing it.
- Download and install the Skip Trackapp from Github. Thanks to XDA member Cilenco for this app.
- Enable USB Debugging on your device: Head over to Settings >System >About phone > Tap on Build Number 7 times. This will enable Developer options. Under that section, enable the USB Debugging toggle.
So this was all the requirements for the steps to skip songs on Android devices using Volume keys. Now follow the below instructions given below.
Granting ADB Permissions
- Connect your device to PC via USB cable.
- Navigate to the platform-tools folder and type in cmd in the address bar. This will open the Command Prompt.
- Type the below code to make sure that the device is successfully connected (if you get a device ID, then the connection is successful):
- Finally, give the app the required API permission by executing the below command. Make sure you have installed the Skip Track app before going ahead with this command. Otherwise, you won’t be able to skip songs using Volume keys in this app.
Steps to Skip Song Using Volume Keys
- Open the Skip Track app on your device.
- Just enable the toggle next to Enable Service. You will also have to grant the app access to the notifications. To do so, enable the Skip Track Settings toggle once you are in the Notification access section.
- Now all you have to do is open your favorite music player and try skipping songs using Volume keys. I tried the same on Spotify, and it worked wonders, without any fuss. Just make sure to long-press the Volume keys, while the screen display is turned off.
Alternative to Skip Track
If you don’t want to try out Skip Track since it requires sideloading from Github, well there is another app available on the Play Store. The app is Next Track, and it also requires a similar ADB setup. The steps to skip songs using Volume keys are near identical, just replace the above ADB permission code with the one mentioned below:
Most of the features of this app require you to shell out some bucks. If you are ready to do so, then it is worth a try. Otherwise, you may stick with Skip track. That’s all from this guide on how to skip tracks on Android devices using Volume Keys. Do let us know in the comments below which of the two apps you ultimately end up choosing.
Источник