【问题标题】:Set raw resource as ringtone in Android在Android中将原始资源设置为铃声
【发布时间】:2013-08-07 10:23:52
【问题描述】:

在我的 android 应用程序中,我想将 raw 文件夹中的音频文件设置为铃声。 为此我写了下面的代码,但它不起作用。

请帮我解决这个问题。 谢谢。

代码:

String name =best_song_ever.mp3;
File newSoundFile = new File("/sdcard/media/ringtone",
                        "myringtone.mp3");
                Uri mUri = Uri.parse("android.resource://"
                        + context.getPackageName() + "/raw/" + name);
                ContentResolver mCr = context.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/oog");
            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 {
                RingtoneManager.setActualDefaultRingtoneUri(context,
                        RingtoneManager.TYPE_RINGTONE, newUri);
            } catch (Throwable t) {

            }

            Toast.makeText(context, name + " is set as ringtone.",
                    Toast.LENGTH_LONG).show();
        }

【问题讨论】:

    标签: android android-contentprovider android-mediaplayer android-contentresolver mediastore


    【解决方案1】:

    以下代码解决了我的问题:

    String name = "your_raw_audio_name";
    File file = new File(Environment.getExternalStorageDirectory(),"/myRingtonFolder/Audio/");
    if (!file.exists()) {
       file.mkdirs();
    }
    
    String path = Environment.getExternalStorageDirectory()
       .getAbsolutePath() + "/myRingtonFolder/Audio/";
    File f = new File(path + "/", name + ".mp3");
    Uri mUri = Uri.parse("android.resource://"
                        + context.getPackageName() + "/raw/" + name);
    ContentResolver mCr = context.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(f);
        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, f.getAbsolutePath());
    values.put(MediaStore.MediaColumns.TITLE, name);
    values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
    values.put(MediaStore.MediaColumns.SIZE, f.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, true);
    
    Uri uri = MediaStore.Audio.Media.getContentUriForPath(f.getAbsolutePath());
    Uri newUri = mCr.insert(uri, values);
    
    try {
        RingtoneManager.setActualDefaultRingtoneUri(context,
            RingtoneManager.TYPE_RINGTONE, newUri);
        Settings.System.putString(mCr, Settings.System.RINGTONE,newUri.toString());
    } catch (Throwable t) {}
    

    【讨论】:

    • 我在 context.getContentResolver() 处遇到错误;我应该把它设置成什么?
    • 通过这段代码,我们正在使用原始音频文件在设备中创建一个新文件?就像提取音频一样吗?没有其他方法可以实现吗?
    【解决方案2】:

    如果您想再次设置与铃声相同的分辨率,以下修改可能会有所帮助

    Uri uri = MediaStore.Audio.Media.getContentUriForPath(f
                         .getAbsolutePath());
    mCr.delete(uri, MediaStore.MediaColumns.DATA + "=\"" + f.getAbsolutePath() + "\"", null);
    Uri newUri = mCr.insert(uri, values);
    

    【讨论】:

      【解决方案3】:

      这是我使用的代码:对我非常有用:)

          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);
      

      或者参考这个完整的tutorial

      编码愉快!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-03-02
        • 2011-06-21
        相关资源
        最近更新 更多