如何解决无法将声音文件添加到 Mediastore铃声管理器
我想将自定义声音文件添加到我设备的铃声库 (MediaStore),这样如果用户希望更改由我的应用程序创建的通知声音,他应该简单地转到通知设置并选择我的应用程序特定铃声(myApp_Ring_1 、myApp_Ring_2 等)来自铃声管理器。目前我的声音文件正在被复制到 Music 文件夹,而不是被添加到 Mediastore(铃声管理器)。请帮忙。下面是我的代码。
public void AddAlertSound(Context c )
{
InsertCustomSoundInMediaStore("AlertSound/myApp_Ring_1.mp3","myApp_Ring_1",c);
}
public Android.Net.Uri InsertCustomSoundInMediaStore(string filePath,string name,Context c)
{
ContentValues values = new ContentValues();
ContentResolver contentResolver = c.ContentResolver;
var appName = "myApp";
values.Put(MediaStore.MediaColumns.DisplayName,name + ".mp3");
values.Put(MediaStore.MediaColumns.Title,name);
values.Put(MediaStore.MediaColumns.MimeType,"audio/mp3");
values.Put(MediaStore.MediaColumns.Size,GetDuration(filePath));
values.Put(MediaStore.Audio.Media.InterfaceConsts.Artist,appName);
values.Put(MediaStore.Audio.Media.InterfaceConsts.IsRingtone,true);
values.Put(MediaStore.Audio.Media.InterfaceConsts.IsNotification,true);
values.Put(MediaStore.Audio.Media.InterfaceConsts.IsAlarm,true);
values.Put(MediaStore.Audio.Media.InterfaceConsts.IsMusic,false);
Android.Net.Uri newUri = contentResolver.Insert(MediaStore.Audio.Media.ExternalContentUri,values);
try
{
AssetManager assets = c.Assets;
AssetFileDescriptor descriptor = Android.App.Application.Context.Assets.OpenFd(filePath);
using (System.IO.Stream inputStream = descriptor.CreateInputStream())
{
using (System.IO.Stream outputStream = contentResolver.OpenOutputStream(newUri))
{
byte[] buffer = new byte[inputStream.Length];
int bytesRead;
while ((bytesRead = inputStream.Read(buffer,buffer.Length)) > 0)
{
outputStream.Write(buffer,bytesRead);
outputStream.Flush();
}
}
}
}
catch (Exception ex)
{
Toast.MakeText(c,ex + "",ToastLength.Long).Show();
System.Console.WriteLine(ex.Message);
}
return newUri;
}
public double GetDuration(string fileName)
{
MediaPlayer mediaPlayer = new MediaPlayer();
AssetFileDescriptor descriptor = Android.App.Application.Context.Assets.OpenFd(fileName);
mediaPlayer.SetDataSource(descriptor.FileDescriptor,descriptor.StartOffset,descriptor.Length);
mediaPlayer.Prepare();
return mediaPlayer.Duration;
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。