Android Play Beep

Source: Internet
Author: User

There are two main ways of doing this:

Add a sound file to your project's Res/raw directory.

1, MediaPlayer play general audio

private void PlaySound () {final MediaPlayer MediaPlayer = mediaplayer.create (activity, r.raw.bubble);  Mediaplayer.start (); Mediaplayer.setoncompletionlistener (New Mediaplayer.oncompletionlistener () {//Broadcast completed event  @Override public void Oncompletion (MediaPlayer arg0) {  mediaplayer.release ();}}  );  }

2, Soundpool play short sound effects (maximum can only apply for 1M of memory space)

private void Playsounds () {//Specifies the maximum number of audio streams for the sound pool is 10, with a sound quality of 5  soundpool pool = new Soundpool (audiomanager.stream_system , 5);  Load the audio stream, return the ID in the pool  final int sourceid = pool.load (activity, r.raw.bubble, 0);  Pool.setonloadcompletelistener (New Onloadcompletelistener () {public  void Onloadcomplete (Soundpool SoundPool, int SampleID, int status) {//Play audio, second parameter is left channel volume, third parameter is right channel volume,//fourth parameter is priority, fifth parameter is loop number, 0 does not loop, 1 loop;//sixth parameter is rate, rate    Minimum 0.5 up to 2, 1 for normal speed  soundpool.play (SourceID, 2, 2, 0, 0, 1);});}

Android Play Beep

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.