2012-08-02 3 views
1

나는 안드로이드 응용 프로그램을 사용하여 캐시 디렉토리에 동적으로 midi 파일을 생성합니다.Android MediaPlayer는 설치시 파일이 필요합니다.

생성 후 동일한 앱 내에서 MediaPlayer로 파일을 재생합니다.

앱을 처음 실행할 때 이미 캐시 디렉토리에 파일이 있어야합니다 (앱이 다운 됨). 파일 관리자를 사용하여 더미 파일을 먼저 배치하면 에뮬레이터에서 작동합니다. 어떻게 이것을 피할 수 있습니까?

파일을 필요로하지 않고 처음으로 태블릿에서 실행하려면 앱이 필요합니다.

지금 이러한 명령을 사용하고 있습니다 :

try { 

      filePath = getCacheDir() + "/optimuse" + song + ".mid"; 
      file = new File(filePath); 

      inputStream = new FileInputStream(file); 
      if (inputStream.getFD().valid()) { 
       System.out.println("Valid!"); 
      } 
     } catch (Exception e1) { 
      e1.printStackTrace(); 
      System.exit(-1); 
     } 

     try { 
      mediaPlayer = new MediaPlayer(); 
      mediaPlayer.setDataSource(inputStream.getFD()); 
      inputStream.close(); 
     } catch (Exception e1) { 
      e1.printStackTrace(); 
      System.exit(-1); 
     } 

     try { 
      mediaPlayer.prepare(); 
     } catch (IllegalStateException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

이 주변에 어떤 방법이 있나요?

감사합니다.

답변

1

파일을 사용하기 전에 파일이 존재하는지 확인해야할까요? File#exists() 메서드를 사용하여이 작업을 수행 할 수 있습니다.

먼저 Context#getFileStreamPath(String) 메서드를 사용합니다. 여기서 String은 액세스하려는 파일의 파일 이름입니다. 그런 다음 반환 된 객체에 File#exists()을 호출 할 수 있습니다.

+0

고마워요! 방금 제 3 행 다음에 if (file.exists()) {를 추가했습니다. – dorien

관련 문제