在代码中设置默认铃声的问题

发布于 2025-01-06 18:09:23 字数 2361 浏览 0 评论 0原文

可能的重复:
如何在设置铃声之前清除 Mediastore

我有一套大约130个mp3 在我的 Android 应用程序中,包含不同的声音剪辑。它们列在列表视图中,当用户长按其中一个时,他们可以选择将其设置为默认铃声或通知。在大多数情况下,我将其用于铃声,但它有点不一致。

例如,它可能第一次设置默认铃声,但下次我尝试将另一个剪辑设置为默认铃声,然后进入我的铃声列表时,它选择了“静音”。另外,我注意到在整个测试过程中,该应用程序在我的铃声列表中创建了 3-4 个没有相应文件的选项,而且我不知道如何删除这些选项。

我不是一个非常有经验的 Android 开发人员,所以,我不太清楚我在这里做错了什么。这是我的 setRingtone() 的代码,其中传递了 resourceid:

public void playSound(int input){
    byte[] buffer=null;
    InputStream fIn = getBaseContext().getResources().openRawResource(input);
    int size=0;

    try {
        size = fIn.available();
        buffer = new byte[size];
        fIn.read(buffer);
        fIn.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
    }

    String path="/sdcard/sounds/";
    String filename="my_ringtone"+".mp3";

    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) {
        // TODO Auto-generated catch block
    } catch (IOException e) {
        // TODO Auto-generated catch block
    }

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

    File k = new File(path, filename);

    ContentValues values = new ContentValues();
    values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath());
    values.put(MediaStore.MediaColumns.TITLE, "MyRingtone");
    values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
    values.put(MediaStore.Audio.Media.ARTIST, "cssounds ");
    values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
    values.put(MediaStore.Audio.Media.IS_NOTIFICATION, false);
    values.put(MediaStore.Audio.Media.IS_ALARM, false);
    values.put(MediaStore.Audio.Media.IS_MUSIC, false);

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

Possible Duplicate:
How to clear Mediastore before setting ringtone

I have a set of about 130 mp3's in my android app of different sound clips. They're listed in a listview, and when the user long-presses on one of them, it gives them the option to set it as their default ringtone or notification. For the most part, I got this working for ringtone, but its kind of inconsistent.

For example, it may set the default ringtone the first time, but the next time I try to set another clip as default ringtone, and then go into my ringtone lists, it has 'Silent' selected. Also, I've noticed that throughout my testing, the app has created 3-4 options in my ringtone list that has no corresponding file, and I have no idea how to remove these.

I'm not a very experienced android dev, so, I can't quite figure out what I'm doing wrong here. Here is the code for my setRingtone() with resourceid being passed:

public void playSound(int input){
    byte[] buffer=null;
    InputStream fIn = getBaseContext().getResources().openRawResource(input);
    int size=0;

    try {
        size = fIn.available();
        buffer = new byte[size];
        fIn.read(buffer);
        fIn.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
    }

    String path="/sdcard/sounds/";
    String filename="my_ringtone"+".mp3";

    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) {
        // TODO Auto-generated catch block
    } catch (IOException e) {
        // TODO Auto-generated catch block
    }

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

    File k = new File(path, filename);

    ContentValues values = new ContentValues();
    values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath());
    values.put(MediaStore.MediaColumns.TITLE, "MyRingtone");
    values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
    values.put(MediaStore.Audio.Media.ARTIST, "cssounds ");
    values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
    values.put(MediaStore.Audio.Media.IS_NOTIFICATION, false);
    values.put(MediaStore.Audio.Media.IS_ALARM, false);
    values.put(MediaStore.Audio.Media.IS_MUSIC, false);

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜扑 2025-01-13 18:09:23

最有可能的是 getBaseContext是你的问题。基础上下文会随着其他事情的发生而改变。您需要应用程序的上下文。

Most likely, getBaseContext is your problem. The base context will change as other things happen. You want the context of your app.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文