android-Ringtones settings and playback

Source: Internet
Author: User

in the Android system, different ringtones are stored in the ringtone path:/system/media/audio/Ringtones Call ringtone/system/media/audio/Notifications SMS Notification ringtones/system/media/audio/alarms settings for alarm ringtones:ImportJava.io.File; Importandroid.app.Activity; Importandroid.content.Intent; ImportAndroid.media.RingtoneManager; ImportAndroid.net.Uri; ImportAndroid.os.Bundle; ImportAndroid.view.View; ImportAndroid.widget.Button;  Public classActivity01extendsActivity {/*3 x Buttons*/          PrivateButton Mbuttonringtone; PrivateButton Mbuttonalarm; PrivateButton mbuttonnotification; /*Types of Customizations*/           Public Static Final intButtonringtone = 0;  Public Static Final intButtonalarm = 1;  Public Static Final intButtonnotification = 2; /*Ringtone Folder*/          PrivateString Strringtonefolder = "/sdcard/music/ringtones"; PrivateString Stralarmfolder = "/sdcard/music/alarms"; PrivateString Strnotificationfolder = "/sdcard/music/notifications"; /**Called when the activity is first created.*/@Override Public voidonCreate (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 up incoming call tones*/Mbuttonringtone.setonclicklistener (NewButton.onclicklistener () {@Override Public voidOnClick (View arg0) {if(Bfolder (Strringtonefolder)) {//turn on system ringtone settingsIntent Intent =NewIntent (Ringtonemanager.action_ringtone_picker); //type is call ringtoneIntent.putextra (Ringtonemanager.extra_ringtone_type, Ringtonemanager.type_ringtone); //set the title of the displayIntent.putextra (Ringtonemanager.extra_ringtone_title, "Set caller ringtone"); //returns to the current activity when the setting is completeStartactivityforresult (Intent, buttonringtone);              }                  }              }); /*Set alarm tones*/Mbuttonalarm.setonclicklistener (NewButton.onclicklistener () {@Override Public voidOnClick (View arg0) {if(Bfolder (Stralarmfolder)) {//turn on system ringtone settingsIntent Intent =NewIntent (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 the alarm ringtone"); //returns to the current activity when the setting is completeStartactivityforresult (Intent, buttonalarm);              }                  }              }); /*Set notification ringtone*/Mbuttonnotification.setonclicklistener (NewButton.onclicklistener () {@Override Public voidOnClick (View arg0) {if(Bfolder (Strnotificationfolder)) {//turn on system ringtone settingsIntent Intent =NewIntent (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"); //returns to the current activity when the setting is completeStartactivityforresult (Intent, buttonnotification);          }                  }              }); }          /*callback function after setting the ringtone*/@Overrideprotected voidOnactivityresult (intRequestcode,intResultCode, Intent data) {              //TODO auto-generated Method Stub            if(ResultCode! =RESULT_OK) {                  return; }              Switch(requestcode) { CaseButtonringtone:Try                      {                          //get our choice of ringtonesUri Pickeduri =Data.getparcelableextra (Ringtonemanager.extra_ringtone_picked_uri); //Set the ringtone we choose to be the default                        if(Pickeduri! =NULL) {Ringtonemanager.setactualdefaultringtoneuri (Activity01. This, Ringtonemanager.type_ringtone, Pickeduri); }                      }                      Catch(Exception e) {} Break;  CaseButtonalarm:Try                      {                          //get our choice of ringtonesUri Pickeduri =Data.getparcelableextra (Ringtonemanager.extra_ringtone_picked_uri); //Set the ringtone we choose to be the default                        if(Pickeduri! =NULL) {Ringtonemanager.setactualdefaultringtoneuri (Activity01. This, Ringtonemanager.type_alarm, Pickeduri); }                      }                      Catch(Exception e) {} Break;  Casebuttonnotification:Try                      {                          //get our choice of ringtonesUri Pickeduri =Data.getparcelableextra (Ringtonemanager.extra_ringtone_picked_uri); //Set the ringtone we choose to be the default                        if(Pickeduri! =NULL) {Ringtonemanager.setactualdefaultringtoneuri (Activity01. This, Ringtonemanager.type_notification, Pickeduri); }                      }                      Catch(Exception e) {} Break; }              Super. Onactivityresult (Requestcode, ResultCode, data); }          //detects whether the specified folder exists//If it does not exist, create        Private Booleanbfolder (String strfolder) {BooleanBtmp =false; File F=NewFile (strfolder); if(!f.exists ()) {                  if(F.mkdirs ()) {btmp=true; }                  Else{btmp=false; }              }              Else{btmp=true; }              returnbtmp; }} Ringtone playback: Player=NewMediaPlayer (); Try{player.setdatasource (context, Pickuri); }Catch(Exception e) {e.printstacktrace (); }          FinalAudiomanager 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 (); }  

android-Ringtones settings and playback

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.