Android-set and play ringtones

Source: Internet
Author: User
In the Android system, the path for storing Different ringtones is as follows:
/System/Media/Audio/ringtones ringtone/system/Media/Audio/notifications SMS notification ringtone/system/Media/Audio/alarms alert ringtone
Ringtone settings:
Import Java. io. file; import android. app. activity; import android. content. intent; import android. media. ringtonemanager; import android.net. uri; import android. OS. bundle; import android. view. view; import android. widget. button; public class activity01 extends activity {/* 3 buttons */private button mbuttonringtone; private button mbuttonalarm; private button mbuttonnotification;/* Custom type */public static final int Tonringtone = 0; public static final int buttonalarm = 1; public static final int buttonnotification = 2;/* ringtone folder */private string strringtonefolder = "/sdcard/music/ringtones "; private string stralarmfolder = "/sdcard/music/alarms"; private string strnotificationfolder = "/sdcard/music/notifications";/** called when the activity is first created. * // @ overridepublic void oncreate (bundle savedinstancestate) {Super. oncreate (savedinstancestate); setcontentview (R. layout. main); mbuttonringtone = (button) findviewbyid (R. id. buttonringtone); mbuttonalarm = (button) findviewbyid (R. id. buttonalarm); mbuttonnotification = (button) findviewbyid (R. id. buttonnotification);/* set the ringtone */mbuttonringtone. setonclicklistener (New button. onclicklistener () {@ overridepublic void onclick (view arg0) {If (bfolder (strringtonefolder )) {// Set intent = new intent (ringtonemanager. action_ringtone_picker); // The type is incoming call ringtoneintent. putextra (ringtonemanager. extra_ringtone_type, ringtonemanager. type_ringtone); // sets the displayed titleintent. putextra (ringtonemanager. extra_ringtone_title, "set ringtone"); // return activitystartactivityforresult (intent, buttonringtone) to the current activitystartactivityforresult (intent, buttonringtone) after the setting is completed;});/* Set alert. setonclicklistener (New button. onclicklistener () {@ Overridepublic void onclick (view arg0) {If (bfolder (stralarmfolder) {// set intent = new intent (ringtonemanager. action_ringtone_picker); // set the ringtone type and titleintent. putextra (ringtonemanager. extra_ringtone_type, ringtonemanager. type_alarm); intent. putextra (ringtonemanager. extra_ringtone_title, "Set alert tone"); // The activitystartactivityforresult (intent, buttonalarm) will be returned to the current activitystartactivityforresult (intent, buttonalarm);});/* Set alert tone */mbut Tification. setonclicklistener (New button. onclicklistener () {@ overridepublic void onclick (view arg0) {If (bfolder (strnotificationfolder) {// enable the system ringtone to set intent = new intent (ringtonemanager. action_ringtone_picker); // set the ringtone type and titleintent. putextra (ringtonemanager. extra_ringtone_type, ringtonemanager. type_notification); intent. putextra (ringtonemanager. extra_ringtone_title, "set notification ringtone"); // return to the current activi when the setting is complete. Tystartactivityforresult (intent, buttonnotification) ;}});}/* callback function after the ringtone is set */@ overrideprotected void onactivityresult (INT requestcode, int resultcode, intent data) {// todo auto-generated method stubif (resultcode! = Result_ OK) {return;} switch (requestcode) {Case buttonringtone: Try {// get the selected ringtone URI pickeduri = data. getparcelableextra (ringtonemanager. extra_ringtone_picked_uri); // set the selected ringtone to the default if (pickeduri! = NULL) {ringtonemanager. setactualdefaultringtoneuri (activity01.this, ringtonemanager. type_ringtone, pickeduri) ;}} catch (exception e) {} break; Case buttonalarm: Try {// get the selected ringtone URI pickeduri = data. getparcelableextra (ringtonemanager. extra_ringtone_picked_uri); // set the selected ringtone to the default if (pickeduri! = NULL) {ringtonemanager. setactualdefaultringtoneuri (activity01.this, ringtonemanager. type_alarm, pickeduri) ;}} catch (exception e) {} break; Case buttonnotification: Try {// get the selected ringtone URI pickeduri = data. getparcelableextra (ringtonemanager. extra_ringtone_picked_uri); // set the selected ringtone to the default if (pickeduri! = NULL) {ringtonemanager. setactualdefaultringtoneuri (activity01.this, ringtonemanager. type_notification, pickeduri) ;}} catch (exception e) {} break;} super. onactivityresult (requestcode, resultcode, data);} // check whether a specified folder exists. // if not, create private Boolean bfolder (string strfolder) {Boolean btmp = false; file F = new file (strfolder); If (! F. exists () {If (F. mkdirs () {btmp = true;} else {btmp = false ;}} else {btmp = true;} return btmp ;}}
Playing of ringtones:
  player = new MediaPlayer();      try{      player.setDataSource(context, pickUri);      }catch(Exception e){      e.printStackTrace();      }     final AudioManager  audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);     if(audioManager.getStreamVolume(AudioManager.STREAM_ALARM)!=0){         player.setAudioStreamType(AudioManager.STREAM_ALARM);         player.setLooping(true);         try {player.prepare();} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}         player.start();    }

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.