2011-09-24 187 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