Android development of beep codes and vibration prompts the principle of implementation and reference code _android

Source: Internet
Author: User
Tags prepare
Recently in Reading Zxing project, learned a lot of things. Recommend that you also read. There is a Beepmanager class that implements a beep and vibration. Let's take a look at how he did it:
Bee-Ringing
1. Prepare an audio file for example: Beep.ogg. Ogg format is a kind of sound compression format, similar to MP3. When we are ready to play it, it produces a beep effect.
2. The default audio channel registered for the activity.
Activity.setvolumecontrolstream (Audiomanager.stream_music);
This is declared as the channel of Stream_music, is multimedia playback, after registering, we use the volume on the phone on the size of the key can adjust the playback sound size.
If this channel is not set, our activity Default Volume button processing will be used for the size of the phone ring tone.
3. Check the current ring tone mode, or become a situation mode.
Description: Getringermode ()--Returns the current ringtone mode. such as Ringer_mode_normal (ordinary), ringer_mode_silent (Mute), ringer_mode_vibrate (vibration)
Copy Code code as follows:

If you are currently in the ring tone mode, continue to prepare the following beep beep operation, if it is mute or vibrating mode. Don't go on. Because the user chooses the silent mode, we also do not make a noise.
Audiomanager Audioservice = (audiomanager) activity
. Getsystemservice (Context.audio_service);
if (Audioservice.getringermode ()!= audiomanager.ringer_mode_normal) {
Shouldplaybeep = false;
}

4. Initialize the MediaPlayer object and specify that the sound channel to play is Stream_music, which is consistent with the steps above and points to the same channel. MediaPlayer MediaPlayer = new MediaPlayer ();
Mediaplayer.setaudiostreamtype (Audiomanager.stream_music);
Registers an event. When the playback is complete, point again at the beginning of the stream file to prepare for the next playback.
Copy Code code as follows:

When the Beep has finished playing, rewind to queue up another one.
MediaPlayer
. Setoncompletionlistener (New Mediaplayer.oncompletionlistener () {
@Override
public void Oncompletion (MediaPlayer player) {
Player.seekto (0);
}
});

Set the data source and prepare to play
Copy Code code as follows:

Assetfiledescriptor file = Activity.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 IoE) {
LOG.W (TAG, IoE);
MediaPlayer = null;
}
return mediaPlayer;

5. Start Playback
Copy Code code as follows:

if (playbeep && mediaPlayer!= null) {
Mediaplayer.start ();
}

-----------------------------------------------------------------
Vibrating
This is relatively simple. A two-step:
1. Declaring permissions
Write in Androidmanifest.xml.
Copy Code code as follows:

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

2. Get vibration service.
Copy Code code as follows:

Vibrator Vibrator = (Vibrator) activity.getsystemservice (Context.vibrator_service);

3. Start Vibration.
Copy Code code as follows:

Vibrator.vibrate (vibrate_duration);

Copy Code code as follows:

public void Playbeepsoundandvibrate () {
if (enablevibrate) {
Vibra Tor vibrator = (vibrator) activity
. Getsystemservice (Context.vibrator_service);
//Shake once
vibrator.vibrate (vibrate_duration);
//The first parameter, which refers to an array of frequencies of vibrations. Each two is a group, the first of each group is the wait time, and the second is the vibration time.
//For example [2000,500,100,400], it waits 2000 milliseconds, vibrates 500, waits 100, vibrates
//The second parameter, repest refers to starting the loop vibration from the position of the first few indexes (one array parameter).
//Will be kept loop, we need to use Vibrator.cancel () active termination of the
//vibrator.vibrate (new long[]{300,500},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.