2017-01-15 2 views
0

Так что я пытаюсь установить звуковой файл, расположенный в raw, как мелодию звонка, а затем сохранить его на мою SD-карту. Текущий код, похоже, сохраняет файл, но этот файл воспроизводит какую-то общую мелодию звонка, а не звук, который я пытаюсь воспроизвести. В чем проблема?Как установить исходный звуковой файл в качестве мелодии звонка

  String exStorePath = Environment.getExternalStorageDirectory().getAbsolutePath(); 
      String path = exStorePath + "/media/ringtone/"; 

      File k = new File(path, "wearenumberone.mp3"); 

      Uri mUri = Uri.parse("android.resource://com.example.matig.mlgsoundboarddeluxe/" + R.raw.wearenumberone); 
      ContentResolver mCr = SoundActivity1.this.getContentResolver(); 

      ContentValues values = new ContentValues(); 
      values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath()); 
      values.put(MediaStore.MediaColumns.TITLE, "NUMBERONE2"); 
      values.put(MediaStore.MediaColumns.SIZE, k.length()); 
      values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3"); 
      values.put(MediaStore.Audio.Media.ARTIST, "guy"); 
      values.put(MediaStore.Audio.Media.DURATION, 230); 
      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()); 
      Uri newUri = mCr.insert(uri, values); 

      RingtoneManager.setActualDefaultRingtoneUri(SoundActivity1.this, RingtoneManager.TYPE_RINGTONE, newUri); 
      Settings.System.putString(mCr,Settings.System.RINGTONE,newUri.toString()); 
      Toast.makeText(SoundActivity1.this,"done",Toast.LENGTH_SHORT).show(); 

ответ

1

попробовать это

  File file = new File(Environment.getExternalStorageDirectory(), 
       "/myRingtonFolder/Audio/"); 
     if (!file.exists()) { 
      file.mkdirs(); 
     } 

     String path = Environment.getExternalStorageDirectory() 
       .getAbsolutePath() + "/myRingtonFolder/Audio/"; 

     File f = new File(path + "/", name + ".mp3"); 

     Uri mUri = Uri.parse("android.resource://" 
       + context.getPackageName() + "/raw/" + name); 
     ContentResolver mCr = context.getContentResolver(); 
     AssetFileDescriptor soundFile; 
     try { 
      soundFile = mCr.openAssetFileDescriptor(mUri, "r"); 
     } catch (FileNotFoundException e) { 
      soundFile = null; 
     } 

     try { 
      byte[] readData = new byte[1024]; 
      FileInputStream fis = soundFile.createInputStream(); 
      FileOutputStream fos = new FileOutputStream(f); 
      int i = fis.read(readData); 

      while (i != -1) { 
       fos.write(readData, 0, i); 
       i = fis.read(readData); 
      } 

      fos.close(); 
     } catch (IOException io) { 
     } 
     ContentValues values = new ContentValues(); 
     values.put(MediaStore.MediaColumns.DATA, f.getAbsolutePath()); 
     values.put(MediaStore.MediaColumns.TITLE, name); 
     values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3"); 
     values.put(MediaStore.MediaColumns.SIZE, f.length()); 
     values.put(MediaStore.Audio.Media.ARTIST, R.string.app_name); 
     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, true); 

     Uri uri = MediaStore.Audio.Media.getContentUriForPath(f 
       .getAbsolutePath()); 
     Uri newUri = mCr.insert(uri, values); 

     try { 
      RingtoneManager.setActualDefaultRingtoneUri(context, 
        RingtoneManager.TYPE_RINGTONE, newUri); 
      Settings.System.putString(mCr, Settings.System.RINGTONE, 
        newUri.toString()); 
     } catch (Throwable t) { 

     } 
Смежные вопросы