2011-08-23 57 views
2

嗨,大家好,我想以編程方式設置設備鈴聲,但應用程序崩潰。以編程方式在Android中設置鈴聲

我使用的代碼是這樣的:

@Override 
public void onClick(View v) { 

    Uri uri = Uri.parse("android.resource://" 
     +"com.ghzncl.barneystinsonsoundboard/"+R.raw.single]); 

    RingtoneManager.setActualDefaultRingtoneUri(
     mContext, RingtoneManager.TYPE_NOTIFICATION, uri 
    ); 

    // I set the data source of the mPlayer. I pass to 
    // the method the context and the uri of the resource. 
    try{ 
     // I reset the mPlayer to its uninitialized state, in 
     // this way I don't need to stop the previews running quotes. 
     mPlayer.reset(); 
     mPlayer.setDataSource(mContext,uri); 
    }catch(Exception e){ 
     // Do Exception handling here... 
    } 

    try{ 
     mPlayer.prepare(); 
    }catch(Exception e){ 
     // Do Exception handling here... 
    } 

    //The quotes start 
    mPlayer.start(); 
} 

任何人都可以請幫我嗎?

+0

什麼是例外? – CommonMan

+0

檢查此問題http://stackoverflow.com/questions/1986756/setting-ringtone-in-android – Randroid

+0

現在是差異我添加的權限和應用程序不會崩潰了,但鈴聲設置爲默認的Android鈴聲,而不是到我的鈴聲。爲什麼? – Nicola

回答

0

Nocola我用這個代碼,以設置爲鈴聲:

ContentValues values = new ContentValues(4); 
long current = System.currentTimeMillis(); 
values.put(MediaStore.MediaColumns.DATA, path+audioname); 
values.put(MediaStore.MediaColumns.TITLE, path+audioname); 
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); 
///new 

但我在此代碼做了這麼多的調整,不知何故,我發現瞭如何做出正確的報警音編程的tutorial,和你自己你的方式... 只是做實驗!