在我的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;
}
答案 0 :(得分:0)
这将为您提供所有媒体文件
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);
}
答案 1 :(得分:0)
尝试此代码,它将帮助您将音频文件的歌曲设置为铃声:
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
);
答案 2 :(得分:0)
要在外部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