0

How can I play a audio file from System.Environment.SpecialFolder.LocalApplicationData in Xamarin Android?

The Error is: Java.IO.IOException: 'setDataSourceFD failed.: status=0x80000000' FD is FileDescriptor? When yes have i to declare it? I've commented him out now, and also in the sample there isn't one....

The file path is consisting of:

string pathToNewFolder = System.Environment.GetFolderPath(System.Environment.SpecialFolder.LocalApplicationData);
            
string file = Path.Combine(pathToNewFolder, Path.GetFileName(url));
 public void PlayAudioFile(string file, int time)
        {
            var player = new MediaPlayer();
            // var fd =  global::Android.App.Application.Context.Assets.OpenFd(file);
            player.Prepared += (s, e) =>
            {
                player.SeekTo(Convert.ToInt32(time));
                player.Start();
            };
            player.Reset();
            player.SetDataSource(file);
            player.Prepare();
        }
       
ToolmakerSteve
  • 18,547
  • 14
  • 94
  • 196
artemis
  • 1
  • 1
  • https://learn.microsoft.com/en-us/xamarin/android/platform/files/#reading-or-writing-to-files-on-internal-storage – Jason Jun 03 '22 at 22:45
  • first, do not stuff a bunch of code in a comment. It is unreadable. [edit] your question instead. Second, tell us the error message, not just the code. I do not know what CS1061 is unless I look it up. Finally, it helps a great deal to tell us which specific line causes the error. – Jason Jun 03 '22 at 22:59
  • It's this line: " var fd = global::Android.App.Application.Context.Assets.OpenFd(file);" And i think the file have to be there because a other funktion store it at the "file" path. – artemis Jun 03 '22 at 23:07
  • does that refer to your original code, or the revised code you tried based on my first comment? Your original code is trying to open an Asset, which is not the same as a file. – Jason Jun 03 '22 at 23:13
  • Sorry for the diffucults, but i am new and not english proofed. I want to play a file that is stored in the Environment.SpecialFolder.LocalApplicationData. – artemis Jun 03 '22 at 23:15
  • see the [sample](https://learn.microsoft.com/en-us/xamarin/android/app-fundamentals/android-audio#initializing-and-playing) - you should be able to pass the file path to `SetDataSource` – Jason Jun 03 '22 at 23:20
  • I edit the question. I get now this error: "Java.IO.IOException: 'setDataSourceFD failed.: status=0x80000000'" Now other have also the error: https://stackoverflow.com/questions/13156980/java-io-ioexception-setdatasource-failed-status-0x80000000 but whitout cleart solution – artemis Jun 03 '22 at 23:28
  • what is the value of `path`? – Jason Jun 03 '22 at 23:29
  • file path is composed, i say in the edited question in which way – artemis Jun 03 '22 at 23:34
  • what is the actual value? If you do `File.Exists(file)` does it return true? – Jason Jun 04 '22 at 02:40
  • Yes, with ConsoleWriteLine it return True – artemis Jun 04 '22 at 02:55
  • The Error is: Java.IO.IOException: 'setDataSource **FD** failed.: status=0x80000000' FD is FileDescriptor? When yes have i to declare it? I've commented him out now, and also in the sample there isn't one.... – artemis Jun 04 '22 at 03:07
  • First, check that the file exists: `File.Exists(file)`. Does that return `true` or `false`? If `false`, then you are attempting to access a file that is not there. – ToolmakerSteve Jun 05 '22 at 00:36
  • It returns: "True" – artemis Jun 05 '22 at 01:15

1 Answers1

0

This could implemented by dependency service.However,this time I play the mp3 file in Assets folder.I think playing it from System.Environment.SpecialFolder.LocalApplicationData folder is quite similar with my solution.Hope it can shed some light on it.

Step 1: Add IAudio interface to your shared project.

    public interface IAudio
    {
        void PlayAudioFile(string fileName);
    }

Step2:In your Android project, add AudioService.cs that implements IAudio interface

[assembly: Dependency(typeof(AudioService))]
namespace AppAudio.Droid
{
    public class AudioService : IAudio
    {
        public AudioService()
        {
        }
        protected MediaPlayer player;
        public void PlayAudioFile(string fileName)
        {
            player = new MediaPlayer();
            var fd = global::Android.App.Application.Context.Assets.OpenFd(fileName);
            player.Prepared += (s, e) =>
            {
                player.Start();
            };
            player.SetDataSource(fd.FileDescriptor, fd.StartOffset, fd.Length);
            player.Prepare();
        }
    }
}

Step3: Call below line to play the mp3 file.

 DependencyService.Get<IAudio>().PlayAudioFile("AllSong.mp3");

Last but not least,don't forget to add below permissions in your manifest file.

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Useful link.

Alexandar May - MSFT
  • 6,536
  • 1
  • 8
  • 15