Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

App Windows crashes when doing build "flutter build windows" #455

Closed
tonyhoang2712 opened this issue Dec 29, 2024 · 1 comment
Closed

App Windows crashes when doing build "flutter build windows" #455

tonyhoang2712 opened this issue Dec 29, 2024 · 1 comment

Comments

@tonyhoang2712
Copy link

tonyhoang2712 commented Dec 29, 2024

Package version
record: ^5.2.0

Environment

  • OS: [Windows]

Describe the bug

[ERROR:flutter/shell/common/shell.cc(1065)] The 'com.llfbandit.record/eventsRecord/44bafddf-6f0a-445d-a813-b94d10e70258' channel sent a message from native to Flutter on a non-platform thread. Platform channel messages must be sent on the platform thread. Failure to do so may result in data loss or crashes, and must be fixed in the plugin or application code creating that channel.
See https://docs.flutter.dev/platform-integration/platform-channels#channels-and-platform-threading for more information.

To Reproduce

I implemented audio streaming using the record package: ^5.2.0.

Step 1: run "flutter pub add record"
Step 2: When I run "flutter run -d windows" it seems to work fine.
Step 3: When I run "flutter build windows", the app crashes.
When I revisited Step 2, I noticed the following error:

[ERROR:flutter/shell/common/shell.cc(1065)] The 'com.llfbandit.record/eventsRecord/44bafddf-6f0a-445d-a813-b94d10e70258' channel sent a message from native to Flutter on a non-platform thread. Platform channel messages must be sent on the platform thread. Failure to do so may result in data loss or crashes, and must be fixed in the plugin or application code creating that channel.
See https://docs.flutter.dev/platform-integration/platform-channels#channels-and-platform-threading for more information.

I found that this is the cause of the crash when running 'flutter build windows'.
Do you know how I can fix this issue?

Here is the source code I used.

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

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  runApp(const MyApp());
}

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

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

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


  @override
  _AudioRecorderExampleState createState() => _AudioRecorderExampleState();
}

class _AudioRecorderExampleState extends State<AudioRecorderExample> {
  final AudioRecorder _record = AudioRecorder();
  bool _isRecording = false;
  String? _recordedFilePath;

  Future<void> startRecording() async {
    if (await _record.hasPermission()) {
      debugPrint('Permission access');
    } else {
      debugPrint('Permission denied');
    }
    await _record.start(
        RecordConfig(
            encoder: AudioEncoder.pcm16bits, numChannels: 1, sampleRate: 8000),
        path: 'C:/Zensho/ACCSWatcherV2/myFile.m4a');
    setState(() {
      _isRecording = true;
    });

    debugPrint('Recording started');
    try {
      final stream = await _record.startStream(RecordConfig(
          encoder: AudioEncoder.pcm16bits, numChannels: 1, sampleRate: 8000));

      stream.listen((data) {
        debugPrint('Stream data length: ${data.length}');
      });

      debugPrint('Streaming started');
    } catch (e) {
      debugPrint('Streaming error $e');
    }
  }

  Future<void> stopRecording() async {
    if (_isRecording) {
      final path = await _record.stop();
      setState(() {
        _isRecording = false;
        _recordedFilePath = path;
      });
      debugPrint('Recording stopped. File saved at: $path');
    }
  }

  Future<void> startStreaming() async {}

  @override
  void dispose() {
    _record.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Audio Recorder Example')),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            ElevatedButton(
              onPressed: _isRecording ? stopRecording : startRecording,
              child: Text(_isRecording ? 'Stop Recording' : 'Start Recording'),
            ),
            const SizedBox(height: 16),
            if (_recordedFilePath != null) ...[
              const SizedBox(height: 16),
              Text('Recorded File: $_recordedFilePath'),
            ],
          ],
        ),
      ),
    );
  }
}

Thank you.

@tonyhoang2712 tonyhoang2712 changed the title record app crash when build windows App Windows crashes when doing build "flutter build windows" Dec 29, 2024
@llfbandit
Copy link
Owner

Thanks for reporting.
Duplicates #379

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants