开发者

set sound as ringtone or notification in android app

开发者 https://www.devze.com 2023-02-09 04:56 出处:网络
since, im trying to learn the code. I decided to make one of those soundboards. Since, i like having them on my phone. I\'ve been googling for he source codes of these.I\'ve found one main source code

since, im trying to learn the code. I decided to make one of those soundboards. Since, i like having them on my phone. I've been googling for he source codes of these. I've found one main source code of a soundboard project. And, it seems this is really the main one out there that everyone else talks about. Then, I wanted to learn how to set a sound on the soundboard as a ringtone, and notification. Ive been googling for hours and running into the same codes that dont work for me. I can't figure out the codes to set a sound as a ringtone, and or anything else. I've even tried from scratch with no luck.

I can't find anything else on this topic. please help me, and im all googled out just to find the same information.

i want to be able to program a soundboard to set a sound as a ringtone or notification in android/java.

thanks.

i guess i should put in some code i suppose. :) - i know this is simple, but its giving me such a hard time, and id really like to know this part. the menu does come up, but its not saving it to the sdcard file. i just have no clue whats happening here in java/android. im a php guy, ty for all help

            Button btn = (Button) findViewById(R.id.sound1);
            registerForContextMenu(btn);
        }

        @Override
        public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
            menu.setHeaderTitle("Set as...");
            menu.add(0, v.getId(), 0, "Ringtone");
            menu.add(0, v.getId(), 0, "Notification");
        }

        @Override
        public boolean onContextItemSelected(MenuItem item) {
            if(item.getTitle()=="Ringtone"){function1(item.getItemId());}
            else if(item.getTitle()=="Notification"){function2(item.getItemId());}
            else {return false;}
        return true;
        }

        public void function1(int id){
            Toast.makeText(this, "Ringtone Saved", Toast.LENGTH_SHORT).show();
        }
        public void function2(int id){
            Toast.makeText(this, "Notification Saved", Toast.LENGTH_SHORT).show();
        }




    boolean saveas(int sound1){
         byte[] buffer=null;
         InputStream fIn = getBaseContext().getResources().openRawResource(sound1);
         int size=36462;

         try {
          size = fIn.available();
          buffer = new byte[size];
          fIn.read(buffer);
          fIn.close();
         } catch (IOException e) {
          // TODO Au开发者_StackOverflow中文版to-generated catch block
          return false;
         }

         String path="/sdcard/media/audio/ringtones/";
         String filename="sound1.ogg";

         boolean exists = (new File(path)).exists();
         if (!exists){new File(path).mkdirs();}

         FileOutputStream save;
         try {
          save = new FileOutputStream(path+filename);
          save.write(buffer);
          save.flush();
          save.close();
         } catch (FileNotFoundException e) {
          // TODO Auto-generated catch block
          return false;
         } catch (IOException e) {
          // TODO Auto-generated catch block
          return false;
         }    

         sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://"+path+filename)));

         File k = new File(path, filename);

         ContentValues values = new ContentValues();
         values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath());
         values.put(MediaStore.MediaColumns.TITLE, "sound1");
         values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/ogg");
         values.put(MediaStore.Audio.Media.ARTIST, "we");
         values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
         values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
         values.put(MediaStore.Audio.Media.IS_ALARM, true);
         values.put(MediaStore.Audio.Media.IS_MUSIC, false);

         //Insert it into the database
         this.getContentResolver().insert(MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath()), values);

         return false;}

};


try converting your sound file from .ogg to .mp3. The media player isn't able to play .ogg files from my experience. If you fix that it should work.


Might be something to do with your permissions (you may need data storage permissions).

0

精彩评论

暂无评论...
验证码 换一张
取 消