Setting a MediaPlayer object as AndroidStudio Ringtone

0

I am in the development of an application which has 4 objects MediaPlayer loaded and are reproducible, very similar to a keypad

I want to be able to set them as a ringtone, notifications or message and I can not find anywhere to do it.

Thank you very much for your time :), excuse me if I do not formulate the question very well, I am quite new in stackoverflow.

The objects are loaded like this, I add them to a raw folder previously and then.

sonido1 = MediaPlayer.create(this,R.raw.SonidoRandom);
    
asked by WhySoBizarreCode 08.03.2018 в 20:11
source

1 answer

0

Well, after a few days of research and testing I finally get it. I leave the code ...

btn5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {


                if(rt1.isChecked() == true){ //aca comparo con dos radio buttons si quiero un ringtone o un sonido de notificacion

                    ways="/media/audio/ringtones/";

                }else if(rt2.isChecked() == true) {

                    ways="/media/audio/notifications/";

                }else{
                    ways="/media/audio/ringtones/";
                }



                pathi=Environment.getExternalStorageDirectory().getPath()+ ways; //aca tomo el directorio de sd mas el path de los ringtones o sonidos de notificaciones

                boolean exists = (new File(pathi)).exists();
                if (!exists) {
                    new File(pathi).mkdirs();
                } //si no existe el direct lo creo

                File newSoundFile = new File(pathi, "sound.mp3");//creo el archivo de sonido
                Uri mUri = Uri.parse("android.resource://com.harrypotter.harrysound/"+R.raw.sound); //lo cargo con mi mp3 en el raw
                ContentResolver mCr = getApplicationContext().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(newSoundFile);
                    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, newSoundFile.getAbsolutePath()); //indico la ruta del archivo
                values.put(MediaStore.MediaColumns.TITLE, "soundharrybtn");
                values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
                values.put(MediaStore.MediaColumns.SIZE, newSoundFile.length());
                values.put(MediaStore.Audio.Media.ARTIST, "HarrypotterMagia");


                if(rt1.isChecked() == true){
                    types=RingtoneManager.TYPE_RINGTONE;
                    values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
                    tipito="Ringtone";

                }if(rt2.isChecked() == true){

                    types=RingtoneManager.TYPE_NOTIFICATION;
                    values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
                    tipito="Sonido de notificacion";

                }if(rt1.isChecked() == true || rt2.isChecked() == true){

                    Uri uri = MediaStore.Audio.Media.getContentUriForPath(newSoundFile.getAbsolutePath());
                    Uri newUri = mCr.insert(uri, values);

                    try {
                        RingtoneManager.setActualDefaultRingtoneUri(tuneActivity.this, types, newUri);
                        Toast.makeText (tuneActivity.this,tipito+" puesto con exito",Toast.LENGTH_SHORT).show(); //con ringtone manager set, pongo el audio como predeterminado

                    } catch (Throwable t) {

                        Toast.makeText (tuneActivity.this,"No se pudo establecer el ringtone puesto con exito",Toast.LENGTH_SHORT).show();

                    }

                }else{

                    Toast.makeText (tuneActivity.this,"No selecciono Sonido de notificacion ni Ringtone ",Toast.LENGTH_SHORT).show();

                }



            }
        });
   ## Extras##

Everything is working but this block. It is still not clear to me if someone can explain it to me better. ;)

    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(newSoundFile);
                int i = fis.read(readData);

                while (i != -1) {
                    fos.write(readData, 0, i);
                    i = fis.read(readData);
                }

I hope the code works for them and helped them

    
answered by 11.03.2018 / 03:55
source