Where To Put Media Resource And Start Media Player?
I want to play a song in a Xamarin app using the standard android media player. I've got two questions that i haven't been able to answer myself: Here do I put the media file (.m
Solution 1:
I tried saving it in the raw-folder, but that doesn't seem to work
You can't directly use string "Ressources.raw.test" as the DataSource
. If you need play a song from Resource/Raw
folder, here is two solutions.
Solution 1:
You could try using MediaPlayer.Create(Context context, int resid) method:
Convenience method to create a MediaPlayer for a given resource id. On success, prepare() will already have been called and must not be called again.
Usage:
MediaPlayerplayer= MediaPlayer.Create(this, Resource.Raw.test);
player.Start();
Solution 2:
Convert your Resource/Raw/test
path to Android.Net.Uri
:
player = newMediaPlayer();
Android.Net.Uriuri= Android.Net.Uri.Parse("android.resource://" + PackageName + "/" + Resource.Raw.test);
player.SetDataSource(this, uri2);
player.Prepare();
player.Start();
how do I link to the file (it's also saved on my SD-Card)
Answer :
Try using SetDataSource (Context context, Uri uri) method, for example:
player = new MediaPlayer();
//My file path is Path == file:///storage/emulated/0/netease/cloudmusic/Music/love.MP3var uri = Android.Net.Uri.Parse("file://" + Android.OS.Environment.ExternalStorageDirectory + "/netease/cloudmusic/Music/love.MP3");
player.SetDataSource(this, uri);
player.Prepare();
player.Start();
Post a Comment for "Where To Put Media Resource And Start Media Player?"