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. aFileDescriptorをプログラムに渡します。これも機能していません。

私の質問:
1. どこが間違っていますか? どうすればこれを機能させることができますか?

4

1 に答える 1