选择sd卡android的铃声



在我的android应用程序中,我添加了从内置音调中选择通知音调的代码。但我还需要添加一个选项,从sd卡中选择歌曲。此外,最好添加仅选择音乐文件的条件。请帮帮我。我正在给我的应用程序中的代码

public boolean onOptionsItemSelected(MenuItem item)
    {
        Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
        intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_NOTIFICATION);
        intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, "Select Tone");
        intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, (Uri) null);
        this.startActivityForResult(intent, 5);
        return true;
    }
@Override
     protected void onActivityResult(final int requestCode, final int resultCode, final Intent intent)
     {
         if (resultCode == Activity.RESULT_OK && requestCode == 5)
         {
              Uri uri = intent.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
              if (uri != null)
              {
                  this.chosenRingtone = uri.toString();
              }
              else
              {
                  this.chosenRingtone = null;
              }
                  DatabaseHandler1 dbtone = new DatabaseHandler1(getApplicationContext());
                    int count = dbtone.getRowCount_5();
                    if(count > 0){
                    dbtone.resetTables_5();
                    }
                 dbtone.addUser_5(chosenRingtone);
          }            
      }

我在这里设置铃声

DatabaseHandler1 db2 = new DatabaseHandler1(context);
        int count = db2.getRowCount_5();
            if(count > 0){
         x =db2.getUserDetails_5();
        String temp =  x.get("tone");
                try{
                    notification.sound = Uri.parse(temp);
                    }
                catch (Exception e) {
                    notification.defaults |= Notification.DEFAULT_SOUND;
                    }
            }
            else{
        // Play default notification sound
        notification.defaults |= Notification.DEFAULT_SOUND;
            }

这将为您提供所有媒体文件

String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";
        //your projection statement 
        String[] projection = {
                MediaStore.Audio.Media._ID,
                MediaStore.Audio.Media.ARTIST,
                MediaStore.Audio.Media.TITLE,
                MediaStore.Audio.Media.DATA,
                MediaStore.Audio.Media.DISPLAY_NAME,
                MediaStore.Audio.Media.DURATION,
                MediaStore.Audio.Media.ALBUM_ID
        };
        //query 
        cursor = this.managedQuery(
                MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
                projection,
                selection,
                null,
                null);

        while(cursor.moveToNext()){
                songs.add(cursor.getString(0));
                songs.add(cursor.getString(1));
                songs.add(cursor.getString(2));
                songs.add(cursor.getString(3));
                songs.add(cursor.getString(4));
                songs.add(cursor.getString(5));
                album_id.add((long) cursor.getFloat(6));
        } 
        int a[]= new int[]{R.id.textView1 ,R.id.textView3};//, R.id.textview2};
        ListAdapter adapter = new SimpleCursorAdapter(this,
                R.layout.items, cursor, new String[]{MediaStore.Audio.Media.TITLE,           MediaStore.Audio.Media.ARTIST/*, MediaStore.Audio.Media.DURATION*/} ,a);
                setListAdapter(adapter); 
    }

试试这个代码,它将帮助您将音频文件的歌曲设置为铃声:

File path = new File(path, "mysong.mp3"); // path is a file to /sdcard/media/ringtone
ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA, path.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(path.getAbsolutePath());
Uri newUri = main.getContentResolver().insert(uri, values);
RingtoneManager.setActualDefaultRingtoneUri(
  myActivity,
  RingtoneManager.TYPE_RINGTONE,
  newUri
);

要在外部sd卡中搜索音频文件,可以使用以下代码:

public ArrayList<File> findAllMP3s() {
    static File storage = Environment.getExternalStorageDirectory();
    ArrayList<File> allFiles = new ArrayList<File>();
    recursiveAudioFinder(storage, allFiles);
    return allFiles;
}
public static void recursiveAudioFinder(File directory, ArrayList<File> data>) {
    if(directory == null) {
        return;
    }
    File[] files = directory.listFiles();
    for(File file: files) {
        // Grab audio files, write some filter to check if audio file
        if(file.isFile() && file.getAbsolutePath().endsWith(".mp3")) {
            data.add(file);
        } else if(file.isDirectory()) {
            // Recursive call to find files in sub directories
            recursiveAudioFinder(directory, data);
        }
    }
}

为此,必须添加权限READ_EXTERNAL_STORAGE。从列表中选择一个文件,然后使用RingtoneManager 设置铃声

相关内容

  • 没有找到相关文章

最新更新