Android铃声以编程方式设置? [英] Android ringtone set programmatically?

查看:229
本文介绍了Android铃声以编程方式设置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在录制语音后为我的应用程序实现设置铃声.铃声只能正确设置一次,它将被设置为铃声,而再次设置它不能正常工作,这里我添加了我的代码:

I want to implement set ringtone to my application after recording voice.Ringtone will set correctly only one time it will be set as ringtone while set again its not working properly here i have added my code:

String filepath ="/sdcard/sample/"+currentName+"";
                            System.out.println("/sdcard/sample/"+currentName+"");


                            File ringtoneFile = new File(filepath);

                            ContentValues content = new ContentValues();
                            content.put(MediaStore.MediaColumns.DATA,ringtoneFile.getAbsolutePath());
                            content.put(MediaStore.MediaColumns.TITLE, currentName);
                            content.put(MediaStore.MediaColumns.SIZE, 215454);
                            content.put(MediaStore.MediaColumns.MIME_TYPE, "audio/*");
                            //  content.put(MediaStore.Audio.Media.ARTIST, "Madonna");
                            content.put(MediaStore.Audio.Media.DURATION, 230);
                            content.put(MediaStore.Audio.Media.IS_RINGTONE, true);
                            content.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
                            content.put(MediaStore.Audio.Media.IS_ALARM, true);
                            content.put(MediaStore.Audio.Media.IS_MUSIC, true);

                        String Ringtonepath= "content://media/internal/audio/media/297";
                            Uri Ringtone1 = Uri.parse(Ringtonepath);   
                            //Insert it into the database
                            Log.i("TAG", "the absolute path of the file is :"+
                                    ringtoneFile.getAbsolutePath());
                            Uri uri = MediaStore.Audio.Media.getContentUriForPath(
                                    ringtoneFile.getAbsolutePath());
                            Uri newUri = getContentResolver().insert(uri, content);
                            System.out.println("uri=="+uri);

                            Log.i("TAG","the ringtone uri is :"+newUri);
                            //   getContentResolver().delete(uri, MediaStore.MediaColumns.DATA + "=\"" + ringtoneFile.getAbsolutePath() + "\"",
                            //           null);

                               RingtoneManager.setActualDefaultRingtoneUri(
                                       getApplicationContext(), RingtoneManager.TYPE_RINGTONE,
                                       newUri);

我的错误是::

新铃声:

TAG    the ringtone uri is :content://media/internal/audio/media/297

现有铃声:

TAG    the ringtone uri is :null

推荐答案

重新排列代码行.. 据我了解,您可以在删除旧铃声之前插入新铃声.只需将上面的代码替换为此.

Rearrange the code lines .. From my understanding you insert new ringtone before deleting the old one.Just replace the above code with this.

 String filepath ="/sdcard/sample/"+currentName+"";
                            System.out.println("/sdcard/sample/"+currentName+"");


                            File ringtoneFile = new File(filepath);

                            ContentValues content = new ContentValues();
                            content.put(MediaStore.MediaColumns.DATA,ringtoneFile.getAbsolutePath());
                            content.put(MediaStore.MediaColumns.TITLE, currentName);
                            content.put(MediaStore.MediaColumns.SIZE, 215454);
                            content.put(MediaStore.MediaColumns.MIME_TYPE, "audio/*");
                            //  content.put(MediaStore.Audio.Media.ARTIST, "Madonna");
                            content.put(MediaStore.Audio.Media.DURATION, 230);
                            content.put(MediaStore.Audio.Media.IS_RINGTONE, true);
                            content.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
                            content.put(MediaStore.Audio.Media.IS_ALARM, true);
                            content.put(MediaStore.Audio.Media.IS_MUSIC, true);

                        String Ringtonepath= "content://media/internal/audio/media/297";
                            Uri Ringtone1 = Uri.parse(filepath);   
                            //Insert it into the database
                            Log.i("TAG", "the absolute path of the file is :"+
                                    ringtoneFile.getAbsolutePath());
                            Uri uri = MediaStore.Audio.Media.getContentUriForPath(
                                    ringtoneFile.getAbsolutePath());




                            getContentResolver().delete(uri, MediaStore.MediaColumns.DATA + "=\"" + ringtoneFile.getAbsolutePath() + "\"",
                                      null);
                            Uri newUri = getContentResolver().insert(uri, content);
                            System.out.println("uri=="+uri);
                            Log.i("TAG","the ringtone uri is :"+newUri);
                               RingtoneManager.setActualDefaultRingtoneUri(
                                       getApplicationContext(), RingtoneManager.TYPE_RINGTONE,
                                       newUri);

这篇关于Android铃声以编程方式设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆