如何让我的铃声在android中播放?
我正在制作一个音板应用程序,并试图将声音播放作为铃声或通知,具体取决于单击的菜单项。铃声当前在ringtone功能表中显示为默认铃声,但在接到电话时不播放。我做错了什么?下面列出了我的代码如何让我的铃声在android中播放?,android,ringtone,Android,Ringtone,我正在制作一个音板应用程序,并试图将声音播放作为铃声或通知,具体取决于单击的菜单项。铃声当前在ringtone功能表中显示为默认铃声,但在接到电话时不播放。我做错了什么?下面列出了我的代码 public boolean saveas(int ressound,String file,String typesound){ byte[] buffer=null; InputStream fIn = getBaseContext().getResources().openRawRe
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;
}
根据这个问题,您需要使用“外部”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);
或者您可以获得默认的通知声音
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
r = RingtoneManager.getRingtone(getApplicationContext(), notification);
r.play();
嘿,我想你需要在android清单中设置权限。 以下是我使用的权限。
<uses-permission android:name="android.permission.WRITE_SETTINGS" ></uses-permission>
我也不确定ogg文件。我使用mp3文件,它对我有效。你有错误吗?你看到的是什么行为?此外,如果发现异常导致应用程序无法按预期运行,请尝试记录。
<uses-permission android:name="android.permission.WRITE_SETTINGS" ></uses-permission>