0

I wrote this simple Main.java and need to know how to use simple codes to set a soundpool or mediaplayer as a ringtone. Direct question is: set as ringtone by button "b2"

there are few sources in stackoverflow but I could n't understand any of them. thanks in advance

My codes:

public class Main extends Activity implements OnClickListener{

SoundPool sp;
int dicesound;
Button play, setAsRingtone;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    sp = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);
    dicesound = sp.load(this, R.raw.onedice, 1);
    play = (Button) findViewById(R.id.b1);
    setAsRingtone = (Button) findViewById(R.id.b2);
    play.setOnClickListener(this);
    setAsRingtone.setOnClickListener(this);
}
@Override
public void onClick(View v) {
    // TODO Auto-generated method stub

    switch(v.getId()){

    case R.id.b1:
        sp.play(dicesound, 1, 1, 0, 0, 1);
        break;

    case R.id.b2:
        // HOW TO SET "dicesound" SOUND AS A RINGTONE ???
        break;
    }
}

}

Civiato
  • 57
  • 8
  • check this link http://stackoverflow.com/questions/1271777/how-to-set-ringtone-in-android-from-my-activity – umerk44 Apr 24 '15 at 06:52
  • 1
    Please check this link. here it is explained quit nicely http://stackoverflow.com/questions/1271777/how-to-set-ringtone-in-android-from-my-activity – Vishwajit Palankar Apr 24 '15 at 06:54

2 Answers2

1

Please check below code , hope it helps you

Replace "com.example.sample" with your package name

    Uri m_path = Uri.parse("android.resource://com.example.sample/" + R.raw.onedice);
RingtoneManager.setActualDefaultRingtoneUri(activity.this,RingtoneManager.TYPE_RINGTONE, m_path);
0

First of All You need to copy the Your Sound File (Raw file to sd card)to SD Card ant then used following code to set as ringtone.:

String filepath ="/sdcard/myring.mp3";
File ringtoneFile = new File(filepath);



//To set a ringtone you have to add it to the database.

//otherwise it will not be set also gives no error also.

ContentValues content = new ContentValues();
content.put(<span id="IL_AD7" class="IL_AD">MediaStore</span>.MediaColumns.DATA,ringtoneFile.getAbsolutePath());
content.put(MediaStore.MediaColumns.TITLE, "test");

content.put(MediaStore.MediaColumns.SIZE, 215454);

content.put(MediaStore.MediaColumns.MIME_TYPE, "audio/*");

content.put(MediaStore.Audio.Media.ARTIST, "artist");

content.put(MediaStore.Audio.Media.DURATION, 230);

content.put(MediaStore.Audio.Media.IS_RINGTONE, true);

content.put(MediaStore.Audio.Media.IS_NOTIFICATION, <span id="IL_AD4" class="IL_AD">false</span>);

content.put(MediaStore.Audio.Media.IS_ALARM, false);

content.put(MediaStore.Audio.Media.IS_MUSIC, false);

Insert it into the database

Log.i(TAG, "the absolute path of the file is :"+ringtoneFile.getAbsolutePath());
Uri uri = MediaStore.Audio.Media.getContentUriForPath(
ringtoneFile.getAbsolutePath());

Uri newUri = context.getContentResolver().<span id="IL_AD5" class="IL_AD">insert</span>(uri, content);

ringtoneUri = newUri;

Log.i(TAG,"the ringtone uri is :"+ringtoneUri);

RingtoneManager.setActualDefaultRingtoneUri(context,
RingtoneManager.TYPE_RINGTONE,newUri);
Rajan Bhavsar
  • 1,977
  • 11
  • 25
  • I tried this: "Uri ringtoneUri = Uri.parse("/sdcard/media/audio/ringtones/onedice.mp3"); RingtoneManager.setActualDefaultRingtoneUri(Main.this, RingtoneManager.TYPE_RINGTONE, ringtoneUri); Ringtone rt = RingtoneManager.getRingtone(Main.this,ringtoneUri); rt.play(); – Civiato Apr 24 '15 at 10:48
  • But it just plays the current ringtone and it wont replace it with the new one. – Civiato Apr 24 '15 at 10:50
  • I think, the problem is with the address. – Civiato Apr 24 '15 at 11:32