Live Streaming in Swift

2020-07-22 19:17发布

问题:

I need to create a live streaming application in SWIFT

Tried many ways using AVFoundation framework (AVPLayer & AVPlayeritem) but didn't work

It's audio only so I don't want to do it in the webview way.

Thanks in advance

回答1:

You might try leveraging the VideoCore library that wraps these frameworks into a complete streaming package. With this package, you can readily start publishing with little effort. Take the following for consideration (replicated from their example projects):

To initiate a connection (Obj-C):

VCSimpleSession *sess = [[VCSimpleSession alloc] initWithVideoSize:CGSizeMake(width, height) frameRate:frameRate bitrate:bitrate] useInterfaceOrientation:YES];
sess.delegate = self;

To initiate a connection (Swift):

var sess:VCSimpleSession = VCSimpleSession(videoSize: CGSize(width: width, height: height), frameRate: frameRate, bitrate: bitRate, useInterfaceOrientation: false)
sess.delegate = self;

Utilize the event handler (Obj-C):

- (void) connectionStatusChanged:(VCSessionState) state{
   if(state==VCSessionStateStarting){
      // connecting to destination host
   }
   else if(state==VCSessionStateStarted){
     // connected, streaming has begun
   }
   // ... etc
}

Utilize the event handler (Swift):

func connectionStatusChanged(sessionState: VCSessionState) {
        switch session.rtmpSessionState {
        case .Starting:
            // initiating connection

        case .Started:
            // connected

        default:
           // connect
        }
    }

Alternatively, there are paid SDK's out there that you can get support on like Wowza's GoCoder SDK. A slew more options and a stable infrastructure.

Thanks

Matt