Currently trying to use stereo to play background music in my app and I keep receiving issues whenever I call the load(String)
from the Stereo.dart
class.
Whenever I looked further into the issue I figured out that the issue comes directly from the AudioTrack.java
class whenever this line is called
mmr.setDataSource(path);
To replicate the issue, just add this code into the example project.
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:path_provider/path_provider.dart';
import 'package:stereo/stereo.dart';
void main() {
runApp(new MaterialApp(home: new ExampleApp()));
}
class ExampleApp extends StatefulWidget {
@override
_ExampleAppState createState() => new _ExampleAppState();
}
class _ExampleAppState extends State<ExampleApp> {
Stereo _stereo;
@override
initState() {
super.initState();
_stereo = new Stereo();
_playMusic();
}
_playMusic() async {
await _copyFiles();
await _loadMusic();
}
_loadMusic() async {
String dir = '';
String mp3 = "pi.mp3";
await getApplicationDocumentsDirectory().then(
(Directory directory) => dir = 'file://' + directory.path + '/');
try {
await _stereo.load('$dir$mp3');
// _stereo.play();
} on StereoFileNotPlayableException {
var alert = new AlertDialog(
title: new Text('File not playable'),
content: new Text('The file you specified is not playable.'));
showDialog(context: context, child: alert);
}
}
_copyFiles() async {
final Directory dir = await getApplicationDocumentsDirectory();
final File song = new File('${dir.path}/pi.mp3');
if (!(await song.exists())) {
final data = await rootBundle.load('assets/songs/pi.mp3');
final bytes = data.buffer.asUint8List();
await song.writeAsBytes(bytes, flush: true);
}
}
@override
Widget build(BuildContext context) {
return Material(
child: Scaffold(
body: Container(),
),
);
}
}
Flutter --version
Flutter 1.2.2 • channel dev • https://github.com/flutter/flutter.git
Framework • revision 007a415c2a (9 weeks ago) • 2019-02-21 20:22:47 -0800
Engine • revision f1f19bba8f
Tools • Dart 2.2.0 (build 2.2.0-dev.2.1 c92d5ca288)