在Android应用程序中将声音设置为铃声或通知

时间:2011-02-05 02:11:25

标签: android notifications click long-integer ringtone

因为,我试图学习代码。我决定制作其中一个音板。因为,我喜欢在手机上使用它们。我一直在谷歌搜索他们的源代码。我找到了一个音板项目的主要源代码。而且,这似乎是其他人谈论的主要内容。然后,我想学习如何在音板上设置声音作为铃声和通知。我已经谷歌搜索了几个小时,并遇到了对我不起作用的相同代码。我无法弄清楚将声音设置为铃声和其他任何东西的代码。我甚至从头开始尝试没有运气。

我在这个主题上找不到任何其他内容。请帮助我,我全都谷歌搜索只是为了找到相同的信息。

我希望能够编写音板以将声音设置为android / java中的铃声或通知。

感谢。

我猜我应该输入一些代码。 :) - 我知道这很简单,但它让我很难过,而且我很想知道这一部分。菜单确实出现了,但它没有将其保存到sdcard文件中。我只是不知道在java / android中发生了什么。我是一个php的家伙,所有帮助的人

            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 Auto-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;}

};

2 个答案:

答案 0 :(得分:2)

尝试将声音文件从.ogg转换为.mp3。媒体播放器无法根据我的经验播放.ogg文件。如果你修复它应该工作。

答案 1 :(得分:0)

可能与您的权限有关(您可能需要数据存储权限)。