作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在做我的第一个应用程序:我想放置一个按钮来将 .mp3 设置为铃声,一切正常,Logcat Eclipse 不会显示任何类型的错误,但是当我调用电话时(在模拟器和真手机上)没有声音!另外,当我转到“音乐”应用程序并尝试播放这些铃声(我在“最近添加”中找到它们)时,它向我显示:“抱歉,播放器不支持音频文件类型”。
我做错了什么?
这是我的代码:
setringtone.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast msg = Toast.makeText(TonosSet.this, "Sound set as ringtone!", Toast.LENGTH_LONG);
msg.show();
File k = new File("/sdcard/media/ringtone", "song_file.mp3");
Uri mUri = Uri.parse("android.resource://com.app/"+R.raw.song_file);
ContentResolver mCr = getContentResolver();
AssetFileDescriptor soundFile;
try {
soundFile= mCr.openAssetFileDescriptor(mUri, "r");
} catch (FileNotFoundException e) {
soundFile=null;
}
try {
InputStream ins = TonosSet.this .getResources().openRawResource (R.raw.song_file);
byte[] buffer = new byte[ins.available()];
ins.read(buffer);
ins.close();
String filename = Environment.getExternalStorageDirectory().toString()+File.separator+R.raw.song_file;
FileOutputStream fos = new FileOutputStream(filename);
fos.write(buffer);
fos.close();
} catch (IOException io) {
}
ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath());
values.put(MediaStore.MediaColumns.TITLE, "Name sound");
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
values.put(MediaStore.Audio.Media.ARTIST, "Artist");
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
Uri uri = MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath());
getContentResolver().delete(uri, MediaStore.MediaColumns.DATA + "=\"" + k.getAbsolutePath() + "\"", null);
Uri newUri = getContentResolver().insert(uri, values);
RingtoneManager.setActualDefaultRingtoneUri(
TonosSet.this,
RingtoneManager.TYPE_NOTIFICATION,
newUri);
}
});
请帮助我,我不知道该怎么办,我已经寻找解决方案大约 5 个小时,但我找不到 =(
最佳答案
也许您应该看看下面的帖子,它似乎提供了与您的版本不同的版本(关于 isNotification 和其他参数)。
关于java - Android:设置铃声,一切正常,但不响且无法播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12774721/
我是一名优秀的程序员,十分优秀!