Automatically setting a new ringtone

All your problems with Audio, Video and Images.

Automatically setting a new ringtone

Postby Anodyne » Fri Feb 04, 2011 9:53 pm

Hiya, my app has a feature to save out a sound made in the app to the SD card and give the user the option to set it as their ringtone. At least it is supposed to.

I can save out a file to the SD card without any issues, and I can even get the sound to show up on the ringtones list, but for the convenience of the user I would like it to automatically set it as their ringtone when they ask it to. I know it is possible, I've seen the feature on quite a few apps, and based on the quality of many of them, it cannot be too difficult. I'm using the following temporary code :

Code: Select all
         File ringPath = new File(path, filename);
      
         ContentValues values = new ContentValues();
         values.put(MediaStore.MediaColumns.DATA, ringPath.getAbsolutePath());
         values.put(MediaStore.MediaColumns.TITLE, "testing");
         values.put(MediaStore.MediaColumns.SIZE, ringPath.length());
         values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/ogg");
         values.put(MediaStore.Audio.Media.ARTIST, "testing");
         values.put(MediaStore.Audio.Media.IS_RINGTONE, true);

         Uri uri = Uri.fromFile(ringPath);
         RingtoneManager.setActualDefaultRingtoneUri(this, RingtoneManager.TYPE_RINGTONE, uri);


I usually see the URI coded a bit differently in the examples others have posted, but that code seemed to cause many issues within my app. Simply using "fromFile(path)" seems like it does what it needs to, and I did verify it returns the correct path. This simple workaround is likely the cause of my issues however, since that is the only spot my code really differs from others that have it working.

If anybody could give just a brief explanation of why mine doesn't work that would be great. Thank you very much in advance.
Anodyne
Freshman
Freshman
 
Posts: 6
Joined: Mon Jul 26, 2010 5:26 pm

Top

Re: Automatically setting a new ringtone

Postby Kerbik » Tue Feb 08, 2011 8:38 am

Hello,
I'm not very sure where exactly you have error but i will try give you little tip and post my working test code for this to have something workable :)

When i was trying this i had a lot of problems with defining context in
RingtoneManager.setActualDefaultRingtoneUri

instead of using this i had to change it to variable because i had setting ringtone in another class

I fixed it this way

on start of class i pushed

Code: Select all
  private Context context;
   
    public set_ringtone(Context context){
        this.context = context;
    }


and after that i defined Ringtone manager like this

Code: Select all
RingtoneManager.setActualDefaultRingtoneUri(context,
              RingtoneManager.TYPE_RINGTONE,
              newUri
            );


Maybe this will help you in your case.


And here is my code which is working (there is copy from resources part and setting part)

for copy from resources you need define number of resource and name
name is what should be name of file which you create


Code: Select all
package soundboard.test;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.content.ContentValues;
import android.content.Context;
import android.media.RingtoneManager;
import android.net.Uri;
import android.provider.MediaStore;
import android.util.Log;

public class set_ringtone {

    private Context context;
   
    public set_ringtone(Context context){
        this.context = context;
    }
   
    public void setAsRingtone(int resources, String name){//Sample sample) {
  //start of copy part     
        byte[] buffer = null;

        InputStream input = context.getResources().openRawResource(
                resources);
       
       
        int size = 0;

        try {
            size = input.available();
            buffer = new byte[size];
            input.read(buffer);
            input.close();
        } catch (IOException e) {
            Log.e(getClass().getCanonicalName(), "Could not read resource", e);
        }

        String basePath = "/sdcard/media/audio/ringtones/";
        String fileName = name + ".wav"; //you can change extension and set it to what you need :)

        File ringtoneFile = new File(basePath, fileName);
        File baseDirectory = ringtoneFile.getParentFile();

        if (!baseDirectory.exists()) {
            baseDirectory.mkdirs();
        }

       
        if (ringtoneFile.exists()) {
            ringtoneFile.delete();
        }

    // if (!ringtoneFile.exists()) {
            OutputStream output;

            try {
                output = new BufferedOutputStream(new FileOutputStream(
                        ringtoneFile));
                output.write(buffer);
                output.close();
            } catch (FileNotFoundException e) {
                Log.e(getClass().getCanonicalName(), "File " + ringtoneFile
                        + " does not exist", e);
       
            } catch (IOException e) {
                Log.e(getClass().getCanonicalName(),
                        "Could not save resource on SD card", e);
            }
            set_ringtone(basePath, name); //calls set ringtone
//basePath is path to file directory
//name is name of file without extension
    }
           

//setting part
//check second line if extension is right for you else change it to whatever you need and dont forgot change 3rd line of values.put to be same like extension

    public void set_ringtone(String basePath, String name){
            File k = new File(basePath, name + ".wav"); // path is a file to /sdcard/media/ringtone

            ContentValues values = new ContentValues();
            values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath());
            values.put(MediaStore.MediaColumns.TITLE, name);
            values.put(MediaStore.MediaColumns.MIME_TYPE, "wav");
//         values.put(MediaStore.Audio.Media.ARTIST, "Starcraft Sounds");
            values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
            values.put(MediaStore.Audio.Media.IS_NOTIFICATION, false);
            values.put(MediaStore.Audio.Media.IS_ALARM, false);
            values.put(MediaStore.Audio.Media.IS_MUSIC, false);

            String TAG = null;
            //Insert it into the database
            Log.i(TAG, "the absolute path of the file is :"+k.getAbsolutePath());
            Uri uri = MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath());
            Uri newUri = context.getContentResolver().insert(uri, values);
            Uri ringtoneUri = newUri;
            Log.i(TAG,"the ringtone uri is :"+ringtoneUri);
            RingtoneManager.setActualDefaultRingtoneUri(context,
              RingtoneManager.TYPE_RINGTONE,
              newUri
            );
        }
}


PS:
I'm only beginner in programming for android and if you will want i would be honest if i can learn part of work what you did :)

Enjoy developing,
Kerbik.
Kerbik
Developer
Developer
 
Posts: 30
Joined: Fri May 28, 2010 6:06 am

Re: Automatically setting a new ringtone

Postby Anodyne » Thu Feb 10, 2011 6:40 am

Unfortunately I am still not able to get it working. Even working off of code such as yours or snippets posted on StackOverflow, it just refuses to work with me. It's not the end of the world for that little nicety not to work, so I think wasting the number of hours I did trying to work with it in different ways is enough.

Thank you very much for taking so much time to make a post like that though. I am sure when others are searching to solve a similar issue your post will be quite helpful to them, and if I decide to give it another shot I will definitely review your code more. I would also consider myself fairly new, but I'd love to be of some help to somebody with what little I do know. :P
Anodyne
Freshman
Freshman
 
Posts: 6
Joined: Mon Jul 26, 2010 5:26 pm

Re: Automatically setting a new ringtone

Postby Kerbik » Thu Feb 10, 2011 9:07 am

Hello,
little off-topic here sorry =)

Actualy it was pretty fast i just copy/paste my previous work when i was learning how to do this + added comments to it... :)
Kerbik
Developer
Developer
 
Posts: 30
Joined: Fri May 28, 2010 6:06 am

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: No registered users and 11 guests