2

I want to set an mp3 file as a ringtone on button press. I have attached my code file with this question. As you can see in the image I want to set shiv shiv as ringtone once user click on setting 1 button and same for other settings button, each setting button will set the ringtone showing in its column.

i have added some code to set mp3 as ringtone but it doesn't work

package com.techjapreet.shivshankarkiringtone;

import android.content.ContentValues;
import android.content.Context;
import android.media.MediaPlayer;
import android.media.RingtoneManager;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.PopupMenu;
import android.widget.Toast;

import java.io.File;

public class ringtone_tab extends AppCompatActivity {



    Button clk1;
    Button clk2;
    Button clk3;
    Button clk4;
    Button clk5;
    Button clk6;



    private Button ring1;
    MediaPlayer mediaPlayer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ringtone_tab);

ring1 = findViewById(R.id.btn_setting1);
ring1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

        String filepath ="/sdcard/sample/"+currentName+"";
        System.out.println("/sdcard/sample/"+currentName+"");


        File ringtoneFile = new File(filepath);

        ContentValues content = new ContentValues();
        content.put(MediaStore.MediaColumns.DATA,ringtoneFile.getAbsolutePath());
        content.put(MediaStore.MediaColumns.TITLE, currentName);
        content.put(MediaStore.MediaColumns.SIZE, 215454);
        content.put(MediaStore.MediaColumns.MIME_TYPE, "audio/*");
        //  content.put(MediaStore.Audio.Media.ARTIST, "Madonna");
        content.put(MediaStore.Audio.Media.DURATION, 230);
        content.put(MediaStore.Audio.Media.IS_RINGTONE, true);
        content.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
        content.put(MediaStore.Audio.Media.IS_ALARM, true);
        content.put(MediaStore.Audio.Media.IS_MUSIC, true);

        String Ringtonepath= "content://media/internal/audio/media/297";
        Uri Ringtone1 = Uri.parse(filepath);
        //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());




        getContentResolver().delete(uri, MediaStore.MediaColumns.DATA + "=\"" + ringtoneFile.getAbsolutePath() + "\"",
                null);
        Uri newUri = getContentResolver().insert(uri, content);
        System.out.println("uri=="+uri);
        Log.i("TAG","the ringtone uri is :"+newUri);
        RingtoneManager.setActualDefaultRingtoneUri(
                getApplicationContext(), RingtoneManager.TYPE_RINGTONE,
                newUri);
    }
});




        clk6 = (Button) findViewById(R.id.btn_play6);

        clk5 = (Button) findViewById(R.id.btn_play5);

        clk4 = (Button) findViewById(R.id.btn_play4);

        clk3 = (Button) findViewById(R.id.btn_play3);

        clk2 = (Button) findViewById(R.id.btn_play2);

        clk1 = (Button) findViewById(R.id.btn_play1);

        mediaPlayer = new MediaPlayer();

    }



    public void setBtn_play6(View v)
    {
        stopPlayer();
        mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.shiv_vandana);
        mediaPlayer.start();

    }

    public void setBtn_play5(View v)
    {
        stopPlayer();
        mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.shiv_tandav_mantra);
        mediaPlayer.start();

    }



    public void setBtn_play4(View v)
    {
        stopPlayer();
        mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.shiv_shiv_om);
        mediaPlayer.start();

    }

    public void setBtn_play3(View v)
    {
        stopPlayer();
        mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.shiv_shiv);
        mediaPlayer.start();

    }



    public void setBtn_play2(View v)
    {
        stopPlayer();
        mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.shiv_aaradhna);
        mediaPlayer.start();


    }


    public void setBtn_play1(View v)
    {
        stopPlayer();
        mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.shiv_shankar);
        mediaPlayer.start();
    }

    private void stopPlayer(){
        if(mediaPlayer != null && mediaPlayer.isPlaying())
        {mediaPlayer.stop();
            mediaPlayer.reset();
            mediaPlayer.release();
        }
    }

}

here

  • 2
    Possible duplicate of [How to set ringtone in Android from my activity?](https://stackoverflow.com/questions/1271777/how-to-set-ringtone-in-android-from-my-activity) – Android Dec 31 '18 at 09:01

1 Answers1

0

Create method and put this inside it

 String filepath ="/sdcard/sample/"+currentName+"";
                            System.out.println("/sdcard/sample/"+currentName+"");


                            File ringtoneFile = new File(filepath);

                            ContentValues content = new ContentValues();
                            content.put(MediaStore.MediaColumns.DATA,ringtoneFile.getAbsolutePath());
                            content.put(MediaStore.MediaColumns.TITLE, currentName);
                            content.put(MediaStore.MediaColumns.SIZE, 215454);
                            content.put(MediaStore.MediaColumns.MIME_TYPE, "audio/*");
                            //  content.put(MediaStore.Audio.Media.ARTIST, "Madonna");
                            content.put(MediaStore.Audio.Media.DURATION, 230);
                            content.put(MediaStore.Audio.Media.IS_RINGTONE, true);
                            content.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
                            content.put(MediaStore.Audio.Media.IS_ALARM, true);
                            content.put(MediaStore.Audio.Media.IS_MUSIC, true);

                        String Ringtonepath= "content://media/internal/audio/media/297";
                            Uri Ringtone1 = Uri.parse(filepath);   
                            //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());




                            getContentResolver().delete(uri, MediaStore.MediaColumns.DATA + "=\"" + ringtoneFile.getAbsolutePath() + "\"",
                                      null);
                            Uri newUri = getContentResolver().insert(uri, content);
                            System.out.println("uri=="+uri);
                            Log.i("TAG","the ringtone uri is :"+newUri);
                               RingtoneManager.setActualDefaultRingtoneUri(
                                       getApplicationContext(), RingtoneManager.TYPE_RINGTONE,
                                       newUri);

and just call this method inside your button click

and if you want to let the user choose his own file from his device

add next line to get the filepath

  Intent intent = new Intent();
      intent.setAction(Intent.ACTION_GET_CONTENT);
      intent.setType("file/*");
      startActivity(intent);
Hossam Hassan
  • 795
  • 2
  • 13
  • 39