闹钟铃声选择,使用了 TabHost 分了三个activity,分别是 1.应用raw文件下自带的音频文件。 2.手机系统自带的闹铃音频文件。 3.录音文件。
目的是要将音频的名称和时长获取出来显示在listview中。
获取raw文件中音频的名称和时长的方式如下:
Field[] fields = R.raw.class.getDeclaredFields(); int rawId; String rawName; for (int i = 0; i < fields.length; i++) { try { rawId = fields[i].getInt(R.raw.class); rawName = fields[i].getName(); Log.i(TAG, "-----------rawId="+rawId+"----------"); Uri uri = Uri.parse("android.resource://"+this.getPackageName()+"/"+ rawId); mediaPlayer = new MediaPlayer(); mediaPlayer.setDataSource(this, uri); mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); mediaPlayer.prepare(); int duration = mediaPlayer.getDuration(); audioInfo = new AlarmClockAudioInfo(rawName, RingTextUtil.showAudioTime(duration), uri); } catch (Exception e) { e.printStackTrace(); } mediaPlayer =null; audioInfos.add(audioInfo); } fields = null;
Uri uri = MediaStore.Audio.Media.INTERNAL_CONTENT_URI; Cursor cursor = resolver.query(uri, new String[]{"_data", "title","duration"}, "is_alarm = 1", null, null); while (cursor.moveToNext()){ String audioPath = cursor.getString(0); String ringName = cursor.getString(1); int ringDuration = cursor.getInt(2); audioInfo = new AlarmClockAudioInfo(ringName, RingTextUtil.showAudioTime(ringDuration), Uri.parse(audioPath)); audioInfos.add(audioInfo); } cursor.close();
我是这样设计的:如果有sd卡,则保存在sd卡上指定的文件夹中,如果没有,就保存在应用的files文件下。
/** * 获取录音的铃声信息 * @return 录音的铃声信息的集合 */ private List<AlarmClockAudioInfo> getAudioInfos() { audioInfos = new ArrayList<AlarmClockAudioInfo>(); //获取sd卡上的录音 boolean isSDCardExist = Environment.getExternalStorageState().equals( android.os.Environment.MEDIA_MOUNTED); // 判断sd卡是否存在 if(isSDCardExist){ File SDFileDir = new File(Environment.getExternalStorageDirectory()+"/cardhelper/record"); List<AlarmClockAudioInfo> SDRecordFiles = getRecordFiles(SDFileDir); if(SDRecordFiles != null){ for (AlarmClockAudioInfo alarmClockAudioInfo : SDRecordFiles) { audioInfos.add(alarmClockAudioInfo); } } } //获取保存在应用文件中的录音 File filesDir = this.getFilesDir(); List<AlarmClockAudioInfo> RecordFiles = getRecordFiles(filesDir); if(RecordFiles != null){ for (AlarmClockAudioInfo alarmClockAudioInfo : RecordFiles) { audioInfos.add(alarmClockAudioInfo); } } return audioInfos; } /** * 获取指定目录下的录音铃声信息 * @param fileDir * @return 录音铃声信息的集合 */ private List<AlarmClockAudioInfo> getRecordFiles(File fileDir){ List<AlarmClockAudioInfo> RecordAudioInfos = new ArrayList<AlarmClockAudioInfo>(); if(fileDir.exists()){ File[] records = fileDir.listFiles(); if(records != null){ for (File record : records) { AlarmClockAudioInfo audioInfo = null; try{ String recordName = record.getName(); //如果用split(".")[0] 会报数组角标越界异常 如同见鬼一样令人费解 recordName = recordName.substring(0, recordName.lastIndexOf(".")); Uri uri = Uri.parse(record.getPath()); mediaPlayer = new MediaPlayer(); mediaPlayer.setDataSource(AlarmClockChooseRingsRecordActivity.this, uri); mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); mediaPlayer.prepare(); int duration = mediaPlayer.getDuration(); audioInfo = new AlarmClockAudioInfo(recordName, RingTextUtil.showAudioTime(duration), uri); }catch(Exception e){ e.printStackTrace(); } mediaPlayer =null; RecordAudioInfos.add(audioInfo); } } } return RecordAudioInfos; }