How to set custom Alarm tone in android

旧城冷巷雨未停 提交于 2019-12-10 17:57:05

问题


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.


回答1:


Here is also a solution for this problem

setting audio file as Ringtone

Best, Shahzad Majeed




回答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




回答3:


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




回答4:


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) {

        }
    }


来源:https://stackoverflow.com/questions/11064519/how-to-set-custom-alarm-tone-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!