5

I need to set custom alarm tone in my App. Could anyone please just tell me how to set custom ringtone or Mp3 as an alarm ? Any kind of help will be appreciated.

Charles
  • 50,943
  • 13
  • 104
  • 142
Munazza
  • 361
  • 2
  • 6
  • 15
  • http://stackoverflow.com/questions/6464080/how-to-play-mp3-file-in-raw-folder-as-notification-sound-alert-in-android Here is another solution for you. Best, Shahzad – Shahzad Majeed Jun 17 '12 at 19:31
  • http://stackoverflow.com/questions/8314727/setting-a-custom-ringtone-produces-filenotfound-exception Here is one more answer. Shahzad Majeed. – Shahzad Majeed Jun 17 '12 at 19:33
  • thanks Shahzad :) ur links are helpful :) – Munazza Jun 22 '12 at 12:25

4 Answers4

3

Here is also a solution for this problem

setting audio file as Ringtone

2

You can use audio player to play your mp3.But here is a better alarm app which fulfills your requirements.

http://code.google.com/p/kraigsandroid/source/browse/#git%2Fandroid%2Falarmclock%2Fsrc%2Fcom%2Fangrydoughnuts%2Fandroid%2Falarmclock

0

in the Android docs look at the Status Bar Notifications page. In particular see the Adding a Sound section.

FoamyGuy
  • 46,603
  • 18
  • 125
  • 156
0

Try this

add any .mp3 file in raw folder place name of that file

 public void setAlarm() {
        File file = new File(Environment.getExternalStorageDirectory(),
                "/Your Directory Name");
        if (!file.exists()) {
            file.mkdirs();
        }

        String path = Environment.getExternalStorageDirectory()
                .getAbsolutePath() + "/Your Directory Name";

        File f = new File(path + "/", filename + ".mp3");

        Uri mUri = Uri.parse("android.resource://" + getContext().getPackageName() + "/raw/" + filename);
        ContentResolver mCr = getContext().getContentResolver();
        AssetFileDescriptor soundFile;
        try {
            soundFile = mCr.openAssetFileDescriptor(mUri, "r");
        } catch (FileNotFoundException e) {
            soundFile = null;
        }

        try {
            byte[] readData = new byte[1024];
            FileInputStream fis = soundFile.createInputStream();
            FileOutputStream fos = new FileOutputStream(f);
            int i = fis.read(readData);

            while (i != -1) {
                fos.write(readData, 0, i);
                i = fis.read(readData);
            }

            fos.close();
        } catch (IOException io) {
        }

        ContentValues values = new ContentValues();
        values.put(MediaStore.MediaColumns.DATA, f.getAbsolutePath());
        values.put(MediaStore.MediaColumns.TITLE, filename);
        values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
        values.put(MediaStore.MediaColumns.SIZE, f.length());
        values.put(MediaStore.Audio.Media.ARTIST, R.string.app_name);
        values.put(MediaStore.Audio.Media.IS_ALARM, true);






        Uri uri = MediaStore.Audio.Media.getContentUriForPath(f.getAbsolutePath());
        getContext().getContentResolver().delete(uri, MediaStore.MediaColumns.DATA + "=\"" + f.getAbsolutePath() + "\"", null);
        Uri newUri = mCr.insert(uri, values);

        try {
            RingtoneManager.setActualDefaultRingtoneUri(getContext(),
                    RingtoneManager.TYPE_ALARM, newUri);
            Settings.System.putString(mCr, Settings.System.ALARM_ALERT,
                    newUri.toString());
            Toast.makeText(getContext(), "Done", Toast.LENGTH_SHORT).show();

        } catch (Throwable t) {

        }
    }
Rahul
  • 219
  • 1
  • 10