내가 만든 m4a 파일의 재생 시간을 알고 싶은데, MediaPlayer나 MediaMetadataRetriever 로는 가져올 수가 없다….ㅠㅠ
그래서 어쩔 수 없이 이렇게….
/** * 재생 시간 가져오기 * * @param $context * context * @param $path * 전체 경로<br> * /storage/sdcard0/Sounds/voice_-1791181537.m4a * @return */ public static String duration(Context $context, String $path) { String result = "00:00"; String[] cursorColumns = new String[] { MediaStore.Audio.Media.DATA, MediaStore.Audio.Media.DURATION }; String selection = MediaStore.Images.Media.DATA + "=? "; String[] selectionArgs = new String[] { $path }; Cursor cursor = $context.getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, cursorColumns, selection, selectionArgs, MediaStore.Audio.Media.DEFAULT_SORT_ORDER); cursor.moveToFirst(); int duration = 0; while (!cursor.isAfterLast()) { int dataColumn = cursor.getColumnIndex(MediaStore.Audio.Media.DATA); int durationColumn = cursor.getColumnIndex(MediaStore.Audio.Media.DURATION); if ($path.equals(cursor.getString(dataColumn))) { duration = cursor.getInt(durationColumn); break; } cursor.moveToNext(); } cursor.close(); Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(duration); SimpleDateFormat format = new SimpleDateFormat("mm:ss"); result = format.format(calendar.getTime()); return result; }
물론 그 전에 미디어 스캔을 해줘야 한다.
이건 파일 만들 때 하면 될 듯….
File path = new File(Environment.getExternalStorageDirectory() + "/Sounds"); if (!path.exists()) path.mkdirs(); sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + path)));