adrianstevens / Xamarin-Plugins

Cross-platform Plugins for Xamarin, Xamarin.Forms and Windows
https://www.nuget.org/packages/Xam.Plugin.SimpleAudioPlayer/
MIT License
133 stars 53 forks source link

Exception : System.IO.IOException:"The file "C:\Users\HelloWorld\Music\1.wav" is being used by another process, so this process cannot access this file." #77

Closed AtlantisDe closed 3 years ago

AtlantisDe commented 3 years ago

Exception : System.IO.IOException:"The file "C:\Users\HelloWorld\Music\1.wav" is being used by another process, so this process cannot access this file."

//i open this file : 
@"D:\Tmp\Sound\您有新的消息请注意查收.mp3";

//but why, it Open the file :???? 
"C:\Users\HelloWorld\Music\1.wav" 

here is my test code

        var filePath = @"D:\Tmp\Sound\您有新的消息请注意查收.mp3";

            /*test 1*/
            var player = Plugin.SimpleAudioPlayer.CrossSimpleAudioPlayer.Current;
            player.Load(FileToStream1(filePath));
            player.Play();

            /*test 2*/

            var player1 = Plugin.SimpleAudioPlayer.CrossSimpleAudioPlayer.CreateSimpleAudioPlayer();
            player1.Load(FileToStream0(filePath));
            player1.Play();

            /*test 3*/

            var player2 = Plugin.SimpleAudioPlayer.CrossSimpleAudioPlayer.CreateSimpleAudioPlayer();
            player2.Load(FileToStream1(filePath));
            player2.Play();

            var player3 = Plugin.SimpleAudioPlayer.CrossSimpleAudioPlayer.CreateSimpleAudioPlayer();
            player3.Load(filePath);
            player3.Play();

            /*all test is failed*/

``

## test the open stream fun
```c#
     Stream FileToStream0(string fileName)
            {

                try
                {
                    // 打开文件
                    FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);
                    // 读取文件的 byte[]
                    byte[] bytes = new byte[fileStream.Length];
                    fileStream.Read(bytes, 0, bytes.Length);
                    fileStream.Close();
                    // 把 byte[] 转换成 Stream
                    Stream stream = new MemoryStream(bytes);
                    return stream;
                }
                catch (Exception ex)
                {

                    LogsExtenions.ExLog(System.Reflection.MethodBase.GetCurrentMethod().ReflectedType.FullName, ex.Message);
                }
                finally
                {
                }
                return null;
            }

            Stream FileToStream1(string fileName)
            {

                try
                {
                    using (var stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete))
                    {
                        return stream;
                    }

                }
                catch (Exception ex)
                {

                    LogsExtenions.ExLog(System.Reflection.MethodBase.GetCurrentMethod().ReflectedType.FullName, ex.Message);
                }
                finally
                {
                }
                return null;
            }