2017-02-08 3 views
-1

나는 응용 프로그램을 개발하고있어 그 응용 프로그램에서 하나의 버튼을 '소리를 선택하십시오. 사용자가이 버튼을 클릭하면 파일 관리자/메모리에서 오디오 파일을 선택하라는 메시지를 표시해야합니다.안드로이드 어떻게 sdcard/파일 관리자에서 오디오 파일을로드하고 그것을 재생

그래서, 나는 이것을 알고, Intent.Action_GetData을 사용해야 할 것입니다.

//code start 
Intent intent = new Intent(); 
intent.setType("audio/*"); 
intent.setAction(Intent.ACTION_GET_CONTENT); 
startActivityForResult(intent,1); 

@Override 
protected void onActivityResult(int requestCode,int resultCode,Intent data){ 

    if(requestCode == 1){ 

    if(resultCode == RESULT_OK){ 

     //the selected audio. 
     Uri uri = data.getData(); 
int SoundID=soundPool.Load(uri.toString(), 1); 
//SoundPool is already constructed and is working perfectly for the resource files 
PlaySound(SoundID); 
//PlaySound method is already defined 
    } 
    } 
    super.onActivityResult(requestCode, resultCode, data); 
} 
//end of code 

을하지만 Android 4.4하기 전에, 그것은 반환하기 때문에, 내가 어떻게 사용자가 선택한 파일의 적절한 URI를로드 할 것을받지 못했습니다 OnActivityResult에 지금
을 작동하지 않습니다 : 나도 같은 일을 해요 다른 URI와 Android 4.4 이후에 intent.GetData();에 다른 URI를 반환합니다. 자, 내가해야 할 일은?

또한 오디오 파일을 재생할 때 SoundPool을 사용해야한다는 것을 알고 있습니다. 실제로 코드는 리소스/원시 파일/오디오 파일에 잘 적용되지만로드하는 방법은 있습니다./SoundPool에있는 파일을이 URI에서 재생 하시겠습니까?

+0

'OnActivityResult'에서 수행 한 코드를 게시 할 수 있습니까? 그리고 어떻게'URI'를 얻고 있습니까? –

+0

@Prathamesh Toradmal이 코드 –

답변

2

을에 onActivityResult() llowing 변경 :

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    if (resultCode == Activity.RESULT_OK && data != null) 
    { 
     String realPath = null; 
     Uri uriFromPath = null; 

     realPath = getPathForAudio(YourActivity.this, data.getData()); 
     uriFromPath = Uri.fromFile(new File(realPath)); // use this uriFromPath for further operations 
    } 
}  

당신의 Activity에이 방법을 추가

public static String getPathForAudio(Context context, Uri uri) 
{ 
    String result = null; 
    Cursor cursor = null; 

    try { 
     String[] proj = { MediaStore.Audio.Media.DATA }; 
     cursor = context.getContentResolver().query(uri, proj, null, null, null); 
     if (cursor == null) { 
      result = uri.getPath(); 
     } else { 
      cursor.moveToFirst(); 
      int column_index = cursor.getColumnIndex(MediaStore.Audio.AudioColumns.DATA); 
      result = cursor.getString(column_index); 
      cursor.close(); 
     } 
    } 
    catch (Exception e) 
    { 
     e.printStackTrace(); 
    } 
    finally { 
     if (cursor != null) { 
      cursor.close(); 
     } 
    } 
    return result; 
} 

그것은 당신의 일을 할 것입니다 바랍니다. MediaPlayer 클래스를 사용하여 오디오를 재생할 수도 있습니다

+0

안녕하세요, 친애하는 친구, 덕분에 이제 파일의 적절한 경로를 가져올 수 있습니다. , 여전히 SoundPool.Load 메서드를 통해 사운드를로드 할 수 없습니다. 이 메서드는 파일 이름을 수동으로 시도했지만 작동하지 않는, 거기에 메서드 자체에 몇 가지 문제가 있다고 생각 : SoundPool.Load (문자열 경로, int 우선 순위) 당신이 도와주십시오 수 있습니까? –

+0

안녕하세요, 저는 MediaPlayer 클래스를 사용해 보았습니다. SoundPool에서 왜 작동하지 않는지 이해할 수 없습니다. 어떤 아이디어? –

+0

SoundPool을 사용한 적이 없기 때문에 SoundPool에 대한 확신이 없습니다. 이것이 내가 MediaPlayer에 대해 제안한 이유입니다. SoundPool에 대해 공부해야합니다. :) 내가 찾으면 알려주지. –

0

오디오를 선택하려는 경우 아래 코드를 프로젝트에 넣을 수 있습니다.

Intent intent_upload = new Intent(); 
intent_upload.setType("audio/*"); 
intent_upload.setAction(Intent.ACTION_GET_CONTENT); 
startActivityForResult(intent_upload,1); 

그리고 경로를 얻을 수

@Override 
protected void onActivityResult(int requestCode,int resultCode,Intent data){ 

    if(requestCode == 1){ 

    if(resultCode == RESULT_OK){ 

     //the selected audio. 
     Uri uri = data.getData(); 
    } 
    } 
    super.onActivityResult(requestCode, resultCode, data); 
} 
+0

을 작성했습니다. 이미이 방법을 시도했지만이 코드는 작동하지 않습니다. 먼저 모든 응용 프로그램이 다른 URI를 반환합니다. 두 번째로, SoundPool.Load methoad가 URI를 받아들이지 않고 사운드를로드하지 않습니다. –

0

한번에 아래로, 같은 활동에 onActivityResult를 오버라이드 (override) :

//method to get the file path from uri 
    public String getPath(Uri uri) { 
     Cursor cursor = getContentResolver().query(uri, null, null, null, null); 
     cursor.moveToFirst(); 
     String document_id = cursor.getString(0); 
     document_id = document_id.substring(document_id.lastIndexOf(":") + 1); 
     cursor.close(); 

     cursor = getContentResolver().query(
       android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI, 
       null, MediaStore.Images.Media._ID + " = ? ", new String[]{document_id}, null); 
     cursor.moveToFirst(); 
     String path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA)); 
     cursor.close(); 

     return path; 
    } 

를 다음로드 :

s2 = soundPool.load(YOU_PATH, PRIORITY);