2010-12-12 3 views
1

제 문제는 약간 설명하기가 까다 롭습니다.새 파일 (경로)은 항상 파일이 없음을 말합니다.

내 프로젝트 (및 apk 파일)에 분리 된 리소스 폴더가 있습니다.

String path = "/resources/instruments/data/bongo/audio/bong1.wav"; 

이미

url = StreamHelper.class.getClassLoader().getResource(path); 
url.openStream(); 

와 함께 사용할 수 있지만, 실제로 나는 SoundPool로 파일을로드 할. 나는 이런 식 시도 :

SoundPool soundPool = new SoundPool( 5, AudioManager.STREAM_MUSIC, 0); 
soundPool.load (path, 1); 

을 ...하지만 난 항상 오류 정보를 얻을 : 나는 내가 필요로하는 것을 본 적이이 링크에 "... 오류 로딩/resourc을"

load(String path, int) 을 파일의 올바른 경로

File file = new File(path); 
if(file.exists()) 
    //always false but i thing if it would be true soundPool.load should work too 

이제 내 질문 : 어떻게 작동하는 경로가 있습니다. 아니면 내 문제에 대한 다른 아이디어가 있습니까 (AssetManager에서)?

btw. R.id.View와 같은 리소스를 얻을 수있는 특별한 안드로이드 방식이 있다는 것을 알고 있지만, 제 경우에는 다루기가 쉽지 않습니다.

감사합니다.

답변

5

개인적으로 WAV 파일이 '리소스'로 표시되지 않으며 '자산'폴더에 저장하고 AssetManager를 사용하는 것이 좋습니다.

이것은 나를 위해 작동 ...

/assets/instruments/data/bongo/audio 

은 ... 다음 거기 bong1.wav 파일을 복사 ... 프로젝트의 폴더 구조를 만듭니다.

다음을 사용하여로드하십시오. 참고 :

soundPool.play(mySoundId, 1, 1, 0, 0, 1); 
1

그건 ...

// Declare globally if needed 
    int mySoundId; 
    SoundPool soundPool = new SoundPool(5, AudioManager.STREAM_MUSIC, 0); 
    AssetManager am = this.getAssets(); 

    //Use in whatever method is used to load the sounds 
    try { 
     mySoundId = soundPool.load(am.openFd("instruments/data/bongo/audio/bong1.wav"), 1); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

사용이 그것을 재생 ... '악기'soundPool.load()에 대한 경로를 제공 할 때 앞에 '/'넣어하지 마십시오 클래스 경로가 아닌 파일 시스템 경로가 예상됩니다.

URL#getPath()을 사용하십시오.

soundPool.load(url.getPath()); 
+0

내 경우에는 작동하지 않습니다. 나는 이유를 모르지만 자원은 여전히 ​​발견되지 않습니다. 그것은 훨씬 더 쉽게 될 것이기 때문에 그것은 유감스러운 일입니다 :) –

관련 문제