MrJai / flutter_to_airplay

Flutter plugin that offers two widgets, one to play a video for a given URL or file path using native AVPlayer and second with an option to airplay it on available Apple devices.
MIT License
30 stars 24 forks source link

App crashes on loading the page second time with AirPlayRoutePickerView() Widget #8

Closed JigarKotak7 closed 4 years ago

JigarKotak7 commented 4 years ago

Hi,

First of all thanks a lot for creating this package (turned out to be exactly what I wanted to finish my project) however I am having this issue where when I load the page with AirPlayRoutePickerView() Widget the first time it shows up but if I pop back and try to open the page again, app crashes, it happen every time.

Also the app crashes when the Widget is added by itself, I mean I had to put a boolean and do setState by putting Delay & checking if the player is initialized and playing OR I used your example code to check Platform Exception. But still it just the first time it shows up whenever I pop back and load the page again app would crash.

Any help would be appreciated.

Thank you Jigar Kotak

JigarKotak7 commented 4 years ago

I have found a workaround but you can find a proper solution to it, I was going through the code and using breakpoints realized that the first time I build AirPlayRoutePickerView() Widget it builds it properly and when I try to build it second time it was trying to build FlutterAVPlayer() Widget instead and it was null so the app was crashing. So in the below file you see if viewId == 1 it build the AirPlayRoutePickerView() else it would build FlutterAVPlayer() Widget and when I try to load the page second time the viewId is incremented, for now I have changed it to if viewId >= 1 and it works for my purpose but you can implement a proper fix for it.

Thank you Jigar Kotak

// // SharePlatformViewFactory.swift // flutter_to_airplay // // Created by Junaid Rehmat on 22/08/2020. // import Foundation import Flutter

class SharePlatformViewFactory: NSObject, FlutterPlatformViewFactory { var _messenger : FlutterBinaryMessenger

init(messenger: FlutterBinaryMessenger & NSObjectProtocol) {
    _messenger = messenger
}

func create(withFrame frame: CGRect, viewIdentifier viewId: Int64, arguments args: Any?) -> FlutterPlatformView {
    if viewId == 1 {
        let pickerView = FlutterRoutePickerView(frame: frame, viewIdentifier: viewId, arguments: args as! Dictionary<String, Any>, binaryMessenger: _messenger)
        return pickerView
    }
    else
    {
        let pickerView = FlutterAVPlayer(frame: frame, viewIdentifier: viewId, arguments: args as! Dictionary<String, Any>, binaryMessenger: _messenger)
        return pickerView
    }
}

func createArgsCodec() -> FlutterMessageCodec & NSObjectProtocol {
    return FlutterStandardMessageCodec.sharedInstance()
}

}

MrJai commented 4 years ago

Thank you @JigarKotak7, for using my package and appreciating my efforts. You also really cut short my effort by debugging and identifying the issue with viewId. I have pushed a permanent fix and publishing an update to pub.dev as well. Please confirm when you can so that I can close this issue.