Bug 5889 - Android.Media.MediaPlayer issue with resources
Summary: Android.Media.MediaPlayer issue with resources
Alias: None
Product: Android
Classification: Xamarin
Component: General ()
Version: 4.2.x
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Bugzilla
Depends on:
Reported: 2012-06-27 14:01 UTC by Goncalo Oliveira
Modified: 2012-09-19 09:48 UTC (History)
2 users (show)

Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Goncalo Oliveira 2012-06-27 14:01:02 UTC
Android.Media.MediaPlayer fails when trying to play from a resource.

I tried both Create() and SetDataSource methods.
Resource.Raw.alarm_67 is set as AndroidResource.
The file format is mp3.

Fail scenario 1:

Android.Media.MediaPlayer player = Android.Media.MediaPlayer.Create( Application.Context, Resource.Raw.alarm_67560 );
player.Completion += new EventHandler( player_Completion );
player.Prepare(); // IllegalStateException here

Fail scenario 2:

Android.Media.MediaPlayer player = new Android.Media.MediaPlayer();
player.SetDataSource( Resources.OpenRawResourceFd( Resource.Raw.alarm_67560 ) );
player.Completion += new EventHandler( player_Completion );

this scenario fails on SetDataSource().

Success scenario 1 - Uri to physical path (SD card)
Success scenario 2 - Uri to physical path (external storage)
Success scenario 3 - Uri to http address
Success scenario 4 - Assets.OpenFd( fileName ).FileDescriptor
Comment 1 Atsushi Eno 2012-09-19 09:36:56 UTC
I believe this is wrong API usage. Here is what I observed:

Scenario 1:

It seems you cannot call Prepare() when you use MediaPlayer (Context, int) constructor.

Scenario 2:

You cannot pass AndroidAssetFileDescriptor to MediaPlayer.SetDataSource(). When I passed "Resources.OpenRawResourceFd( Resource.Raw.some_my_resource ).FileDescriptor" , it just worked.
Comment 2 Goncalo Oliveira 2012-09-19 09:48:05 UTC
public static MediaPlayer create (Context context, Uri uri)

Since: API Level 1
Convenience method to create a MediaPlayer for a given Uri. On success, prepare() will already have been called and MUST NOT be called again.


public AssetFileDescriptor.FileDescriptor getFileDescriptor ()

Since: API Level 1
Returns the FileDescriptor that can be used to read the data in the file.


Thank you for the feedback.