On Thu, Aug 6, 2009 at 6:31 AM, JAG <[email protected]> wrote:

>
> What is the preferred way of reusing a MediaPlayer when using
> resources?
> My try looks a bit ... too complicated... so maybe I should be happy
> to see that it didn't work.
> (I run the Android 1.5 SDK and emulator.)
>
> According to this thread my problem may be a permission problem.
>
> http://groups.google.se/group/android-developers/browse_thread/thread/5569a88c50dcc043?pli=1
>
> I get the same error. But in my case it only occurs when I use
> FileDescriptor!?!
> The use case differs however, because I use a resource stored in the
> bundle.
> I have s0.mp3 safely stored in the project /res/raw and it
> successfully plays when I use SoundPool or MediaPlayer.create. And the
> log print tells me I referenced the correct file and stay safely in my
> package.
>
> I'm sure my way of getting the fd out from a package resource is the
> problem, I didn't find any other way...


You need to use the version of setDataSource() that takes a FileDescriptor,
offset, and length.

>
> But why didn't setDataSource throw the exception instead of a
> printout?


Because setDataSource() doesn't parse the file, so the player engine is only
finding out that it can't play your apk file after you call prepare().

And why on earth is the same resource happily played one way but not
> the other? Seems like a design flaw rather than a security raise.


There is no design flaw. The issue is that you were only using the
FileDescriptor that you obtained from the AssetFileDescriptor that
openRawResource() returns. That file descriptor is for the entire apk file,
whereas you only want to play a specific part of that apk file.

Should I look in the  MediaPlayer.create code to see how they get
> around the problem or can you help me understand how it works?
>
>
> This is a test code snippet: (from onCreate of a simple Activity)
>
>    soundPlayer = new SoundPool(1, AudioManager.STREAM_NOTIFICATION,
> 0);
>
>    FileDescriptor fd = getResources().openRawResourceFd
> (R.raw.s0).getFileDescriptor();
>    Log.d("SoundTest", getResources().getResourceName(R.raw.s0));
>
>    mediaPlayer = new MediaPlayer();
>    try {
>      mediaPlayer.setDataSource(fd);
>      mediaPlayer.prepare();
>    } catch (Exception e) {
>      Log.e("SoundTest","Error! " + Log.getStackTraceString(e));
>    }
>
>      mediaPlayer = MediaPlayer.create(this, R.raw.s0);
>      if (mediaPlayer != null) {
>        mediaPlayer.start();
>      }
>
>      soundPlayer.play(sounds[key], 1, 1, 1, 0, 2);
>
>
> And the error:
>
> PlayerDriver: Command PLAYER_SET_DATA_SOURCE completed with an error
> or info PVMFErrNotSupported
> MediaPlayer:  error (1, -4)
> SoundTest:    Error! java.io.IOException: Prepare failed.: status=0x1
>   at android.media.MediaPlayer.prepare(Native Method)
>
>
> Thanks for any insight shared!
>  JAG
>
> >
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to