Skip to content

Commit

Permalink
fix: Use unique tmp location for each AudioCache (#1724)
Browse files Browse the repository at this point in the history
# Description

There are conflicts on writing to the same location of the temporary
file path, when loading assets. This should be differentiated anyways as
two individual AudioCaches should not share the same cache location.

Closes #1424
  • Loading branch information
Gustl22 authored Jan 19, 2024
1 parent e4262f4 commit 2333cb7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
14 changes: 12 additions & 2 deletions packages/audioplayers/lib/src/audio_cache.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart' show rootBundle;
import 'package:http/http.dart' as http;
import 'package:path_provider/path_provider.dart';
import 'package:uuid/uuid.dart';

const _uuid = Uuid();

/// This class represents a cache for Local Assets to be played.
///
Expand Down Expand Up @@ -45,7 +48,14 @@ class AudioCache {
/// crucial).
String prefix;

AudioCache({this.prefix = 'assets/'});
/// An unique ID generated for this instance of [AudioCache].
///
/// This is used to load a file into an unique location in the temporary
/// directory.
String? cacheId;

AudioCache({this.prefix = 'assets/', String? cacheId})
: cacheId = cacheId ?? _uuid.v4();

/// Clears the cache for the file [fileName].
///
Expand Down Expand Up @@ -89,7 +99,7 @@ class AudioCache {
final byteData = await loadAsset('$prefix$fileName');

// create a temporary file on the device to be read by the native side
final file = fileSystem.file('${await getTempDir()}/$fileName');
final file = fileSystem.file('${await getTempDir()}/$cacheId/$fileName');
await file.create(recursive: true);
await file.writeAsBytes(byteData.buffer.asUint8List());

Expand Down
16 changes: 15 additions & 1 deletion packages/audioplayers/test/audio_cache_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import 'package:flutter_test/flutter_test.dart';
class FakeAudioCache extends AudioCache {
List<String> called = [];

FakeAudioCache({super.prefix});
FakeAudioCache({super.prefix, super.cacheId});

@override
Future<Uri> fetchToMemory(String fileName) async {
Expand Down Expand Up @@ -55,5 +55,19 @@ void main() {
await cache.clear('audio.mp3');
expect(cache.loadedFiles, <String, Uri>{});
});

test('Use different location for two audio caches', () async {
const fileName = 'audio.mp3';
final cacheA = FakeAudioCache(cacheId: 'cache-path-A');
await cacheA.load(fileName);
expect(cacheA.loadedFiles[fileName]?.path, '//cache-path-A/audio.mp3');

final cacheB = FakeAudioCache(cacheId: 'cache-path-B');
await cacheB.load(fileName);
expect(cacheB.loadedFiles[fileName]?.path, '//cache-path-B/audio.mp3');

await cacheA.clearAll();
await cacheB.clearAll();
});
});
}

0 comments on commit 2333cb7

Please sign in to comment.