Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cant play file thats stored locally? #840

Closed
jimdog1001 opened this issue Oct 4, 2021 · 7 comments
Closed

Cant play file thats stored locally? #840

jimdog1001 opened this issue Oct 4, 2021 · 7 comments

Comments

@jimdog1001
Copy link

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
Copy link

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
Copy link
Author

jimdog1001 commented Nov 30, 2021 via email

@4or5trees
Copy link

@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
Copy link
Author

jimdog1001 commented Nov 30, 2021 via email

@drmaven
Copy link

drmaven commented Dec 10, 2021

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

@DaLegacy
Copy link

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
Copy link

@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:

  • MyProject.Android contains an Assets folder
    • Contents are
      • sound-files.json
        • Contains relative paths to sound files + metadata in folder sound-files
        • Example path: sound-files/Example/Here/There/my-sound-file.ogg
      • Folder sound-files
        • Contains sub-folders containing short sound files in .ogg format
  • MyProject.Android .csproj includes the JSON file and sound files like this:
<ItemGroup>
  <AndroidAsset Include="Assets\sound-files.json" />
</ItemGroup>
<!-- Example of sound file: -->
<ItemGroup>
    <AndroidAsset Include="Assets\sound-files\Example\Here\There\my-sound-file.ogg" />
</ItemGroup>
  • In MyProject.Android MainActivity.cs I read out the JSON sound files in the OnCreate method
string jsonFileName = "sound-files.json";

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
     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}");
     }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants