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

发布于 2024-10-15 17:47:40 字数 3418 浏览 2 评论 0原文

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

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

我希望能够对音板进行编程,将声音设置为 android/java 中的铃声或通知。

谢谢。

我想我应该输入一些代码。 :) - 我知道这很简单,但它给我带来了很大的困难,我真的很想知道这一部分。菜单确实出现了,但它没有将其保存到 SD 卡文件中。我只是不知道 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;}

};

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

};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

辞别 2024-10-22 17:47:41

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

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.

青春如此纠结 2024-10-22 17:47:41

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文