Android player volume control-use vibration sample code-progress bar to control volume

Source: Internet
Author: User

When writing a program, we will use the following code to notify the sound to implement this behavior:

Setvolumecontrolstream (audiomanager. stream_music );
Mediaplayer = new mediaplayer ();
Mediaplayer. setaudiostreamtype (audiomanager. stream_music );
// Mediaplayer. setoncompletionlistener (beeplistener );
Assetfiledescriptor file = getresources (). openrawresourcefd (R. Raw. Beep );
Try {
Mediaplayer. setdatasource (file. getfiledescriptor (), file. getstartoffset (),
File. getlength ());
File. Close ();
Mediaplayer. setvolume (beep_volume, beep_volume );
Mediaplayer. Prepare ();
} Catch (ioexception e ){
Mediaplayer = NULL;
}
Mediaplayer. Start ();

The following code uses the system vibration:

/Prompt
Vibrator = (vibrator) getsystemservice (vibrator_service );
Long [] pattern = {800, 50,400, 30}; // off/On/Off/on...
Vibrator. vibrate (pattern, 2); //-1 is not repeated, and non-1 is repeated starting from the specified subscript of Pattern

The system volume has the following parameters:

136/** the audio stream for system sounds */
137 public static final int stream_system = audiosystem. stream_system;
138/** the audio stream for the phone ring */
139 public static final int stream_ring = audiosystem. stream_ring;
140/** the audio stream for music playback */
141 public static final int stream_music = audiosystem. stream_music;

If you want to use the progress box for adjustment, you can use the following code:

Mediaplayer MP
=
Mediaplayer. Create (context, R. Raw. Recv );
MP. setaudiostreamtype (audiomanager. stream_music );

Int
Maxvolume
=
Maudiomanager. getstreammaxvolume (audiomanager. stream_music );
MP. setvolume (maxvolume
*
Mlastprogress
/

10
, Maxvolume
*
Mlastprogress
/

10
);

Audiomanager maudiomanager
=
(Audiomanager) Context. getsystemservice (context. audio_service );
Maudiomanager. setstreamvolume (audiomanager. stream_music, maxvolume
*
Mlastprogress
/

10
,
0
);

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.