2011-09-24 115 views
2

这已从原始帖子更改。铃声正在播放默认铃声

基本上发生了什么是我得到了这个来显示手机铃声列表中的铃声。它也选择它。在此代码中,它会在选定铃声后播放铃声(我已将它取出以缩小尺寸)。它似乎在播放手机的默认铃声。任何想法为什么?谢谢

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

下面是最终解决它的问题。

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; 
     } 

在加入Environment.getExternatStorageDirectory路径字符串()。的getPath(),而不是仅仅在SD卡它实际上能够播放声音和正常工作。

+0

我试过上面的代码,但继续收到“FileNotFound”异常。我正在模拟器上测试......这有什么区别吗? – worked

+2

@worked我不相信模拟器可以做这样的铃声。 – steven

+0

它可以,只需要设置正确的权限,并给SD卡一个实际的空间量。 – worked

0

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

“com.packange.name”应该是包的名称,我想你haven't叫它这样。硬编码您的软件包名称,或从正确的上下文中调用getPackageName()

更新:你可以从你的newSoundFile对象中得到一个URI,只是做Uri.fromFile(newSoundFile)

+0

@steven:然后编辑您的问题以反映真实的代码。 –

+0

@steven:那还不是你的包名,是吗?你真的打电话给你的包“com.package.name”? –

+0

我应该在哪里使用该Uri.fromFile(newSoundFile)?我无法使用它从mUri权利,因为然后不会有任何实际发送这个声音文件在 – steven