audioplayers icon indicating copy to clipboard operation
audioplayers copied to clipboard

File handle is inaccessible after calling release() / Implement ReleaseMode.release

Open Horschig opened this issue 1 year ago • 1 comments

Checklist

  • [X] I read the troubleshooting guide before raising this issue
  • [X] I made sure that the issue I am raising doesn't already exist

Current bug behaviour

When having played an audio file with releasemode release, the file handle is inaccessible e.g. for overwriting the file.

Expected behaviour

Releasing should properly release the file handle, allowing all file access including overwriting the played file.

Steps to reproduce

  1. Execute flutter run on the code audiotestappx.zip
  2. Click on the + button to have the sound file generated in the temporary folder and played.
  3. Wait until sound has played (~4 seconds)
  4. Click on the + button to have the sound file overwritten in the temporary folder. The newly generated file should be played properly.

Code sample

Code sample

In main.dart, call

    ...
    await MyWaveBuilder.exampleAppend();
    final Directory tempDir = await getTemporaryDirectory();
    final String tempFile = '${tempDir.absolute.path}/out.wav';
    MyAudioPlayer.play(tempFile);

MyWaveBuilder:

import 'dart:io';
import 'dart:math';

import 'package:wave_builder/wave_builder.dart';
import 'package:path_provider/path_provider.dart';

class MyWaveBuilder {
  /// Create 1 bar of a 4 random beats
  static Future<void> exampleAppend() async {
    var rng = Random();
    final Directory tempDir = await getTemporaryDirectory();
    final String tempFile = '${tempDir.absolute.path}/out.wav';

    var fileOut = File(tempFile);
    var primary = File('./assets/sounds/primary.wav');
    var secondary = File('./assets/sounds/secondary.wav');
    var silenceType = WaveBuilderSilenceType.BeginningOfLastSample;

    var primaryBytes = await primary.readAsBytes();
    var secondaryBytes = await secondary.readAsBytes();

    await fileOut.create();

    var waveBuilder = WaveBuilder();

    waveBuilder.appendFileContents(primaryBytes);
    waveBuilder.appendSilence(750 - rng.nextInt(500), silenceType);
    waveBuilder.appendFileContents(secondaryBytes);
    waveBuilder.appendSilence(750 - rng.nextInt(500), silenceType);
    waveBuilder.appendFileContents(secondaryBytes);
    waveBuilder.appendSilence(750 - rng.nextInt(500), silenceType);
    waveBuilder.appendFileContents(secondaryBytes);
    waveBuilder.appendSilence(750 - rng.nextInt(500), silenceType);
    await fileOut.writeAsBytes(waveBuilder.fileBytes);
  }
}

MyAudioPlayer:

import 'dart:io';

import 'package:audioplayers/audioplayers.dart';

class MyAudioPlayer {
  static play(String src) async {
    final player = AudioPlayer();
    player.setReleaseMode(ReleaseMode.release);
    await player.play(DeviceFileSource(src));
  }
}

Affected platforms

Windows

Platform details

Windows 11 (10.0.22621)

AudioPlayers Version

^1.1.1

Build mode

debug

Audio Files/URLs/Sources

See here for zipped assets: soundfiles.zip

Cannot upload raw wav files btw:

image

Screenshots

(sorry for the potato language):

image

Logs

my relevant logs
Full Logs
my full logs or a link to a gist

Flutter doctor:

Output of: flutter doctor -v

Related issues / more information

Rebo on Discord thoughtto have solved it with: https://github.com/bluefireteam/audioplayers/pull/1517/files#diff-9bd303c19c9f1e11702b5b6541381f0910cca3c459576aa8a215b0c880a0ab79R342

Working on PR

no way

Horschig avatar Nov 04 '23 19:11 Horschig

Note that we haven't implemented the ReleasMode.release properly, see the parity table. Calling release on the player manually may should work.

This should be easy to implement though.

Gustl22 avatar Nov 07 '23 07:11 Gustl22