initialize method

Future<void> initialize()

Initialize and prepare the audio file to play

Must be used before any other interactions with the object

Implementation

Future<void> initialize() async {
  try {
    String? fileId;
    if (_dataSource == _VIAudioFileDataSource.file) {
      fileId = await _methodChannel
          .invokeMethod('AudioFile.initWithFile', <String, dynamic>{
        'name': name,
        'type': type,
        'usage': _audioFileUsageToString(_usage),
      });
    } else if (_dataSource == _VIAudioFileDataSource.network) {
      fileId = await Voximplant._channel
          .invokeMethod('AudioFile.loadFile', <String, dynamic>{
        'url': url,
        'usage': _audioFileUsageToString(_usage),
      });
    }
    if (fileId == null) {
      _VILog._e('VIAudioFile: initialize: fileid was null, skipping');
      throw VIAudioFileError.ERROR_INTERNAL;
    }
    _fileId = fileId;
    _eventSubscription =
        EventChannel('plugins.voximplant.com/audio_file_events_$_fileId')
            .receiveBroadcastStream()
            .listen((event) {
      if (event['name'] == 'didStopPlaying') {
        onStopped?.call(event['error']);
      }
    });
  } on PlatformException catch (e) {
    throw VIException(e.code, e.message);
  } catch (e) {
    rethrow;
  }
}