2

I'm doing my first app: I want to put a button in order to set a .mp3 as a ringtone, everythig works ok, Logcat an eclipse doens't show any kind of error, but when I make a call (in emulator and real mobile phone) it doesn't sound! also, when I go to "Music" app and try to play those ringtones (I find them in "recently added") it showsme this: "Sorry, the player does not support type of audio file".

What I'm doing wrong?

Here is my code:

 setringtone.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                        Toast msg = Toast.makeText(TonosSet.this, "Sound set as ringtone!", Toast.LENGTH_LONG);
                        msg.show();

                        File k = new File("/sdcard/media/ringtone", "song_file.mp3");
                        Uri mUri = Uri.parse("android.resource://com.app/"+R.raw.song_file);
                        ContentResolver mCr = getContentResolver();
                        AssetFileDescriptor soundFile;
                        try {
                            soundFile= mCr.openAssetFileDescriptor(mUri, "r");
                        } catch (FileNotFoundException e) {
                            soundFile=null;   
                        }

                        try {

                            InputStream ins = TonosSet.this .getResources().openRawResource (R.raw.song_file);
                                    byte[] buffer = new byte[ins.available()];
                                    ins.read(buffer);
                                    ins.close();
                                    String filename = Environment.getExternalStorageDirectory().toString()+File.separator+R.raw.song_file;
                                    FileOutputStream fos = new FileOutputStream(filename);
                                    fos.write(buffer);
                                    fos.close();
                        } catch (IOException io) {
                        }

                        ContentValues values = new ContentValues();  
                        values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath());  
                        values.put(MediaStore.MediaColumns.TITLE, "Name sound");  
                        values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");  
                        values.put(MediaStore.Audio.Media.ARTIST, "Artist");  
                        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  
                        Uri uri = MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath());

                        getContentResolver().delete(uri, MediaStore.MediaColumns.DATA + "=\"" + k.getAbsolutePath() + "\"", null);

                        Uri newUri = getContentResolver().insert(uri, values);

                        RingtoneManager.setActualDefaultRingtoneUri(
                            TonosSet.this,
                            RingtoneManager.TYPE_NOTIFICATION,
                            newUri);

            }
            });

Please, help me, I don't know what to do, I've been looking for a solution for about 5 hours but I can't =(

Jacobo
  • 87
  • 1
  • 1
  • 13
  • "when I go to "Music" app and try to play those ringtones (I find them in "recently added") it showsme this: "Sorry, the player does not support type of audio file"." Maybe you should try another music file ? Are you sure the format you are trying to read is compliant with the system ? If you cannot read it using the music player, it seems normal your code is not working. Pick a random mp3 on the web and use it instead. – Vincent B. Oct 08 '12 at 03:08
  • I'll try that, maybe if I chage the bitrate of my .mp3 files it could work. – Jacobo Oct 08 '12 at 03:15
  • Well, my app have also a "Play" button and when I press it the .mp3 file sound normally, thats why I don't think that my .mp3 files are corrupted or something like that. I don't know what to do =( – Jacobo Oct 08 '12 at 03:18

1 Answers1

0

Maybe you should have a look at the following post it seems it give a different version than yours (about isNotification and other parameters).

How to set ringtone in Android from my activity?

Community
  • 1
  • 1
Vincent B.
  • 524
  • 3
  • 15