2013-08-27 108 views
2

在Jelly Bean級別18之前,我可以使用不在Media store中的音樂文件(例如,文件夾中的private .mp3文件包含.nomedia文件,因此Media掃描儀不掃描此文件夾,但不知道這個文件),但從Android 4.3(在Nexus 4上測試)我不能這樣做,它只適用於Media Scanner已掃描的音樂文件。如何將不在Media Store中的音樂文件設置爲鈴聲

這個問題的真正原因是我無法將ContentValues與MediaColumns插入.DATA是未由Media Scanner掃描的文件的絕對路徑,insert方法始終返回null。

Uri newUri = getCR().insert(uri, contentValues); // returns null in Android 4.3 

有沒有人有解決方法使用私人文件(不掃描,不被媒體掃描器識別)作爲鈴聲?

這是我如何設置鈴聲:

File ringtoneFile = new File(audio.getPath()); 

    ContentValues cv = new ContentValues(); 
    cv.put(MediaColumns.DATA, ringtoneFile.getAbsolutePath()); 
    cv.put(MediaColumns.TITLE, audio.getTitle()); 

    cv.put(MediaColumns.MIME_TYPE, "audio/*"); 
    if (audio.getArtist() != null) 
     cv.put(Media.ARTIST, audio.getArtist()); 

    cv.put(Media.IS_RINGTONE, true); 
    cv.put(Media.IS_NOTIFICATION, false); 
    cv.put(Media.IS_ALARM, false); 
    cv.put(Media.IS_MUSIC, false); 

    Uri uri = Media.getContentUriForPath(ringtoneFile.getAbsolutePath()); 
    Uri newUri = getCR().insert(uri, cv); ////return null in Android 4.3 
    if (newUri == null) 
    { 
     Cursor c = getCR().query(uri, new String[] { Media._ID }, Media.DATA + "=?", 
       new String[] { ringtoneFile.getAbsolutePath() }, null); 
     long id = -1; 
     if (c != null && c.moveToFirst()) 
     { 
      id = c.getLong(c.getColumnIndex(Media._ID)); 
      newUri = Uri.parse(uri.toString() + "/" + id); 
      c.close(); 
     } 
    }  

    if (newUri != null) 
    { 
     RingtoneManager.setActualDefaultRingtoneUri(getAppContext(), RingtoneManager.TYPE_RINGTONE, newUri);    
    }  
+0

請你可以把一個完整的代碼,以便我們可以試試嗎? –

+0

我剛剛添加了我的代碼。 – Wayne

+0

阿波羅(音樂播放器)可以做到這一點,所以它可以做到。檢查Apollo的源代碼https://github.com/CyanogenMod/android_packages_apps_Apollo,看看你能從這裏瞭解到什麼。 – user2606414

回答

0

這是我的解決方法:

  • 複製媒體文件到手機鈴聲文件夾(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_RINGTONES)),記得是否創建該文件夾不存在。
  • 使用問題中的相同代碼將該文件設置爲默認鈴聲。
1

我已經嘗試的東西,它爲我工作。希望它也能幫助你。

我取在設備上的所有可用的歌曲以下方法:

static void setRingtone(Context context, long id) { 
    ContentResolver resolver = context.getContentResolver(); 
    // Set the flag in the database to mark this as a ringtone 
    Uri ringUri = ContentUris.withAppendedId(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, id); 
    try { 
     ContentValues values = new ContentValues(2); 
     values.put(MediaStore.Audio.Media.IS_RINGTONE, "1"); 
     values.put(MediaStore.Audio.Media.IS_ALARM, "1"); 
     resolver.update(ringUri, values, null, null); 
    } catch (UnsupportedOperationException ex) { 
     // most likely the card just got unmounted 
     Log.e("Music app", "couldn't set ringtone flag for id " + id); 
     return; 
    } 

    String[] cols = new String[] { 
      MediaStore.Audio.Media._ID, 
      MediaStore.Audio.Media.DATA, 
      MediaStore.Audio.Media.TITLE 
    }; 

    String where = MediaStore.Audio.Media._ID + "=" + id; 
    Cursor cursor = query(context, MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, 
      cols, where , null, null); 
    try { 
     if (cursor != null && cursor.getCount() == 1) { 
      // Set the system setting to make this the current ringtone 
      cursor.moveToFirst(); 
      Settings.System.putString(resolver, Settings.System.RINGTONE, ringUri.toString()); 
      String message = context.getString(R.string.ringtone_set, cursor.getString(2)); 
      Toast.makeText(context, message, Toast.LENGTH_SHORT).show(); 
     } 
    } finally { 
     if (cursor != null) { 
      cursor.close(); 
     } 
    } 

public static long [] getAllSongs(Context context) { 
    //System.out.println("In get All Songs Method"); 
    Cursor c = query(context, MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, 
      new String[] {MediaStore.Audio.Media._ID}, MediaStore.Audio.Media.IS_MUSIC +"!= 0", 
      null, MediaStore.Audio.Media.TITLE + " ASC"); 
    try { 
     if (c == null || c.getCount() == 0) { 
      return null; 
     } 
     int len = c.getCount(); 
     long [] list = new long[len]; 
     for (int i = 0; i < len; i++) { 
      c.moveToNext(); 
      list[i] = c.getLong(0); 
      //System.out.println("ID IS: "+c.getLong(0)); 
     } 
     //System.out.println("Total Songs are:); 

     // ALL_SONGS_ID = list; 
     return list; 
    } finally { 
     if (c != null) { 
      c.close(); 
     } 
    } 
} 

獲取聽歌我正在選擇聲音文件,並與下面的代碼將其設置爲鈴聲後}

在上面的方法id是所選歌曲的歌曲ID。

希望這會幫助你。隨意問你是否沒有得到解決方案。

享受Coading ... :)

+0

謝謝,但你誤解我的問題。我想製作一首不在媒體商店中的歌曲。您的代碼只是查詢媒體商店中的所有歌曲,並將其中的1個設置爲鈴聲。 – Wayne

+0

@Wayne我已經回答了你的問題的答案,但我認爲你需要它的代碼。無論如何享受編碼。 :) –

相關問題