2011-03-30 31 views
5

我正在製作一個音板應用程序,並試圖根據點擊哪個菜單項目來進行鈴聲或通知。鈴聲目前在鈴聲菜單中顯示爲默認鈴聲,但在打電話時無法播放。我在做什麼錯了?我的代碼列在下面。如何讓我的鈴聲在android中播放?

public boolean saveas(int ressound,String file,String typesound){ 
    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/audio/ringtones/"; 
    String filename=file+".ogg"; 

    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, file); 
    values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/ogg"); 
    values.put(MediaStore.Audio.Media.ARTIST, "douchebag"); 
    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); 

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

    //set ringtone 
    Uri ringtoneUri = Uri.parse(path+filename); 
    if(typesound=="ringtone") 
     RingtoneManager.setActualDefaultRingtoneUri(this, RingtoneManager.TYPE_RINGTONE, ringtoneUri); 

    return true; 
} 
+0

你有錯誤嗎?你看到的行爲是什麼?另外,如果發現異常,請嘗試進行日誌記錄,以防它導致應用程序無法正常工作。 – CrackerJack9 2011-08-12 15:51:31

回答

1

根據這個問題here,你需要設置鈴聲與「外部」 URI。據我所知,你基本上有所有的東西,只需要做一個小小的調整。

嘗試,如果這個片段對你的作品:

//Insert it into the database 
Uri ringtoneUri = this.getContentResolver().insert(MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath()), values); 
//set ringtone  
if(typesound=="ringtone") 
    RingtoneManager.setActualDefaultRingtoneUri(this, RingtoneManager.TYPE_RINGTONE, ringtoneUri); 
0
FullBatteryAlarm.prefs = PreferenceManager 
      .getDefaultSharedPreferences(this); 
    String strRingtonePrefs = FullBatteryAlarm.prefs.getString(
      FullBatteryAlarm.res.getString(R.string.ringtone), "content://settings/system/alarm_alert"); 
    //Log.i("strringtone", strRingtonePrefs); 
    Uri notification = Uri.parse(strRingtonePrefs); 

    AudioManager mgr = (AudioManager)getSystemService(Context.AUDIO_SERVICE); 
    //mgr.getStreamMaxVolume(AudioManager.STREAM_ALARM); 
    mgr.setStreamVolume(
      AudioManager.STREAM_RING, 
      mgr.getStreamMaxVolume(AudioManager.STREAM_RING), 
      AudioManager.FLAG_PLAY_SOUND); 
    //Toast.makeText(this, "MyAlarmService.onStart()", Toast.LENGTH_LONG).show(); 

或者你可以得到默認的通知聲音。

Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
    r = RingtoneManager.getRingtone(getApplicationContext(), notification); 
    r.play(); 
0

嘿,我想你需要在android清單中設置權限。 以下是我使用的權限。

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

還有我不知道什麼是Ogg文件。我用MP3文件,它爲我工作。