简体   繁体   中英

Set ringtone from a path in android

I had a path of song, example:

/sdcard/music/song/song.mp3

so I want to implement a button click that this song will be set ringtone .Thank you all

Get the path of your file to set as ringtone

String filePath= Environment.getExternalStorageDirectory().getAbsolutePath() + "/YourFolder/Ringtones/myRingTone.mp3";

    File ring = new File("filePath");

Set it as ringtone by using:

 Uri path = MediaStore.Audio.Media.getContentUriForPath(ring.getAbsolutePath());
 RingtoneManager.setActualDefaultRingtoneUri(getApplicationContext(), RingtoneManager.TYPE_RINGTONE,path)

Note: Add this permission in manifest:

<uses-permission android:name="android.permission.WRITE_SETTINGS" ></uses-permission>

first you need to get file with getassert functions , we will call him filesong here MediaPlayer mPlayer = MediaPlayer.create(mainactivity.this, filesong); mPlayer.start(); MediaPlayer mPlayer = MediaPlayer.create(mainactivity.this, filesong); mPlayer.start();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM