5

我正在制作一个音板应用程序,并试图根据单击的菜单项将声音播放为铃声或通知。铃声目前在铃声菜单中显示为默认铃声,但在来电时不播放。我做错了什么?我的代码在下面列出。

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;  
}  
4

3 回答 3

1

根据这里的这个问题,您需要使用“外部”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);
于 2011-08-16T10:09:12.163 回答
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();

OR you can get your default notification sound.

Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    r = RingtoneManager.getRingtone(getApplicationContext(), notification);
    r.play();
于 2013-02-03T00:12:39.883 回答
0

嘿,我想你需要在 android manifest 中设置权限。以下是我使用的权限。

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

另外我不确定ogg文件。我使用了 mp3 文件,它对我有用。

于 2014-01-06T13:58:18.030 回答