铃声正在播放默认铃声

时间:2011-09-24 03:06:56

标签: android

这已从原始帖子更改。

基本上发生了什么事情我已经得到了这个来在手机铃声列表中显示铃声。它也选择它。在此代码中,它在选择后播放铃声(我已将其取出以减小大小)。它似乎正在播放手机默认铃声。有什么想法吗?感谢

private void ringtone(int p){
    File newSoundFile = new File("/sdcard/media/ringtone", "raven.wav");
    Uri mUri = Uri.parse("android.resource://com.fs.hh/"+R.raw.raven);
    ContentResolver mCr = 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(newSoundFile);
          int i = fis.read(readData);

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

          fos.close();
          soundFile.close();
       } catch (IOException io) {
       }

       ContentValues values = new ContentValues();
       values.put(MediaStore.MediaColumns.DATA, newSoundFile.getAbsolutePath());
       values.put(MediaStore.MediaColumns.TITLE, "my ringtone");
       values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/wav");
       values.put(MediaStore.MediaColumns.SIZE, newSoundFile.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, false);

       Uri uri = MediaStore.Audio.Media.getContentUriForPath(newSoundFile.getAbsolutePath());
       mCr.delete(uri, MediaStore.MediaColumns.DATA + "=\"" + newSoundFile.getAbsolutePath() + "\"", null);

       Uri newUri = mCr.insert(uri, values);



      RingtoneManager.setActualDefaultRingtoneUri(Soundboard.this, RingtoneManager.TYPE_RINGTONE, newUri);



}

我还尝试添加一个/之后的铃声并使其获得字节大小。两者都没有奏效。

我也尝试过这段代码,它会导致同样的问题。它显示在通知部分中选中,但播放的声音不是正确的声音。这听起来像是默认铃声。

private boolean setRingtone(int p){
    int ressound = whichSound(p); //The R.raw.sound
    String fileTitle = fileTitle(p); // sound
    String soundTitle = soundTitle(p); // Sound
    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) {
    // TODO Auto-generated catch block
    return false;
    }

    String path="/sdcard/media/notification";
    String filename=fileTitle+".wav";

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

    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, soundTitle);
    values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/wav");
    values.put(MediaStore.Audio.Media.ARTIST, "HH");
    values.put(MediaStore.Audio.Media.IS_RINGTONE, false);
    values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
    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_NOTIFICATION,
    newUri
    );

        return false;
      } 

2 个答案:

答案 0 :(得分:2)

这是最终解决的问题。

private boolean setRingtone(int p){
    int ressound = whichSound(p);
    String fileTitle = fileTitle(p);
    String soundTitle = soundTitle(p);
    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) {
    // TODO Auto-generated catch block
    return false;
    }

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

    String filename=fileTitle+".wav";

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

    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, soundTitle);
    values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/wav");
    values.put(MediaStore.Audio.Media.ARTIST, "");
    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 uri = MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath());
    getContentResolver().delete(uri, MediaStore.MediaColumns.DATA + "=\"" + k.getAbsolutePath() + "\"", null);
    Uri newUri = getContentResolver().insert(uri, values); 
    RingtoneManager.setActualDefaultRingtoneUri(this, RingtoneManager.TYPE_RINGTONE, newUri);



    return false;
      }  

在路径String中添加Environment.getExternatStorageDirectory()。getPath()而不仅仅是sdcard,它实际上能够播放声音并正常工作。

答案 1 :(得分:0)

  

Uri mUri = Uri.parse(“android.resource://com.packange.name/”+ R.raw.raven);

“com.packange.name”应该是你的包的名称,我假设你没有这样称呼它。对软件包名称进行硬编码,或从适当的上下文中调用getPackageName()

更新:您可以从newSoundFile对象中获取一个uri,只需执行Uri.fromFile( newSoundFile )