Android uses Audiomanager to modify the volume of the system _android

Source: Internet
Author: User
Tags sqlite database stub

The example in this article describes how Android uses Audiomanager to modify the volume of the system. Share to everyone for your reference, specific as follows:

Here are a few audiomanager ways to adjust the volume.

The first is to get the Audiomanager instance:

Audiomanager am= (Audiomanager) Getsystemservice (Context.audio_service);

There are two ways to adjust the volume method, one is the progressive, that is, like manually press the volume key, step by step increase or decrease, the other is to set the volume value directly.

1, Progressive type

public void Adjuststreamvolume (int streamtype, int direction, int flags)
Am.adjuststreamvolume ( Audiomanager.stream_music, Audiomanager.adjust_raise, audiomanager.flag_show_ui);

Explain three parameters

The first streamtype is the type of volume that needs to be adjusted, where the media volume is set, which can be:

Stream_alarm Alert
Stream_music Music Playback is the media volume
Stream_notification the top status bar of the window NOTIFICATION,
Stream_ring Ringtones
Stream_system system
Stream_voice_call Call
STREAM_DTMF dual Tone multi-frequency, not very clear what things

The second direction, which is the direction of the adjustment, increases or decreases, can be:

Adjust_lower Lower Volume
Adjust_raise Raise the Volume
Adjust_same remains unchanged, this is primarily used to present the current volume to the user

The third flags are some additional parameters that only describe the two common

Play sound when Flag_play_sound adjust volume
FLAG_SHOW_UI when adjusting the volume bar, that is, the volume button appears the

2, directly set the volume value of the method:

public void Setstreamvolume (int streamtype, int index, int flags)
Am.setstreamvolume (Audiomanager.stream_music, Am.getstreammaxvolume (Audiomanager.stream_music), audiomanager.flag_play_sound);
Am.getstreammaxvolume (Audiomanager.stream_voice_call)//Get the maximum value of the handset mode
am.getstreamvolume (AUDIOMANAGER.STREAM_ Voice_call);//Get current value of handset mode

The first and third parameters are the same as above

The second argument is a volume int value, getstreammaxvolume (int streamtype) gets the maximum value for that type of volume, and you can calculate the volume you want based on this value, and I'm going to go straight to the max.

Last of My Code:

Package COM.LP;
Import Java.io.File;
Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import java.io.IOException;
Import Java.io.InputStream;
Import Android.app.Activity;
Import Android.content.Context;
Import Android.media.AudioFormat;
Import Android.media.AudioManager;
Import Android.media.AudioTrack;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;
Import Android.widget.SeekBar; /** * Audiotrack plays audio such as WAV format * and allows to adjust volume * @author Administrator * */public class MainActivity5 extends activity {p
  Rivate Button play;
  Private Button stop;
  Private SeekBar Soundvalue;
  Private Audiotrack at;
  Private Audiomanager AM;
    @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
    Setcontentview (R.LAYOUT.MAIN_SK);
    am = (Audiomanager) getsystemservice (Context.audio_service);
    Play = (Button) Findviewbyid (R.id.main_sk_play); Stop = (BUtton) Findviewbyid (r.id.main_sk_stop);
    Soundvalue = (SeekBar) Findviewbyid (R.id.skbvolume);
    Setvolumecontrolstream (Audiomanager.stream_voice_call); Play.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) {if am.isspeake
        Rphoneon ()) {Am.setspeakerphoneon (false);
        }//setvolumecontrolstream (Audiomanager.stream_voice_call);
        Am.setmode (Audiomanager.mode_in_call);
        System.out.println (Am.getstreammaxvolume (Audiomanager.stream_voice_call));
        System.out.println ("&&&&&&&&&&&&&");
        System.out.println (Am.getstreamvolume (Audiomanager.stream_voice_call)); Am.setstreamvolume (streamtype, index, flags) int buffersizeinbytes = Audiotrack.getminbuffersize (44100, AudioFor Mat.
        Channel_out_mono, Audioformat.encoding_pcm_16bit); if (at==null) {at = new Audiotrack (Audiomanager.stream_voice_call, 44100, audioforMat.
          Channel_out_mono, Audioformat.encoding_pcm_16bit, Buffersizeinbytes, Audiotrack.mode_stream);
          System.out.println ("22222");
          At.setstereovolume (100f, 100f);
        At.setstereovolume (0.7f, 0.7f);/Set the current volume size new Audiotrackthread (). Start ();
          }else{if (at.getplaystate () ==audiotrack.playstate_playing) {System.out.println ("111111111");
            }else{System.out.println ("33333"); at = new Audiotrack (Audiomanager.stream_voice_call, 44100, Audioformat.channel_out_mono, audioformat.encoding_pcm_
            16BIT, Buffersizeinbytes, Audiotrack.mode_stream);
          New Audiotrackthread (). Start ();
    }
        }
      }
    }); Stop.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) {if at.getplays
          Tate () ==audiotrack.playstate_playing) {try{at.stop (); }catch (IllegalStateException e) {E.printstacktraCE ();
          } at.release ();
        Am.setmode (Audiomanager.mode_normal);
}
      }
    }); Soundvalue.setmax (100);//volume adjustment limit//soundvalue.setprogress (70);//Set Seekbar position value Soundvalue.setmax (am.getstream
    Maxvolume (Audiomanager.stream_voice_call));
    Soundvalue.setprogress (Am.getstreamvolume (Audiomanager.stream_voice_call)); Soundvalue.setonseekbarchangelistener (New Seekbar.onseekbarchangelistener () {@Override public void Onstoptrac Kingtouch (SeekBar SeekBar) {//float vol= (float) (seekbar.getprogress ())/(float) (Seekbar.getmax ());//System.
Out.println (vol.); At.setstereovolume (vol., Vol.)//Set volume Am.setstreamvolume (Audiomanager.stream_voice_call, Seekbar.getprogres
      S (), audiomanager.flag_play_sound);
      @Override public void Onstarttrackingtouch (SeekBar SeekBar) {//TODO auto-generated stub @Override public void onprogresschanged (SeekBar SeekBar, int progress, Boolean Fromuser) {//TODO auto-generated Method stub}}); 
      Class Audiotrackthread extends thread{@Override public void Run () {byte[] out_bytes = new byte[44100];
      InputStream is = Getresources (). Openrawresource (R.raw.start);
      int length;
      try{At.play ();
      }catch (IllegalStateException e) {e.printstacktrace ();
          The try {while (length = Is.read (out_bytes))!=-1) {//system.out.println (length);
        At.write (out_bytes, 0, length);
      } catch (IOException e) {e.printstacktrace ();
        } if (At.getplaystate () ==audiotrack.playstate_playing) {try{at.stop ();
        }catch (IllegalStateException e) {e.printstacktrace ();
        } at.release ();
      Am.setmode (Audiomanager.mode_normal);

 }
    }
  }
}

And of course you have to set permissions

<uses-permission android:name= "Android.permission.MODIFY_AUDIO_SETTINGS"/>
  <uses-permission Android : Name= "Android.permission.WRITE_EXTERNAL_STORAGE"/>
  <uses-permission android:name= " Android.permission.RECORD_AUDIO "/>

PS: About Android Manifest features and permissions set details can refer to the site online tools:

Android manifest Features and Permissions description Encyclopedia:

Http://tools.jb51.net/table/AndroidManifest

For more information on Android-related content readers can view the site: "Android View tips Summary", "Android Programming activity Operating Skills Summary", "Android operation SQLite Database Skills Summary", " Android operation JSON format data tips summary, "Android Database Operation skills Summary", "Android File Operation skills Summary", "Android programming development of SD card Operation Summary", "Android Development introduction and Advanced Course", " Android Resource Operation tips Summary and the "Android Controls usage Summary"

I hope this article will help you with the Android program.

Related Article

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.