Skip to content
This repository was archived by the owner on Mar 24, 2025. It is now read-only.
This repository was archived by the owner on Mar 24, 2025. It is now read-only.

Exception on loading from byte data #126

@S-Man42

Description

@S-Man42

I get following exception, when I try to load an audio file from byte data:

I/AudiofileplayerPlugin( 4354): onMethodCall: method = load
E/MediaPlayerNative( 4354): error (1, -2147483648)
E/flutter ( 4354): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: PlatformException(AudioPluginError, Could not create ManagedMediaPlayer:Prepare failed.: status=0x1, null, null)
E/flutter ( 4354): #0      StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:607:7)
E/flutter ( 4354): #1      MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:167:18)
E/flutter ( 4354): <asynchronous suspension>
E/flutter ( 4354): #2      Audio._sendMethodCall (package:audiofileplayer/audiofileplayer.dart:751:7)
E/flutter ( 4354): <asynchronous suspension>
E/flutter ( 4354): #3      Audio._load (package:audiofileplayer/audiofileplayer.dart:433:7)
E/flutter ( 4354): <asynchronous suspension>
E/flutter ( 4354): 

Could you please have a look if I am using the plugin correctly? I am not quite sure about it.

My Widget:

class GCWSoundPlayer extends StatefulWidget {
  final PlatformFile file;
  final bool showMetadata;

  const GCWSoundPlayer({Key key, @required this.file, this.showMetadata: false}) : super(key: key);

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

class _GCWSoundPlayerState extends State<GCWSoundPlayer> {

  Audio audio;
  var isPlaying = false;
  var isLoaded = false;

  double totalSeconds;
  double currentPosition;

  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    audio
      ..pause()
      ..dispose();
    super.dispose();
  }

  _audioPause() {
    if (audio == null)
      return;

    audio.pause();
    isPlaying = false;
  }

  _onDurationSet(double durationSeconds) {
    totalSeconds = durationSeconds;
    setState(() {
      isLoaded = true;
    });

    audio.play();
    isPlaying = true;
  }

  _onPositionUpdated(double positionSeconds) {
    currentPosition = positionSeconds;
    setState(() {});
  }

  _audioLoad() {
    audio = Audio.loadFromByteData(widget.file.bytes.buffer.asByteData(),
        // // Called when audio has finished playing.
        // onComplete: () => _handleOnComplete(),
        // Called when audio has loaded and knows its duration.
        onDuration: (double durationSeconds) => _onDurationSet(durationSeconds),
        // Called repeatedly with updated playback position.
        onPosition: (double positionSeconds) => _onPositionUpdated(positionSeconds)
    );
  }

  _audioPlay() {
    if (!isLoaded) {
      _audioLoad();
    } else {
      audio.resume();
      isPlaying = true;
    }
  }

  _audioStop() {
    if (audio == null)
      return;

    audio.pause();
    audio.dispose();

    isPlaying = false;
    isLoaded = false;
  }

  @override
  Widget build(BuildContext context) {
    return Row(
      children: [
        GCWIconButton(
          iconData: isPlaying ? Icons.pause : Icons.play_arrow,
          onPressed: isPlaying
              ? () => _audioPause()
              : () => _audioPlay(),
        ),
        GCWIconButton(
          iconData: Icons.stop,
          onPressed: () => _audioStop()
        ),
        if (widget.showMetadata)
        Expanded(
          child: Container(
              child: Column(
                children: [
                  Row(
                    children: [
                      Expanded(
                        child: GCWText(
                            text: widget.file.name,
                            style: gcwTextStyle()
                                .copyWith(fontWeight: FontWeight.bold, color: themeColors().dialogText())),
                      ),
                    ],
                  )
                ],
              ),
              color: themeColors().accent(),
              padding: EdgeInsets.all(DOUBLE_DEFAULT_MARGIN)),
        ),
       Expanded(
         child: GCWSlider(value: 0.0, min: 0.0, max: totalSeconds ?? 100.0, suppressReset: true),
       ),
       GCWText(text: _durationText())
      ],
    );
  }

  _durationText() {
    var total = '--:--';
    if (totalSeconds != null && totalSeconds >= 0) {
      var min = (totalSeconds / 60).floor();
      var sec = (totalSeconds % 60).floor();
      total = min.toString().padLeft(2, '0') + ':' + sec.toString().padLeft(2, '0');
    }

    var position = '--:--';
    if (currentPosition != null && currentPosition >= 0) {
      var min = (currentPosition / 60).floor();
      var sec = (currentPosition % 60).floor();
      position = min.toString().padLeft(2, '0') + ':' + sec.toString().padLeft(2, '0');
    }

    return '$position / $total';
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions