How to change user's ringtone?

All your problems with Audio, Video and Images.

How to change user's ringtone?

Postby barodapride » Thu Dec 02, 2010 12:13 am

Hi all, is there an easy way to change the user's ringtone to a sound file that is included in the res/raw directory of my app? Seems like it should be pretty simple. Anybody know?
barodapride
Junior Developer
Junior Developer
 
Posts: 23
Joined: Sat Sep 25, 2010 4:43 am

Top

Re: How to change user's ringtone?

Postby blundell » Fri Dec 03, 2010 2:59 pm

You cannot change the ringtone, but you can add your ringtone to the list of selectable ringtones on the phone, is this what you want?
User avatar
blundell
Master Developer
Master Developer
 
Posts: 1610
Joined: Tue Nov 18, 2008 12:58 pm
Location: UK

Re: How to change user's ringtone?

Postby jonbonazza » Fri Dec 03, 2010 3:50 pm

Blundell, you should be able to change the ringtone somehow, as well as the notification sound. The reason I say this is that most of the soundboards that clutter the market have both of these features.
jonbonazza
Master Developer
Master Developer
 
Posts: 665
Joined: Thu Jul 15, 2010 2:58 pm
Location: WV USA

Re: How to change user's ringtone?

Postby blundell » Fri Dec 03, 2010 3:57 pm

You may be able to actually, I've written a soundboard myself :-) but my SD card is corrupt at the moment so I can't test it. I thought it just added it to the list, but it would make sense that it could set it as well.

Here's the code for saving a sound out of your raw/res folder as a ringtone or notification tone :-). Need to check to see if it set's it at the same time

Code: Select all
protected void saveSound(int soundId, String name, boolean asRingtone) {
      byte[] buffer=null; 
       InputStream fIn = getBaseContext().getResources().openRawResource(soundId); 
       int size=0; 
       
       try { 
        size = fIn.available(); 
        buffer = new byte[size]; 
        fIn.read(buffer); 
        fIn.close(); 
       } catch (IOException e) { 
          Toast.makeText(this, "1 Error reading from phone memory.", Toast.LENGTH_LONG).show();
          return;
       } 
      
       String sdDir = Environment.getExternalStorageDirectory().toString();
       String path = sdDir;
       if(asRingtone){
          path += "/media/audio/ringtones/";
       } else {
          path += "/media/audio/notifications/";
       }
       String filename=name+".ogg"; 
       
       boolean exists = (new File(path)).exists(); 
       if (!exists){
          new File(path).mkdirs();
       } 
       
       FileOutputStream save; 
       try { 
        save = new FileOutputStream(path+filename);
        save.write(buffer); 
        save.flush(); 
        save.close(); 
       } catch (FileNotFoundException e) {
          Toast.makeText(this, "File not found error.", Toast.LENGTH_LONG).show();
          return;
       } catch (IOException e) { 
          Toast.makeText(this, "2 Error writing to phone memory.", Toast.LENGTH_LONG).show();
          return;
       }     
       
       sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://"+path+filename))); 
       
       File k = new File(path, filename); 
       
       ContentValues values = new ContentValues(); 
       values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath()); 
       values.put(MediaStore.MediaColumns.TITLE, name); 
       values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/ogg"); 
       values.put(MediaStore.Audio.Media.ARTIST, "FPSdoug "); 
       values.put(MediaStore.Audio.Media.IS_RINGTONE, true); 
       values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true); 
       values.put(MediaStore.Audio.Media.IS_ALARM, true); 
       values.put(MediaStore.Audio.Media.IS_MUSIC, false); 
       
       //Insert it into the database 
       this.getContentResolver().insert(MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath()), values); 
       
       // done message
       if(asRingtone){
          Toast.makeText(this, "Ringtone saved", Toast.LENGTH_LONG).show();
       } else {
          Toast.makeText(this, "Notification saved", Toast.LENGTH_LONG).show();
       }
   }
User avatar
blundell
Master Developer
Master Developer
 
Posts: 1610
Joined: Tue Nov 18, 2008 12:58 pm
Location: UK

Re: How to change user's ringtone?

Postby asylumstudios » Mon Mar 21, 2011 7:46 pm

I am completely new to programming and to the creation of apps for android. Where would I place this file in my soundboard app to enable this feature. I feel as though I have tried every file and everyone just turns up a string of errors. I have looked at several tutorials and even found many source code examples but none of them address the issue of how to set a sound as a ringtone or notification from a soundboard. I don't expect to be all that hard since nearly every soundboard app available seems to hav this functionality. Any help would be greatly appreciated on this matter.
asylumstudios
Freshman
Freshman
 
Posts: 2
Joined: Wed Mar 16, 2011 6:00 pm
Location: Redding, CA

Re: How to change user's ringtone?

Postby blundell » Mon Mar 21, 2011 8:32 pm

You need to some more Java programming if you can't figure out simple OO design.

You would put the code in it's own method and then call this method from the on click of the button. :-)
User avatar
blundell
Master Developer
Master Developer
 
Posts: 1610
Joined: Tue Nov 18, 2008 12:58 pm
Location: UK

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: No registered users and 26 guests