【问题标题】:Problems with setting Default Ringtone in code [duplicate]在代码中设置默认铃声的问题[重复]
【发布时间】:2012-02-19 22:14:48
【问题描述】:

可能重复:
How to clear Mediastore before setting ringtone

我的 android 应用程序中有一组大约 130 个 mp3 的不同声音片段。它们列在列表视图中,当用户长按其中一个时,它会为他们提供将其设置为默认铃声或通知的选项。在大多数情况下,我把它用于铃声,但它有点不一致。

例如,它可能第一次设置默认铃声,但下次我尝试将另一个剪辑设置为默认铃声,然后进入我的铃声列表时,它选择了“静音”。另外,我注意到在整个测试过程中,该应用在我的铃声列表中创建了 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
    );
}

【问题讨论】:

标签: android eclipse ringtone


【解决方案1】:

很可能,getBaseContext 是您的问题。基本上下文会随着其他事情的发生而改变。您需要应用的上下文。

【讨论】:

  • 这有点用,但是,每次我设置新的默认铃声时,我仍然会在铃声列表中获得多个列表。上面的代码一直用我想要的相同文件名覆盖文件,但是当我进入 android 的铃声选择时,它会在我的 raw/ 目录(sound1、sound2、sound3 等)中列出一堆铃声作为它们的名称.) 而不是“我的铃声”。知道如何解决这个问题吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-12-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-04
  • 2013-02-04
相关资源
最近更新 更多