2

我正在尝试播放内部存储中的文件,MediaPlayer但无法这样做。初始化序列在setDataSource部件上失败。我正在使用的代码:

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/com.sriram.hellomediaplayer.HelloMediaPlayer@410690f0(19317): file name = word_recorded.wav
10-27 10:23:31.640: E/com.sriram.hellomediaplayer.HelloMediaPlayer@410690f0(19317): /data/data/com.sriram.hellomediaplayer/files/word_recorded.wav exists but cannot be read.
10-27 10:23:31.640: E/com.sriram.hellomediaplayer.HelloMediaPlayer@410690f0(19317): Failed to set permissions either.
10-27 10:23:31.650: E/com.sriram.hellomediaplayer.HelloMediaPlayer@410690f0(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/com.sriram.hellomediaplayer.HelloMediaPlayer@410690f0(19317): Data source set to: /data/data/com.sriram.hellomediaplayer/files/word_recorded.wav
10-27 10:23:31.700: V/com.sriram.hellomediaplayer.HelloMediaPlayer@410690f0(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尽管前面几行说文件word_recorded.wav存在,但我还是得到了一个错误。
3. word_recorded.wav 上的权限表明该文件是可读的,但程序表明不可读。
4.把wav文件拉出来在我的系统上播放。它播放得很好。
5.传递FileDescriptor给程序。这也没有奏效。

我的问题:
1. 我哪里出错了?我该如何进行这项工作?

4

1 回答 1

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

您是否有权从内部存储中读取文件?

于 2013-10-27T05:32:29.317 回答