gpt4 book ai didi

java - Android:设置铃声,一切正常,但不响且无法播放

转载 作者:行者123 更新时间:2023-12-01 05:05:21 33 4
gpt4 key购买 nike

我正在做我的第一个应用程序:我想放置一个按钮来将 .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 和其他参数)。

How to set ringtone in Android from my activity?

关于java - Android:设置铃声,一切正常,但不响且无法播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12774721/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com