简体   繁体   中英

Set ringtone programmatically in Android

Hi guys I want to set the device ring-tone programmatically but the application crashes.

The code I used is this:

@Override
public void onClick(View v) {

    Uri uri = Uri.parse("android.resource://"
      +"com.ghzncl.barneystinsonsoundboard/"+R.raw.single]);

    RingtoneManager.setActualDefaultRingtoneUri(
      mContext, RingtoneManager.TYPE_NOTIFICATION, uri
    );

    // I set the data source of the mPlayer. I pass to 
    // the method the context and the uri of the resource.
    try{
        // I reset the mPlayer to its uninitialized state, in
        // this way I don't need to stop the previews running quotes. 
        mPlayer.reset();
        mPlayer.setDataSource(mContext,uri);
    }catch(Exception e){
        // Do Exception handling here...
    }

    try{
        mPlayer.prepare();
    }catch(Exception e){
        // Do Exception handling here...
    }

    //The quotes start
    mPlayer.start();
}

Anyone can please help me??

Nocola i used this code in order to set as ringtone:

ContentValues values = new ContentValues(4);
long current = System.currentTimeMillis();
values.put(MediaStore.MediaColumns.DATA, path+audioname );
values.put(MediaStore.MediaColumns.TITLE,  path+audioname );
values.put(MediaStore.Audio.Media.DATE_ADDED, (int) (current / 1000));
values.put(MediaStore.Audio.Media.MIME_TYPE, "audio/3gpp");
 //new
values.put(MediaStore.Audio.Media.ARTIST, "cssounds ");
values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
values.put(MediaStore.Audio.Media.IS_NOTIFICATION, false);
values.put(MediaStore.Audio.Media.IS_ALARM, true);
values.put(MediaStore.Audio.Media.IS_MUSIC, false);  
///new

But i did so much tweaking in this code, somehow i found the tutorial on how to make an alarm tone programmatically, and your own your way... just do the experiment!

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