Baseflow / XamarinMediaManager

Cross platform Xamarin plugin to play and control Audio and Video
https://baseflow.com
MIT License
768 stars 305 forks source link

Cant play file thats stored locally? #840

Closed jimdog1001 closed 2 years ago

jimdog1001 commented 2 years ago

I am trying to use this package to play mp3 files that are stored locally. I am using await CrossMediaManager.Current.PlayFromResource("assets:///SaintsVsDemons.mp3"). The file is stored under android in the assets folder and the build action is AndroidAsset. No matter what I try, however, it will not play the local file. Is this a bug? Please help.

4or5trees commented 2 years ago

Hi @jimdog1001, were you able to fix this issue?

I am having the same problem.

This line properly downloads and plays the file (and the mediaItem variable has valid metadata):

IMediaItem mediaItem = await CrossMediaManager.Current.Play("https://ia800806.us.archive.org/15/items/Mp3Playlist_555/AaronNeville-CrazyLove.mp3");

But the line below does nothing and the resulting mediaItem variable has no valid metadata and the Duration property is zero across the board. The long-test MP3 file itself is valid (duration is 1 min 44 secs)

IMediaItem mediaItem = await CrossMediaManager.Current.PlayFromResource("assets:///long-test.mp3");

If I change the filename to a non-existing resource, I get a NullReferenceException. So it does seem to be able to find the long-test.mp3 file

jimdog1001 commented 2 years ago

Hows it going,

I did end up finding the solution, for Android anyways, I haven't tried ios.

1) make sure your file build action is set as AndroidAsset, if you need help with this let me know

2) even tho this isn't what the instructions say, don't use playfromresource(). You need to use crossMediaManage.currebt.Play(file)

3) you might already have this but file name needs to be "file:///android_asset/"filename".mp3

Hope this helps, lmk if you have questions

Thanks,

James

On Tue, Nov 30, 2021, 6:23 PM 4or5trees @.***> wrote:

Hi @jimdog1001 https://github.com/jimdog1001, were you able to fix this issue?

I am having the same problem.

This line properly downloads and plays the file (and the mediaItem variable has valid metadata):

IMediaItem mediaItem = await CrossMediaManager.Current.Play("https://ia800806.us.archive.org/15/items/Mp3Playlist_555/AaronNeville-CrazyLove.mp3");

But the line below does nothing and the resulting mediaItem variable has no valid metadata and the Duration property is zero across the board. The long-test MP3 file itself is valid (duration is 1 min 44 secs)

IMediaItem mediaItem = await CrossMediaManager.Current.PlayFromResource("assets:///long-test.mp3");

If I change the filename to a non-existing resource, I get a NullReferenceException. So it does seem to be able to find the long-test.mp3 file

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/Baseflow/XamarinMediaManager/issues/840#issuecomment-983118475, or unsubscribe https://github.com/notifications/unsubscribe-auth/AVURRKVUAR36P5DWZFTUDELUOVMHLANCNFSM5FJQIHKQ . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

4or5trees commented 2 years ago

@jimdog1001 Thank you so much! You have resolved my issue. (and so quickly too!)

The full working line is now:

IMediaItem mediaItem = await CrossMediaManager.Current.Play("file:///android_asset/long-test.mp3");
jimdog1001 commented 2 years ago

Fantastic. No problem! That problem bugged me for a bit haha. Cause they say to use playfromresource but it jjst doesn't work for some reason. Glad you got it working.

James

On Tue, Nov 30, 2021, 6:53 PM 4or5trees @.***> wrote:

@jimdog1001 https://github.com/jimdog1001 Thank you so much! You have resolved my issue. (and so quickly too!)

The full working line is now:

IMediaItem mediaItem = await CrossMediaManager.Current.Play("file:///android_asset/long-test.mp3");

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/Baseflow/XamarinMediaManager/issues/840#issuecomment-983132806, or unsubscribe https://github.com/notifications/unsubscribe-auth/AVURRKSIQ4ERHDCWA7QROXTUOVPXVANCNFSM5FJQIHKQ . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

drmaven commented 2 years ago

Do you have a solution for iOS as well? Duration doesn't work in iOS either, but the file plays normally

DaLegacy commented 1 year ago

Is it possible you can go more in-depth with whats the point of this? -> IMediaItem mediaItem = await CrossMediaManager.Current.Play("file:///android_asset/long-test.mp3"); I get metadata, I don't need metadata I need a video to play on the screen. Is it possible you can post the whole behind code required to get a video to play on the screen with folder structure because nothing I try works...

4or5trees commented 1 year ago

@DaLegacy The line you quoted is the line that plays the audio file. In my case I do not use the IMediaItem variable it returns because all I want is to play the sound file.

Important to note is that I only worked with short sound files and not video files.

That being said, here is the setup I used to get it working:

string content; AssetManager assets = this.Assets; using (StreamReader sr = new StreamReader(assets.Open(jsonFileName))) { content = sr.ReadToEnd(); } // deserialize as desired

- Then in the actual Xamarin.Forms project, I have a bunch of buttons for each sound file
    - Sound file is played on click of the button like this
    ```csharp
     private async void Button_Clicked(object sender, EventArgs e)
     {
        Button clickedButton = (Button)sender;
        string filePath = clickedButton.CommandParameter.ToString();
        IMediaItem mediaItem = await CrossMediaManager.Current.Play($"file:///android_asset/{filePath}");
     }