melihhakanpektas / flutter_midi_pro

MIT License
14 stars 8 forks source link

PlatformException (PlatformException(channel-error, Unable to establish connection on channel., null, null)) - It's not playing #20

Closed marcosarantesj closed 1 month ago

marcosarantesj commented 1 month ago

Exception has occurred. PlatformException (PlatformException(channel-error, Unable to establish connection on channel., null, null))

Code below:

import 'package:flutter/material.dart';
import 'package:flutter_midi_pro/flutter_midi_pro.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      home: ChordPlayerScreen(),
    );
  }
}

class ChordPlayerScreen extends StatefulWidget {
  const ChordPlayerScreen({super.key});

  @override
  // ignore: library_private_types_in_public_api
  _ChordPlayerScreenState createState() => _ChordPlayerScreenState();
}

class _ChordPlayerScreenState extends State<ChordPlayerScreen> {

  final MidiPro midiPro = MidiPro();
  final String _value = 'assets/SalC5Light2.sf2';
  var soundfontId = 0;

  Future<void> load(String asset) async {
    soundfontId = await midiPro.loadSoundfont(path: asset, bank: 0, program: 0); 
  }

  @override
  void initState() {
    //LOAD sf2
    load(_value);
    super.initState();
  }

  //60, 64, 67 C3 E3 G3
  void _play(List<int> midi) {
    int velocity = 60;
    midiPro.playNote(sfId: soundfontId, key: midi[0], velocity: velocity);
    midiPro.playNote(sfId: soundfontId, key: midi[1], velocity: velocity);
    midiPro.playNote(sfId: soundfontId, key: midi[2], velocity: velocity);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Chord Player'),
      ),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          children: [      
            //NOTES
            ElevatedButton(
              onPressed: () {
                _play([60, 64, 67]);
              },
              child: const Text('C'),
            ),
            ElevatedButton(
              onPressed: () {
                _play([62, 66, 69]);
              },
              child: const Text('D'),
            ),
            ElevatedButton(
              onPressed: () {
                _play([64, 68, 71]);
              },
              child: const Text('E'),
            ),    
            IconButton(
              icon: const Icon(Icons.stop),
              onPressed: () {
                // _stop([60, 64, 67]);
              },
            ),
            //   ],
            // ),
          ],
        ),
      ),
    );
  }
}
marcosarantesj commented 1 month ago

Version 2.0.1 works fine. The new version 3 does not.

melihhakanpektas commented 1 month ago

It works as expected when I test it on Android and iOS platforms.

The only weakness of the new Android package is that it does not work on devices with x86 architecture. Normally it is possible to work, but I had to remove it because the package size increased a lot.

If you don’t think there is a problem for this reason I mentioned, don’t forget to keep the version of Flutter and the packages you use up to date. Enter this command using terminal: flutter upgrade && flutter pub upgrade && flutter clean && flutter pub get

marcosarantesj commented 1 month ago

I was using an x86 emulator. I tested it on x64 and it worked. Thanks. It may close the problem.

melihhakanpektas commented 1 month ago

Thank you for your feedbacks 🚀