2013-10-27 124 views
2

我試圖用MediaPlayer播放內部存儲器中的文件,但我無法這樣做。初始化序列在setDataSource部分上失敗。我使用的代碼:訪問內部存儲的媒體文件 - Android上的MediaPlayer

public class HelloMediaPlayer extends Activity { 

    private MediaPlayer mp = null; 
    private Button play; 
    private boolean isPrepared = false; 
    private String fileName; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_hello_media_player); 

     //init mediaplayer. 
     mp = new MediaPlayer(); 

     mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { 

      @Override 
      public void onPrepared(MediaPlayer mp) { 
       Log.i(this.toString(), "Mediaplayer prepared."); 
       isPrepared = true; 
      } 
     }); 

     fileName = getFilesDir().getAbsolutePath() + "/word_recorded.wav"; 
     //fileName = Environment.getExternalStorageDirectory().getAbsolutePath() + "/word.wav"; 
     Log.v(this.toString(), "filename = " + fileName); 
     File file = new File(fileName); 
     String str = file.getName(); 
     Log.v(this.toString(), "file name = " + str); 
     if(file.exists()) { 
      if(file.canRead()) { 
       Log.v(this.toString(), fileName + " exists and can be read."); 
      } else { 
       Log.e(this.toString(), fileName + " exists but cannot be read."); 
       if(file.setReadable(true, false)) { 
        Log.v(this.toString(), "Changed permissions on " + file.getName()); 
       } else { 
        Log.e(this.toString(), "Failed to set permissions either."); 
        Log.e(this.toString(), "Attempting to copy into another file."); 

        try { 
         @SuppressWarnings("deprecation") 
         FileOutputStream fos = openFileOutput("word_recorded2.wav", Context.MODE_WORLD_READABLE); 
         InputStream is = openFileInput("word_recorded.wav"); 

         //copy data. 
         byte[] buffer = new byte[8192]; 
         int length = 0; 
         try { 
          while((length = is.read(buffer)) > 0) { 
           fos.write(buffer, 0, length); 
          } 
          fos.flush(); 
          fos.close(); 
          is.close(); 
         } catch (IOException e) { 
          // TODO Auto-generated catch block 
          e.printStackTrace(); 
         } 
         Log.v(this.toString(), "Copied file name.");       
        } catch (FileNotFoundException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 
       } 
      } 
     } else { 
      Log.e(this.toString(), fileName + " does not exist."); 
     } 

     try { 
      //mp.setDataSource(openFileInput(str).getFD()); 
      mp.setDataSource(fileName); 
     } catch (IllegalArgumentException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } catch (IllegalStateException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } catch (FileNotFoundException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } catch (IOException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } 

     Log.v(this.toString(), "Data source set to: " + fileName); 
     mp.setAudioStreamType(AudioManager.STREAM_MUSIC); 
     Log.v(this.toString(), "Audio stream for mp set."); 
     try { 
      mp.prepare(); 
     } catch (IllegalStateException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     play = (Button) findViewById(R.id.btn); 
     play.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       Log.v(this.toString(), "Clicked Play button."); 
       if(isPrepared) { 
        Log.v(this.toString(), "Mediaplayer is prepared."); 
        mp.start(); 
       } else { 
        Log.e(this.toString(), "mediaplayer is not prepared."); 
       } 
      } 
     }); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.hello_media_player, menu); 
     return true; 
    } 

} 

LogCat消息:

10-27 10:23:31.630: V/[email protected](19317): file name = word_recorded.wav 
10-27 10:23:31.640: E/[email protected](19317): /data/data/com.sriram.hellomediaplayer/files/word_recorded.wav exists but cannot be read. 
10-27 10:23:31.640: E/[email protected](19317): Failed to set permissions either. 
10-27 10:23:31.650: E/[email protected](19317): Attempting to copy into another file. 
10-27 10:23:31.650: W/System.err(19317): java.io.FileNotFoundException: /data/data/com.sriram.hellomediaplayer/files/word_recorded.wav: open failed: EACCES (Permission denied) 
10-27 10:23:31.650: W/System.err(19317): at libcore.io.IoBridge.open(IoBridge.java:406) 
10-27 10:23:31.650: W/System.err(19317): at java.io.FileInputStream.<init>(FileInputStream.java:78) 
10-27 10:23:31.650: W/System.err(19317): at android.app.ContextImpl.openFileInput(ContextImpl.java:610) 
10-27 10:23:31.660: W/System.err(19317): at android.content.ContextWrapper.openFileInput(ContextWrapper.java:159) 
10-27 10:23:31.660: W/System.err(19317): at com.sriram.hellomediaplayer.HelloMediaPlayer.onCreate(HelloMediaPlayer.java:65) 
10-27 10:23:31.660: W/System.err(19317): at android.app.Activity.performCreate(Activity.java:4465) 
10-27 10:23:31.660: W/System.err(19317): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049) 
10-27 10:23:31.660: W/System.err(19317): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920) 
10-27 10:23:31.670: W/System.err(19317): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981) 
10-27 10:23:31.670: W/System.err(19317): at android.app.ActivityThread.access$600(ActivityThread.java:123) 
10-27 10:23:31.670: W/System.err(19317): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147) 
10-27 10:23:31.670: W/System.err(19317): at android.os.Handler.dispatchMessage(Handler.java:99) 
10-27 10:23:31.670: W/System.err(19317): at android.os.Looper.loop(Looper.java:137) 
10-27 10:23:31.670: W/System.err(19317): at android.app.ActivityThread.main(ActivityThread.java:4424) 
10-27 10:23:31.670: W/System.err(19317): at java.lang.reflect.Method.invokeNative(Native Method) 
10-27 10:23:31.680: W/System.err(19317): at java.lang.reflect.Method.invoke(Method.java:511) 
10-27 10:23:31.680: W/System.err(19317): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 
10-27 10:23:31.680: W/System.err(19317): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 
10-27 10:23:31.680: W/System.err(19317): at dalvik.system.NativeStart.main(Native Method) 
10-27 10:23:31.680: W/System.err(19317): Caused by: libcore.io.ErrnoException: open failed: EACCES (Permission denied) 
10-27 10:23:31.690: W/System.err(19317): at libcore.io.Posix.open(Native Method) 
10-27 10:23:31.690: W/System.err(19317): at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110) 
10-27 10:23:31.690: W/System.err(19317): at libcore.io.IoBridge.open(IoBridge.java:390) 
10-27 10:23:31.690: W/System.err(19317): ... 18 more 
10-27 10:23:31.700: V/[email protected](19317): Data source set to: /data/data/com.sriram.hellomediaplayer/files/word_recorded.wav 
10-27 10:23:31.700: V/[email protected](19317): Audio stream for mp set. 
10-27 10:23:31.700: I/MediaPlayer(19317): ======>prepare 
10-27 10:23:31.700: I/MediaPlayer(19317): ======>prepareAsync_l 
10-27 10:23:31.720: E/MediaPlayer(19317): error (1, -2147483648) 
10-27 10:23:31.720: W/System.err(19317): java.io.IOException: Prepare failed.: status=0x1 
10-27 10:23:31.720: W/System.err(19317): at android.media.MediaPlayer.prepare(Native Method) 
10-27 10:23:31.720: W/System.err(19317): at com.sriram.hellomediaplayer.HelloMediaPlayer.onCreate(HelloMediaPlayer.java:113) 
10-27 10:23:31.720: W/System.err(19317): at android.app.Activity.performCreate(Activity.java:4465) 
10-27 10:23:31.720: W/System.err(19317): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049) 
10-27 10:23:31.720: W/System.err(19317): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920) 
10-27 10:23:31.720: W/System.err(19317): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981) 
10-27 10:23:31.720: W/System.err(19317): at android.app.ActivityThread.access$600(ActivityThread.java:123) 
10-27 10:23:31.720: W/System.err(19317): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147) 
10-27 10:23:31.720: W/System.err(19317): at android.os.Handler.dispatchMessage(Handler.java:99) 
10-27 10:23:31.730: W/System.err(19317): at android.os.Looper.loop(Looper.java:137) 
10-27 10:23:31.730: W/System.err(19317): at android.app.ActivityThread.main(ActivityThread.java:4424) 
10-27 10:23:31.730: W/System.err(19317): at java.lang.reflect.Method.invokeNative(Native Method) 
10-27 10:23:31.730: W/System.err(19317): at java.lang.reflect.Method.invoke(Method.java:511) 
10-27 10:23:31.730: W/System.err(19317): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 
10-27 10:23:31.730: W/System.err(19317): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 
10-27 10:23:31.740: W/System.err(19317): at dalvik.system.NativeStart.main(Native Method) 
10-27 10:23:31.820: D/libEGL(19317): loaded /system/lib/egl/libGLES_android.so 
10-27 10:23:31.830: D/libEGL(19317): loaded /system/lib/egl/libEGL_VIVANTE.so 
10-27 10:23:31.840: D/libEGL(19317): loaded /system/lib/egl/libGLESv1_CM_VIVANTE.so 
10-27 10:23:31.860: D/libEGL(19317): loaded /system/lib/egl/libGLESv2_VIVANTE.so 
10-27 10:23:31.900: D/OpenGLRenderer(19317): Enabling debug mode 0 

我迄今爲止嘗試:
1.更改權限與file.setReadable內部存儲的文件。該命令未能設置WORLD_READABLE權限。
2.試圖將其複製到另一個文件中,該文件是專門用WORLD_READABLE and WORLD_WRITEABLE權限創建的。奇怪的是,儘管前面的文字說文件存在,但我仍收到一個FileNotFound錯誤。
3.關於word_recorded.wav的權限表明該文件是可讀的,但程序指示了其他情況。
4.拉出wav文件並在我的系統上播放。它發揮得很好。
5.通過FileDescriptor程序。這也沒有奏效。

我的問題:
1.我哪裏錯了?我如何完成這項工作?

+0

文件如何進入應用程序數據目錄?爲什麼不簡單地把它放在原始資源目錄中?如果你真的希望它能從它的位置開始工作,當你通過adb shell查看文件時,所有者,組和權限是什麼? – Dave

+0

該文件由TTS引擎生成。我不能將它放在原始資源目錄中,因爲它不是靜態的 - 每當用戶決定讓TTS引擎說出某些內容時,文件word.wav就會被覆蓋。每個人的文件權限都是'rw'。 – Sriram

+0

我也看到了這個(在GT-I9300-SDK15上),但帶有一個XML文件。但是,這是非常罕見的。也許有一次成千上萬的用戶。 –

回答

1
10-27 10:23:31.680: W/System.err(19317): Caused by: libcore.io.ErrnoException: open failed: EACCES (Permission denied) 

您是否有權限從內部存儲中讀取文件?

+0

和那是什麼權限? – Sriram

+2

android.permission.READ_EXTERNAL_STORAGE也許? – R00t

+0

從內部存儲器讀取時不需要任何權限。內部存儲是專用於每個應用程序的專用存儲區域。 「Read_External_Storage」是指SD卡等。 – Sriram