【问题标题】:Saving a sound bite as a ringtone将声音片段保存为铃声
【发布时间】:2015-06-10 03:31:06
【问题描述】:

我正在尝试将 .wav 文件设置为 Android 设备上的铃声。下面的代码能够创建目录和声音文件,但实际上将文件设置为铃声似乎有问题,更不用说选择的铃声了。方法结束后,我去设备上的铃声,选择的铃声默认为“无”。知道这里发生了什么吗?我在清单中使用了 WRITE_EXTERNAL_STORAGE 权限。另外,声音片段的格式对我来说并不重要,我不介意转换任何需要转换的东西。

谢谢!!

private String saveAs(String fileName) {
    int resSound = getContext().getResources().getIdentifier(fileName, "raw", getContext().getPackageName());

    // Resolve save path and ensure we can read and write to it
    String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/media/audio/ringtones/";
    File dir = new File(path);
    fileName += ".wav";

    if (!dir.exists()) {
        dir.mkdirs();
    }

    if(!dir.canRead() || !dir.canWrite()) {
        return "Unable to save ringtone.";
    }

    // Load the audio into a buffer
    byte[] buffer;
    InputStream fIn = this.context.getBaseContext().getResources().openRawResource(resSound);
    int size;

    try {
        size = fIn.available();
        buffer = new byte[size];
        fIn.read(buffer);
        fIn.close();
    }
    catch (IOException e) {
        return "Error opening sound file";
    }


    File file = new File(dir, fileName);
    FileOutputStream save;
    try {
        save = new FileOutputStream(file);
        save.write(buffer);
        save.flush();
        save.close();
    }
    catch (FileNotFoundException e) {
        return "Error loading sound file.";
    }
    catch (IOException e) {
        return "Unable to save ringtone.";
    }

    // Register the sound byte with the OS and set its properties
    this.context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + path + fileName)));

    ContentValues values = new ContentValues();
    values.put(MediaStore.MediaColumns.DATA, file.getAbsolutePath());
    values.put(MediaStore.MediaColumns.TITLE, getSoundTitle(fileName));
    values.put(MediaStore.MediaColumns.SIZE, size);
    values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/*");
    values.put(MediaStore.Audio.Media.ARTIST, "Sound Clip");
    values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
    values.put(MediaStore.Audio.Media.IS_ALARM, false);
    values.put(MediaStore.Audio.Media.IS_MUSIC, false);

    //Insert it into the database
    Uri uri = this.context.getContentResolver().insert(MediaStore.Audio.Media.getContentUriForPath(file.getAbsolutePath()), values);
    RingtoneManager.setActualDefaultRingtoneUri(this.context, RingtoneManager.TYPE_RINGTONE, uri);

    return "Successfully set ringtone.";
}

【问题讨论】:

    标签: java android audio media ringtone


    【解决方案1】:

    对于遇到此问题的其他人,我想通了。就是这条线。

    this.context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + path + fileName)));
    

    如果有人碰巧知道这是为什么,我很想知道。谢谢!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-04-24
      • 1970-01-01
      • 1970-01-01
      • 2011-06-21
      • 1970-01-01
      • 2011-06-03
      • 1970-01-01
      相关资源
      最近更新 更多