Android铃声代码片段

播放默认铃声
Uri alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setDataSource(this, alert);
final AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
if (audioManager.getStreamVolume(AudioManager.STREAM_ALARM) != 0)
{
player.setAudioStreamType(AudioManager.STREAM_ALARM);
player.setLooping(true);
player.prepare();
player.start();
}

设置默认来电铃声
public void setMyRingtone()
{
File k = new File("/sdcard/Shall We Talk.mp3"); // 设置歌曲路径
ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath());
values.put(MediaStore.MediaColumns.TITLE, "Shall We Talk");
values.put(MediaStore.MediaColumns.SIZE, 8474325);
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
values.put(MediaStore.Audio.Media.ARTIST, "Madonna");
values.put(MediaStore.Audio.Media.DURATION, 230);
values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
values.put(MediaStore.Audio.Media.IS_NOTIFICATION, false);
values.put(MediaStore.Audio.Media.IS_ALARM, false);
values.put(MediaStore.Audio.Media.IS_MUSIC, false);
// Insert it into the database
Uri uri = MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath());
Uri newUri = this.getContentResolver().insert(uri, values);
RingtoneManager.setActualDefaultRingtoneUri(this, RingtoneManager.TYPE_RINGTONE, newUri);
;}

需要的权限
<uses-permission android:name="android.permission.WRITE_SETTINGS"></uses-permission>

评论列表

广东硅谷学院
好深啊,表示看不懂。
2011-07-23 10:28:47
深圳二手车淘车城
Android越来越有名气
2011-07-21 22:40:31
宁波废纸回收
很开心能来到博主的博客哦,支持你
2011-07-09 12:37:28