保存听起来而是使用号位置的声音的标题铃声 [英] Saving sounds as ringtones using the title of the sound instead of number location

查看:96
本文介绍了保存听起来而是使用号位置的声音的标题铃声的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Android的应用程序音板用户可以长时间preSS的按钮,声音存放为铃声,通知或报警。一切工作正常和声音保存到SD卡,并设置为默认的声音,但声音名称设置为号位置,他们是在应用程序。例如,如果我救了第一个声音,这是名为笑,它将保存到SD卡,并在铃声列表为1的出现,而不是笑。我想要的声音与他们的标题不是他们的号码保存。

I have a soundboard app in android that users can longpress the buttons to save sounds as either ringtones, notifications, or alarms. Everything works fine and the sounds are saved to the sdcard and set as the default sound but the sound names are set as the number location where they are in the app. For example if I saved the first sound, which is titled "laughing" it will save to sdcard and show up in ringtone list as "1", not "laughing". I want the sounds to be saved with their title not their number.

我需要什么,我code更改为解决这个问题?我使用的是阵列声音标题设置为按钮等这里是code我有节省的声音:

What do I need to change in my code to fix this?? I am using an array to set the sound titles to the buttons, etc. Here is the code I have which saves the sounds:

public boolean setAsTone(int ressound, String filename, String toneType) {

    Log.v("File Name", "" + filename);
    Log.v("Tone Type", "" + toneType);
    byte[] buffer = null;
    InputStream fIn = getBaseContext().getResources().openRawResource(ressound);
    int size = 0;

    try {
        size = fIn.available();
        buffer = new byte[size];
        fIn.read(buffer);
        fIn.close();
    } catch (IOException e) {
        return false;
    }
    switch (Integer.parseInt(filename)) {

    }
    if (toneType.contains(getString(R.string.ringtone))) {
        path = "/sdcard/media/audio/"
                + getString(R.string.ringtone).toLowerCase() + "/";
        Log.v("Path", "" + path);
    } else if (toneType.contains(getString(R.string.notification))) {
        path = "/sdcard/media/audio/"
                + getString(R.string.notification).toLowerCase() + "/";
        Log.v("Path", "" + path);
    } else if (toneType.contains(getString(R.string.alarm))) {
        path = "/sdcard/media/audio/"
                + getString(R.string.alarm).toLowerCase() + "/";
        Log.v("Path", "" + path);
    }

    boolean exists = (new File(path)).exists();
    if (!exists) {
        new File(path).mkdirs();
    }

    FileOutputStream save;
    try {
        save = new FileOutputStream(path + filename);
        save.write(buffer);
        save.flush();
        save.close();
    } catch (FileNotFoundException e) {
        return false;
    } catch (IOException e) {
        return false;
    }

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

    File ringtoneFile = new File(path, filename);

    ContentValues values = new ContentValues();
    values.put(MediaStore.MediaColumns.DATA, ringtoneFile.getAbsolutePath());
    values.put(MediaStore.MediaColumns.TITLE, filename);
    values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/ogg");
    values.put(MediaStore.Audio.Media.ARTIST, "Laughing");
    Log.v("getAbsolute Path", "" + ringtoneFile.getAbsolutePath());
    if (toneType.contains(getString(R.string.ringtone))) {
        values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
        values.put(MediaStore.Audio.Media.IS_NOTIFICATION, false);
        values.put(MediaStore.Audio.Media.IS_ALARM, false);
        type = RingtoneManager.TYPE_RINGTONE;
        Log.v("Ring Type", "" + type);
    } else if (toneType.contains(getString(R.string.notification))) {
        values.put(MediaStore.Audio.Media.IS_RINGTONE, false);
        values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
        values.put(MediaStore.Audio.Media.IS_ALARM, false);
        type = RingtoneManager.TYPE_NOTIFICATION;
        Log.v("Ring Type", "" + type);
    } else if (toneType.contains(getString(R.string.alarm))) {
        values.put(MediaStore.Audio.Media.IS_RINGTONE, false);
        values.put(MediaStore.Audio.Media.IS_NOTIFICATION, false);
        values.put(MediaStore.Audio.Media.IS_ALARM, true);
        type = RingtoneManager.TYPE_ALARM;
        Log.v("Ring Type", "" + type);
    }

    values.put(MediaStore.Audio.Media.IS_MUSIC, true);

    // Insert it into the database
    Uri ringtoneUri = null;
    Uri uri = MediaStore.Audio.Media.getContentUriForPath(ringtoneFile
            .getAbsolutePath());
    Log.v("Uri Path",""+uri);
    getContentResolver().delete(
            uri,
            MediaStore.MediaColumns.DATA + "=\""
                    + ringtoneFile.getAbsolutePath() + "\"", null);

    ringtoneUri = this.getContentResolver().insert(uri, values);
    Log.v("Ringtone Uri", "" + ringtoneUri);
    Log.v("Final Ring Type", "" + type);
    RingtoneManager.setActualDefaultRingtoneUri(getApplicationContext(),
            type, ringtoneUri);
    RingtoneManager.getActualDefaultRingtoneUri(getApplicationContext(), type);
    return true;
}

}

推荐答案

我知道这是旧的,但它可以帮助别人。从ContentValues​​删除此行,然后它会拿起合适的标题:

I know this is old, but it may help somebody. Remove this line and then it will pick up the proper title from your ContentValues:

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

我相信这是你重新扫描刚创建的文件,并覆盖在媒体商店的入口,在这个过程中移除原ContentValues​​。

I believe that is rescanning the file you just created and overwriting the entry in the media store, removing your original ContentValues in the process.

这篇关于保存听起来而是使用号位置的声音的标题铃声的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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