Android custom Seekbar dynamically change hardware volume size and volume key synchronization

Source: Internet
Author: User

1,:

2, Code:

Mainactivity.java

 PackageCom.hero.zhaoq.seekbarchangeddemo;ImportAndroid.content.BroadcastReceiver;ImportAndroid.content.Context;ImportAndroid.content.Intent;ImportAndroid.content.IntentFilter;ImportAndroid.database.ContentObserver;ImportAndroid.media.AudioManager;ImportAndroid.os.Handler;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.os.Bundle;ImportAndroid.util.Log;ImportAndroid.widget.SeekBar;/** * Android implements Seekbar update volume and synchronizes with hardware */ Public  class mainactivity extends appcompatactivity {    PrivateSeekBar Seekbar_video;PrivateContentobserver Mvoiceobserver;@Override    protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);        Setcontentview (R.layout.activity_main); Audiomanager Audiomanager = (audiomanager) getsystemservice (Context.audio_service);//Get Media system Servicesseekbar_video= (Seekbar) Findviewbyid (R.id.seekbar_video);//Registration IDSeekbar_video.setmax ( the);//Set Maximum volumeSeekbar_video.setprogress (Audiomanager.getstreamvolume (audiomanager.stream_music));//Current Media volumeMyregisterreceiver ();//Register for simultaneous updates of the broadcastLOG.I ("Lyj_ring","Mvoiceseekbar max voluem ="+audiomanager.getstreammaxvolume (Audiomanager.stream_system)); Seekbar_video.setonseekbarchangelistener (NewSeekbar.onseekbarchangelistener () { Public void Onstoptrackingtouch(SeekBar arg0) {            } Public void Onstarttrackingtouch(SeekBar arg0) {            } Public void onprogresschanged(SeekBar arg0,intArg1,BooleanARG2) {Audiomanager Audiomanager = (audiomanager) getsystemservice (Context.audio_service); LOG.V ("Lyj_ring","Mvoiceseekbar max progress ="+ARG1);//System volume and Media volume update simultaneouslyAudiomanager.setstreamvolume (Audiomanager.stream_system, Arg1,0); Audiomanager.setstreamvolume (3, Arg1,0);//3 for Audiomanager.stream_music}        }); Mvoiceobserver =NewContentobserver (NewHandler ()) {@Override             Public void OnChange(BooleanSelfchange) {Super. OnChange (Selfchange);                Audiomanager Audiomanager = (audiomanager) getsystemservice (Context.audio_service); Seekbar_video.setprogress (Audiomanager.getstreamvolume (Audiomanager.stream_system));//or you can use the media volume to listen for changes, the effect is the same.                 //mvoiceseekbar.setprogress (Audiomanager.getstreamvolume (Audiomanager.stream_music));}        }; }Private void Myregisterreceiver() {Myvolumereceiver mvolumereceiver =NewMyvolumereceiver (); Intentfilter filter =NewIntentfilter (); Filter.addaction ("Android.media.VOLUME_CHANGED_ACTION") ;    Registerreceiver (mvolumereceiver, filter); }/** * Interface display when handling volume changes * @author Long * *    Private  class myvolumereceiver extends broadcastreceiver {        @Override         Public void OnReceive(context context, Intent Intent) {//Change the position of the Seekbar if the volume has changed            if(Intent.getaction (). Equals ("Android.media.VOLUME_CHANGED_ACTION") {Audiomanager Maudiomanager = (audiomanager) context.getsystemservice (Context.audio_service);intCurrvolume = Maudiomanager.getstreamvolume (Audiomanager.stream_music);//Current Media volumeSeekbar_video.setprogress (Currvolume); }        }    }}

Main_activity.xml:

<?xml version= "1.0" encoding= "Utf-8"?><linearlayout  xmlns: Android  = "http://schemas.android.com/apk/res/android"  android:id  =" @+id/ringtone_seekbar_parent " android:layout_width  =" match_parent " android:layout_height  =" 160px " android:layout_margintop  = "0dip"  android:layout_weight  = "1"   android:gravity  = "center_vertical"  android:o Rientation  =;     <seekbar  android:id  = "@+id/seekbar_video"  android:layout_width< /span>= "match_parent"  android:layout_height  = "25DP"  android:max  =         "0"  android:progress  = "0"  android:progressdrawable  =" @drawable/seekbar_progress_layer_ List " android:thumb  =" @mipmap/play_seekbar_ Icon " android:layout_weight  =" all "/ </linearlayout>

Seekbar_progress_layer_list.xml

<?xml version= "1.0" encoding= "Utf-8"?><layer-listxmlns:android="Http://schemas.android.com/apk/res/android">         <!--unchecked State --    <item android:id="@android: Id/background"android:drawable= "@mipmap/volumn_bg" />            <!--&lt;! &ndash; checked status &ndash;&gt;-->    <item android:id="@android: id/progress">        < scale android:drawable="@mipmap/volumn_primary" android:scalewidth= "100%" />    </Item></layer-list>

Not you, not just you don't know ...
The entire code implementation is simple. The author does not repeat.

Note: Open the demo and open the song. Swipe Seekbar to change the volume size.

Maybe you need this more:

Source:
Https://github.com/229457269/SeekBarChangedVoiceDemo

Reference: http://blog.csdn.net/lyjit/article/details/51538951

Android Custom Seekbar dynamically changes the hardware volume size and volume keys synchronization

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.