简体   繁体   English

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

[英]Android: save sound as ringtone and notification

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. 编辑:我已经尝试进一步调查:删除带有“IS_RINGTONE”的行不会改变任何东西(如果一次只能使用一个标志),声音不会显示在列表中,并带有通知 - 声音。

Any help is appreciated. 任何帮助表示赞赏。

Kind regards, Select0r 亲切的问候,SelectOr

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. 在手机上,如果文件保存到/media/audio/ringtones/media/audio/但不作为通知,声音将显示铃声列表。
If I use the path /media/audio/notifications the sound finally appears in the notification-list (!) but NOT in the ringtones any more. 如果我使用路径/media/audio/notifications ,声音最终会出现在通知列表(!)中,但不再出现在铃声中。

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.) (现在我的解决方法是一个对话框,让用户选择是将声音保存为铃声还是通知。)

Does it help if you try to change from MediaStore.Audio.Media to AudioColumns? 如果您尝试从MediaStore.Audio.Media更改为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. 该引用并未说MediaStore.Audio.Media已被弃用,但我认为现在使用的是AudioColumns。 I'm using it in my app and it seems to work. 我在我的应用程序中使用它似乎工作。

You have to look into the media player API in Android ieandroid.media.MediaPlayer; 你必须在Android ieandroid.media.MediaPlayer中查看媒体播放器API; android.media.MediaPlayer.OnCompletionListener . 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: 这是: 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(); 
                      } 
                 }); 
                } 
            }; 

Also, have you tried recording the sound while playing it? 此外,您是否尝试过播放时录制声音? Try it and see if it works. 试一试,看看它是否有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM