Android:将声音保存为铃声和通知

发布于 2024-09-27 04:00:25 字数 1051 浏览 0 评论 0原文

我正在保存应用程序中的声音以用作铃声或通知声音。这是我的代码的一部分,取自 页面:

ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath());
values.put(MediaStore.MediaColumns.TITLE, soundName);
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/ogg");
values.put(MediaStore.Audio.Media.ARTIST, "artist");
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);

Uri uri = MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath());
this.getContentResolver().insert(uri, values);

我的理解是,声音将与铃声、通知声音和闹钟一起保存,因为标志都设置为“true” ”。至少在模拟器上这确实有效,但在实际设备上,声音仅显示在铃声列表中,我不知道为什么。

编辑:我试图进一步调查:删除带有“IS_RINGTONE”的行不会改变任何内容(如果一次只能使用一个标志),声音不会显示在带有通知的列表中 -声音。

任何帮助表示赞赏。

亲切的问候, Select0r

I'm saving a sound from my app to be used as a ringtone or a notification sound. Here's part of my code, taken from this page:

ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath());
values.put(MediaStore.MediaColumns.TITLE, soundName);
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/ogg");
values.put(MediaStore.Audio.Media.ARTIST, "artist");
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);

Uri uri = MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath());
this.getContentResolver().insert(uri, values);

My understanding is that the sound will be saved as well as a ringtone, a notification sound and an alarm, as the flags are all set to "true". At least on the emulator this does work but on the actual device, the sound only shows up in the ringtone list, and I have no idea why.

EDIT: I've tried to investigate further: removing the line with "IS_RINGTONE" won't change anything (in case only one flag can be used at a time), the sound doesn't show up in the list with the notification-sounds.

Any help is appreciated.

Kind regards, Select0r

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

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

发布评论

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

评论(3

等风来 2024-10-04 04:00:25

我尝试了解决这个问题的路径,这是我迄今为止想到的解决方案:

在模拟器上,路径并不重要,声音将出现在铃声和通知列表中。

在手机上,如果文件保存到 /media/audio/ringtones/media/audio/ 但不是作为通知,声音将显示在铃声列表中.
如果我使用路径 /media/audio/notifications 声音最终会出现在通知列表中(!),但不再出现在铃声中。

所以看来我必须保存声音两次才能将其放入两个列表中? (但是为什么保存一次就可以在模拟器上工作了?)

如果有另一种方法可以将声音放入两个列表(甚至三个列表,也有警报音,我只是懒得玩它们),只需保存他们一次,请告诉我。 (现在我的解决方法是一个对话框,让用户选择是将声音保存为铃声还是通知。)

I played around with the path to get this solved and here's the solution I came up with so far:

On the emulator the path doesn't matter, the sound will appear in the ringtone as well as the notification-list.

On the phone, the sound will show up the the ringtone list if the file is saved to /media/audio/ringtones OR /media/audio/ but NOT as a notification.
If I use the path /media/audio/notifications the sound finally appears in the notification-list (!) but NOT in the ringtones any more.

So it seems I have to save the sound twice to get it into both list? (But why does saving it once work on the emulator?)

If there's another way of getting sounds into both lists (or even three lists, there are alarm tones as well, I just didn't bother playing around with them) with only saving them once, please let me know. (Right now my workaround is a dialog to let the user choose whether to save the sound as a ringtone or a notification.)

一指流沙 2024-10-04 04:00:25

如果您尝试从 MediaStore.Audio.Media 更改为 AudioColumns 有帮助吗?像这样:

values.put(AudioColumns.ARTIST, "artist");
values.put(AudioColumns.IS_RINGTONE, true);
values.put(AudioColumns.IS_NOTIFICATION, true);
values.put(AudioColumns.IS_ALARM, true);
values.put(AudioColumns.IS_MUSIC, false);

该参考并没有说 MediaStore.Audio.Media 已被弃用,但我确实认为 AudioColumns 现在已被使用。我在我的应用程序中使用它,它似乎有效。

Does it help if you try to change from MediaStore.Audio.Media to AudioColumns? Like this:

values.put(AudioColumns.ARTIST, "artist");
values.put(AudioColumns.IS_RINGTONE, true);
values.put(AudioColumns.IS_NOTIFICATION, true);
values.put(AudioColumns.IS_ALARM, true);
values.put(AudioColumns.IS_MUSIC, false);

The reference does not say MediaStore.Audio.Media is deprecated, but I do think AudioColumns is used now. I'm using it in my app and it seems to work.

も星光 2024-10-04 04:00:25

你必须研究Android中的媒体播放器API
即android.media.MediaPlayer;
android.media.MediaPlayer.OnCompletionListener 。这里是:
http://developer.android.com/reference/android/media/package-summary ....
这就是您在触发点击侦听器时播放声音的方式:

     private OnClickListener btnDontBeStupidListener = new 
OnClickListener() 
            { 
                public void onClick(View v) 
                { 
                  //Toast.makeText(getBaseContext(), "Don't Be Stupid audio file 
is being played", Toast.LENGTH_SHORT).show(); 
                  final MediaPlayer mp = MediaPlayer.create(iMEvil.this, 
R.raw.stupid); 
                  //mp.start(); 
                  try { 
                          mp.start(); 
                          //mp.release(); 
                  }catch(NullPointerException e){ 
                        Log.v("MP error", e.toString()); 
                  } 
                  mp.setOnCompletionListener(new OnCompletionListener(){ 
                      // @Override 
                      public void onCompletion(MediaPlayer arg0) { 
                         mp.release(); 
                      } 
                 }); 
                } 
            }; 

另外,您是否尝试过在播放时录制声音?尝试一下,看看是否有效。

You have to look into the media player API in Android
i.e.android.media.MediaPlayer;
android.media.MediaPlayer.OnCompletionListener . Here it is:
http://developer.android.com/reference/android/media/package-summary....
And that is how you play a sound on firing a click listener:

     private OnClickListener btnDontBeStupidListener = new 
OnClickListener() 
            { 
                public void onClick(View v) 
                { 
                  //Toast.makeText(getBaseContext(), "Don't Be Stupid audio file 
is being played", Toast.LENGTH_SHORT).show(); 
                  final MediaPlayer mp = MediaPlayer.create(iMEvil.this, 
R.raw.stupid); 
                  //mp.start(); 
                  try { 
                          mp.start(); 
                          //mp.release(); 
                  }catch(NullPointerException e){ 
                        Log.v("MP error", e.toString()); 
                  } 
                  mp.setOnCompletionListener(new OnCompletionListener(){ 
                      // @Override 
                      public void onCompletion(MediaPlayer arg0) { 
                         mp.release(); 
                      } 
                 }); 
                } 
            }; 

Also, have you tried recording the sound while playing it? Try it and see if it works.

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