2012-06-30 233 views
2

我已閱讀這2帖子link1和這一個link2但代碼似乎不適用於我。將原始資源設置爲鈴聲

這裏是我的代碼:

File newSoundFile = new File("/sdcard/media/ringtone", "myringtone.oog"); 
      Uri mUri = Uri.parse("android.resource://com.pack.android.myapp/R.raw.song1"); 
      ContentResolver mCr = Main.this.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(); 
       } 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/oog"); 
       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()); 
       Uri newUri = mCr.insert(uri, values); 


       try { 
        RingtoneManager.setActualDefaultRingtoneUri(Main.this, RingtoneManager.TYPE_RINGTONE, newUri); 
       } catch (Throwable t) { 
        // Log.d(TAG, "catch exception"); 
       } 

,我得到一個強制關閉。 Logcat在這裏:

06-30 03:13:03.731: E/AndroidRuntime(4187): FATAL EXCEPTION: main 
06-30 03:13:03.731: E/AndroidRuntime(4187): java.lang.NullPointerException 
06-30 03:13:03.731: E/AndroidRuntime(4187):  at com.pack.android.myapp.Main.Music(Main.java:83) 
06-30 03:13:03.731: E/AndroidRuntime(4187):  at com.pack.android.myapp.Main$2.onClick(Main.java:46) 
06-30 03:13:03.731: E/AndroidRuntime(4187):  at android.view.View.performClick(View.java:2538) 
06-30 03:13:03.731: E/AndroidRuntime(4187):  at android.view.View$PerformClick.run(View.java:9152) 
06-30 03:13:03.731: E/AndroidRuntime(4187):  at android.os.Handler.handleCallback(Handler.java:587) 
06-30 03:13:03.731: E/AndroidRuntime(4187):  at android.os.Handler.dispatchMessage(Handler.java:92) 
06-30 03:13:03.731: E/AndroidRuntime(4187):  at android.os.Looper.loop(Looper.java:130) 
06-30 03:13:03.731: E/AndroidRuntime(4187):  at android.app.ActivityThread.main(ActivityThread.java:3687) 
06-30 03:13:03.731: E/AndroidRuntime(4187):  at java.lang.reflect.Method.invokeNative(Native Method) 
06-30 03:13:03.731: E/AndroidRuntime(4187):  at java.lang.reflect.Method.invoke(Method.java:507) 
06-30 03:13:03.731: E/AndroidRuntime(4187):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867) 
06-30 03:13:03.731: E/AndroidRuntime(4187):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625) 
06-30 03:13:03.731: E/AndroidRuntime(4187):  at dalvik.system.NativeStart.main(Native Method) 

soundFile似乎爲空。這是爲什麼?我的歌曲是song1.mpe,位於res下的原始文件夾中。

權限在清單中增加:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 
    <uses-permission android:name="android.permission.WRITE_SETTINGS"></uses-permission> 
+0

給出的答案適用於你?@ ghostrider – Erum

回答

2

放入資產文件夾鈴聲的聲音,並使用此代碼

Uri path = Uri.parse("android.resource://yourpackagename/raw/yoursoundfile") 
RingtoneManager.setActualDefaultRingtoneUri(
        getApplicationContext(), RingtoneManager.TYPE_RINGTONE, 
        path); 
      Log .i("TESTT", "Ringtone Set to Resource: "+ path.toString()); 
      RingtoneManager.getRingtone(getApplicationContext(), path) 
        .play(); 

此權限加入AndroidManifest.xml中:

<uses-permission android:name="android.permission.WRITE_SETTINGS" /> 
+0

它也不適用於我。它設置爲我的手機的默認聲音鈴聲,我也得到一個關閉空指針例外的力量。 – ghostrider

+2

uri字符串在鈴聲資源文件夾中放置錯誤。如上所述,鈴聲需要位於res/** raw **文件夾中。 如果您將鈴聲放置在資產文件夾中,則uri字符串應爲: 「android.resource:// yourpackagename/** assets **/yoursoundfile」) – Baron

+0

@Baron是否需要將鈴聲保存到存儲中之前可以真正玩它嗎? – Roylee

0

這是我使用的代碼!對我來說非常有幫助,特別是這個tutorial

String exStoragePath = Environment.getExternalStorageDirectory().getAbsolutePath(); 
String path=(exStoragePath +"/media/alarms/"); 

saveas(RingtoneManager.TYPE_RINGTONE); 

sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+path+filename+".mp3" 
     + Environment.getExternalStorageDirectory()))); 


File k = new File(path, filename); 

ContentValues values = new ContentValues(4); 
long current = System.currentTimeMillis(); 
values.put(MediaStore.MediaColumns.DATA, path + filename ); 
values.put(MediaStore.MediaColumns.TITLE, filename); 
values.put(MediaStore.Audio.Media.DATE_ADDED, (int) (current/1000)); 
values.put(MediaStore.Audio.Media.MIME_TYPE, "audio/3gpp"); 

//new 
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, true); 
values.put(MediaStore.Audio.Media.IS_MUSIC, false); 

// Insert it into the database 
this.getContentResolver() 
.insert(MediaStore.Audio.Media.getContentUriForPath(k 
.getAbsolutePath()), values); 

HAPPY CODING!