【问题标题】:Android ringtone set programmatically?Android铃声以编程方式设置?
【发布时间】:2015-05-29 04:24:56
【问题描述】:

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

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

【问题讨论】:

标签: android ringtone


【解决方案1】:

也许你需要先删除它,我已经陷入困境一天了..

getContentResolver().delete(uri, MediaStore.MediaColumns.DATA
                            + "=\"" + file.getAbsolutePath() + "\"", null);

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-08
      相关资源
      最近更新 更多