44

我正在尝试找到一种方法来通过我的 Android 活动中的代码设置新的默认铃声。

我已经把铃声下载到了bytearray.

4

11 回答 11

72

最后,我设法将默认铃声设置为我下载的铃声。下面不包含下载代码,仅包含将其设置为默认铃声所需的代码。

File k = new File(path, "mysong.mp3"); // path is a file to /sdcard/media/ringtone

ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath());
values.put(MediaStore.MediaColumns.TITLE, "My Song title");
values.put(MediaStore.MediaColumns.SIZE, 215454);
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
values.put(MediaStore.Audio.Media.ARTIST, "Madonna");
values.put(MediaStore.Audio.Media.DURATION, 230);
values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
values.put(MediaStore.Audio.Media.IS_NOTIFICATION, false);
values.put(MediaStore.Audio.Media.IS_ALARM, false);
values.put(MediaStore.Audio.Media.IS_MUSIC, false);

//Insert it into the database
Uri uri = MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath());
Uri newUri = this.getContentResolver().insert(uri, values);

RingtoneManager.setActualDefaultRingtoneUri(
  myActivity,
  RingtoneManager.TYPE_RINGTONE,
  newUri
);  

无论如何,我不完全理解这段代码在做什么。

铃声管理器需要一个指向要设置为新铃声的文件的 uri。但是这个uri不能像“/sdcard/media/ringtones/mysong.mp3”这样直接到sdcard。那是行不通的!

您需要的是文件的外部文件 uri,可能类似于“/external/audio/media/46”

46是MediaStore数据库中列的id,所以需要先将sdcard文件添加到数据库中。

无论如何,mediastore 是如何维护它的 id 的呢?这个数字可能会变得非常高,因为您多次执行此操作。

我需要自己删除这一行吗?问题是有时我什至无法控制文件的删除,因为可以使用文件浏览器直接从 sdcard 中删除它。

于 2009-08-17T11:21:46.873 回答
4

Answer By Vidar 太长,每次您想将歌曲设置为铃声时,它都会添加重复的条目。相反,你应该试试这个

Uri newUri=Uri.parse("content://media/external/audio/media/"+ID);  
try {
      RingtoneManager.setActualDefaultRingtoneUri(context, RingtoneManager.TYPE_RINGTONE, newUri);
    }
catch (Throwable t) {


                  }
于 2016-08-26T07:24:35.703 回答
3
public void setRingtone() {
  String ringtoneuri = Environment.getExternalStorageDirectory().getAbsolutePath() + "/media/ringtone";
  File file1 = new File(ringtoneuri);
  file1.mkdirs();
  File newSoundFile = new File(ringtoneuri, "myringtone.mp3");


  Uri mUri = Uri.parse("android.resource://globalapps.funnyringtones/raw/sound_two.mp3");


  ContentResolver mCr = this.getContentResolver();
  AssetFileDescriptor soundFile;
  try {
   soundFile = mCr.openAssetFileDescriptor(mUri, "r");
  } catch (FileNotFoundException e) {
   soundFile = null;
  }

  try {
   byte[] readData = new byte[1024];
   FileInputStream fis = soundFile.createInputStream();
   FileOutputStream fos = new FileOutputStream(newSoundFile);
   int i = fis.read(readData);

   while (i != -1) {
    fos.write(readData, 0, i);
    i = fis.read(readData);
   }

   fos.close();
  } catch (IOException io) {
  }

  ContentValues values = new ContentValues();
  values.put(MediaStore.MediaColumns.DATA, newSoundFile.getAbsolutePath());
  values.put(MediaStore.MediaColumns.TITLE, "my ringtone");
  values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
  values.put(MediaStore.MediaColumns.SIZE, newSoundFile.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(newSoundFile.getAbsolutePath());
  Uri newUri = mCr.insert(uri, values);
  try {
   Uri rUri = RingtoneManager.getValidRingtoneUri(this);
   if (rUri != null)
    ringtoneManager.setStopPreviousRingtone(true);
   RingtoneManager.setActualDefaultRingtoneUri(getApplicationContext(), RingtoneManager.TYPE_RINGTONE, newUri);
   Toast.makeText(this, "New Rigntone set", Toast.LENGTH_SHORT).show();
  } catch (Throwable t) {
   Log.e("sanjay in catch", "catch exception"+e.getMessage());
  }

 }
于 2015-01-12T06:34:03.573 回答
2

您可以使用内置的RingtonePreference类。AndroidGuys 在这里有一个很好的教程。

于 2009-08-13T14:52:43.533 回答
0

这是我使用的代码!我希望它有帮助..
这也是链接

 String exStoragePath =    Environment.getExternalStorageDirectory().getAbsolutePath();
String path=(exStoragePath +"/media/alarms/"); 

saveas(RingtoneManager.TYPE_RINGTONE); 

sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,       Uri.parse("file://"+path+filename+".mp3"
  + Environment.getExternalStorageDirectory()))); 


 File k = new File(path, filename);

ContentValues values = new ContentValues(4);   
long current = System.currentTimeMillis();
values.put(MediaStore.MediaColumns.DATA, path + filename  );
values.put(MediaStore.MediaColumns.TITLE,  filename ); 
values.put(MediaStore.Audio.Media.DATE_ADDED, (int) (current / 1000));
values.put(MediaStore.Audio.Media.MIME_TYPE, "audio/3gpp");

//new
 values.put(MediaStore.Audio.Media.ARTIST, "cssounds ");
values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
values.put(MediaStore.Audio.Media.IS_NOTIFICATION, false);
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);

快乐编码!

于 2015-10-20T08:15:47.583 回答
0

我无法评论该解决方案,因为我在堆栈溢出方面没有足够的声誉......我只想添加一种将音频文件添加到媒体数据库中的方法,而无需直接访问数据库,从而避免重复。该解决方案基于 MediaScannerConnection,这是我使用的代码:

    String[] files = { audioFullPath };
    MediaScannerConnection.scanFile(
        getApplicationContext(),
        files,
        null,
        new OnScanCompletedListener() {
            @Override
            public void onScanCompleted(String path, Uri uri) {
                Log.v("myapp", "file " + path + " was scanned seccessfully: " + uri);
            }
        }
    );
于 2016-01-05T11:18:34.037 回答
0

我已经尝试了这些代码它的帮助

  private void setRingtone(Context context, String path) {
    if (path == null) {
        return;
    }
    File file = new File(path);
    ContentValues contentValues = new ContentValues();
    contentValues.put(MediaStore.MediaColumns.DATA, file.getAbsolutePath());
    String filterName = path.substring(path.lastIndexOf("/") + 1);
    contentValues.put(MediaStore.MediaColumns.TITLE, filterName);
    contentValues.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
    contentValues.put(MediaStore.MediaColumns.SIZE, file.length());
    contentValues.put(MediaStore.Audio.Media.IS_RINGTONE, true);
    Uri uri = MediaStore.Audio.Media.getContentUriForPath(path);
    Cursor cursor = context.getContentResolver().query(uri, null, MediaStore.MediaColumns.DATA + "=?", new String[]{path}, null);
    if (cursor != null && cursor.moveToFirst() && cursor.getCount() > 0) {
        String id = cursor.getString(0);
        contentValues.put(MediaStore.Audio.Media.IS_RINGTONE, true);
        context.getContentResolver().update(uri, contentValues, MediaStore.MediaColumns.DATA + "=?", new String[]{path});
        Uri newuri = ContentUris.withAppendedId(uri, Long.valueOf(id));
        try {
            RingtoneManager.setActualDefaultRingtoneUri(context, RingtoneManager.TYPE_RINGTONE, newuri);
            Toast.makeText(context, "Set as Ringtone Successfully.", Toast.LENGTH_SHORT).show();
        } catch (Throwable t) {
            t.printStackTrace();
        }
        cursor.close();
    }
}
于 2018-11-26T10:04:00.510 回答
0

使用此功能设置铃声

private void setAsRingtone(String musicId) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (Settings.System.canWrite(this)) {
            Uri uri = ContentUris.withAppendedId(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, Long.parseLong(musicId));
            RingtoneManager.setActualDefaultRingtoneUri(
                    this,
                    RingtoneManager.TYPE_RINGTONE,
                    uri
            );
            Toast.makeText(this, "Ring set successfully", Toast.LENGTH_SHORT).show();
        } else {
            Intent intent = new Intent(android.provider.Settings.ACTION_MANAGE_WRITE_SETTINGS);
            intent.setData(Uri.parse("package:" + getPackageName()));
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
        }
    }
}

音乐 ID 可以从光标处获得。希望您知道,或 在此处查看

于 2022-01-23T12:31:37.040 回答
0

提供铃声选择的意图。

final Uri currentTone= RingtoneManager.getActualDefaultRingtoneUri(MainActivity.this, RingtoneManager.TYPE_ALARM);
                Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
                intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_RINGTONE);
                intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, "Select Tone");
                intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, currentTone);
                intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_SILENT, false);
                intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, true);
                startActivityForResult(intent, 999);

然后在 中捕获选择的结果onActivityResult

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode == 999 && resultCode == RESULT_OK){
            Uri uri = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
            txtView.setText("From :" + uri.getPath());
            //Set selected ringtone here.
            RingtoneManager.setActualDefaultRingtoneUri(
                    this,
                    RingtoneManager.TYPE_RINGTONE,
                    uri
            );
        }
    }
于 2018-05-18T15:50:15.823 回答
0

如果接受的答案不起作用,请使用:

MediaStore.Audio.Media.INTERNAL_CONTENT_URI

而不是这个:

MediaStore.Audio.Media.getContentUriForPath()

在将值插入数据库时​​。

例如 :

// Defining ringtone.....
ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA, file.getAbsolutePath());
values.put(MediaStore.MediaColumns.TITLE, "Sonify");
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
values.put(MediaStore.Audio.Media.IS_NOTIFICATION,false);
values.put(MediaStore.Audio.Media.IS_ALARM, false);
values.put(MediaStore.Audio.Media.IS_MUSIC, false);

// Setting ringtone....
getContentResolver().delete(MediaStore.Audio.Media.INTERNAL_CONTENT_URI,MediaStore.Audio.Media.TITLE + " = \"Sonify\"",null); 
// To avoid duplicate inserts
Uri ringUri = getContentResolver().insert(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, values);
RingtoneManager.setActualDefaultRingtoneUri(this, RingtoneManager.TYPE_ALARM, ringUri);

不要忘记添加

<uses-permission android:name="android.permission.WRITE_SETTINGS" />

于 2021-09-30T18:48:13.743 回答
-1

我从 Android 的媒体应用程序中找到了这段代码。

Settings.System.putString(resolver, 
Settings.System.RINGTONE, ringUri.toString());

这是我的作品。

于 2010-05-06T17:09:13.547 回答