--audiomanager of system services provided by Android (audio Manager)

Source: Internet
Author: User

--audiomanager of system services provided by Android (audio Manager)

----Reprint Please specify the Source: Coder-pig



Audiomanager related introduction and common method diagram:





Simple examples of use:

Using MediaPlayer to play music, adjust the volume and mute by Audiomanager:

Here, we need to put the audio file to play to the raw folder under Res, this folder is not the default, you need to create it yourself Oh!

To put the native resources, that is, packaging compilation will not turn him into a binary file!!!


Take a look at it first:


is to play the music, then turn up the volume when you can see the slider, and then turn down the volume is set to the sound, do not display the slider bar:

This is determined by the third parameter of the Adjuststreamvolume () Method!

All right, let's start writing the code:


Simple layout: activity_main.xml:

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:id=" @+id/linearlayout1 "android:layout_width=" Match_parent "Android:layout_hei ght= "match_parent" android:orientation= "vertical" tools:context= "com.jay.example.audiomanagerdemo.MainActivity" &    Gt <button android:id= "@+id/btnstart" android:layout_width= "wrap_content" android:layout_height= "wrap _content "android:text=" Play "/> <button android:enabled=" false "android:id=" @+id/btns     Top "android:layout_width=" Wrap_content "android:layout_height=" wrap_content "android:text=" Stop "/> <button android:id= "@+id/btnhigher" android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:text= "Volume Up"/> <button android:id= "@+id/btnlower" Android:layout_ Width= "Wrap_content" Android:layout_height= "Wrap_content" android:text= "Volume Down"/> <button android:id= "@+id/btnquite" Android: Layout_width= "Wrap_content" android:layout_height= "wrap_content" android:text= "mute"/> </LinearLayou T>


Then the Mainactivity.java:

Package Com.jay.example.audiomanagerdemo;import Android.app.activity;import Android.app.service;import Android.media.audiomanager;import Android.media.mediaplayer;import Android.os.bundle;import Android.view.View; Import Android.view.view.onclicklistener;import Android.widget.button;public class Mainactivity extends Activity Implements Onclicklistener{private MediaPlayer meplayer;private audiomanager amanager;private Button btnstart;private button btnstop;private button btnhigher;private button btnlower;private button btnquite;//defines a flag to indicate whether to click the Mute button private int flag = 1; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); btnstart = (Button) Findviewbyid (r.id.btnstart); btnstop = (Button) Findviewbyid (r.id.btnstop); btnhigher = (Button) Findviewbyid (r.id.btnhigher); btnlower = (Button) Findviewbyid ( R.id.btnlower); btnquite = (Button) Findviewbyid (r.id.btnquite);//Get System Audio Object Amanager = (Audiomanager) GetsystemserviCE (service.audio_service);//Initialize the MediaPlayer object, which plays the MP3 resource in the raw file Meplayer = Mediaplayer.create (Mainactivity.this, R.raw.one);//Set Loop playback: Meplayer.setlooping (true); Btnstart.setonclicklistener (this); Btnstop.setonclicklistener ( This), Btnhigher.setonclicklistener (This), Btnlower.setonclicklistener (This), Btnquite.setonclicklistener (this);} @Overridepublic void OnClick (View v) {switch (V.getid ()) {case r.id.btnstart:btnstop.setenabled (true); Meplayer.start ( ); btnstart.setenabled (false); Break;case r.id.btnstop:btnstart.setenabled (true); Meplayer.pause (); Btnstop.setenabled (false); Break;case r.id.btnhigher://Specifies that the audio of the music be adjusted, the volume is increased, and the volume is displayed graphically Amanager.adjuststreamvolume ( Audiomanager.stream_music,audiomanager.adjust_raise, audiomanager.flag_show_ui); Break;case R.id.btnlower:// Specifies the audio to adjust the music, lower the volume, only the sound, does not display the graphics bar Amanager.adjuststreamvolume (Audiomanager.stream_music,audiomanager.adjust_lower, Audiomanager.flag_play_sound); break;case r.id.btnquite://Specifies the audio to adjust the music, depending on the ischecked to determine whether to mute the flag *= -1;if (flag = =-1) { Amanager.setstreammute(audiomanager.stream_music,true); Btnquite.settext ("Unmute");} Else{btnquite.settext ("mute"); Amanager.setstreammute (Audiomanager.stream_music,false);} Break;}}}

The code is also very simple, here is not too much to explain!

there is another point to note: if Adjuststreamvolume () The third parameter you set the vibration (vibrator), need in Androidmanifest.xml

Add this permission in the Oh!

<uses-permission android:name= "Android.permission.VIBRATE"/>


This section of the demo download:audiomanager Simple use demo download




--audiomanager (audio Manager) for system services provided by Android

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.