我正在创建闹钟应用所以我需要在闹钟时间播放音乐。现在它使用铃声管理器顺利工作,请参阅下面的代码
if(d1==1) {
File k = new File(url); // set File from path
if (url != null) { // file.exists
ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath());
values.put(MediaStore.MediaColumns.TITLE, "ring");
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
values.put(MediaStore.MediaColumns.SIZE, k.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(k
.getAbsolutePath());
context.getContentResolver().delete(
uri,
MediaStore.MediaColumns.DATA + "=\""
+ k.getAbsolutePath() + "\"", null);
newUri = context.getContentResolver().insert(uri, values);
// Toast.makeText(context,""+period,Toast.LENGTH_LONG).show();
if(period==0)
{
ringtone = RingtoneManager.getRingtone(context, newUri);
ringtone.play();
}
else
{
new CountDownTimer(period*1000,1000) { // adjust the milli seconds here
public void onTick(long millisUntilFinished) {
if (count==1)
{
ringtone = RingtoneManager.getRingtone(context, newUri);
ringtone.play();
}
count=count+1;
}
public void onFinish() {
ringtone.stop();
}
}.start();
}
现在我想使用媒体播放器播放此音乐,我使用的代码不能正常工作
try {
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setDataSource(context, newUri);
final AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
if (audioManager.getStreamVolume(AudioManager.STREAM_RING) != 0) {
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_RING);
mMediaPlayer.setLooping(true);
mMediaPlayer.prepare();
mMediaPlayer.start();
}
}
catch (Exception e)
{
}
请有人给我看一个解决方案