LiveMediaPublisher
API Reference: UI.LiveMediaPublisher
LiveMediaPublisher is a live streaming publisher. It is used for audio and video playback in RTMP/RTMPT/RTSP/HTTP/TCP/UDP/FILE format.
Original iOS library: https://github.com/NodeMedia/NodeMediaClient-iOS
Original Android library: https://github.com/NodeMedia/NodeMediaClient-Android
You should add camera and microphone permission to workspace/config/iOS/Info.plist
<key>NSCameraUsageDescription</key>
<string>Camera usage description</string>
<key>NSMicrophoneUsageDescription</key>
<string>Microphone usage description</string>
You have to edit your AndroidManifest.xml file under workspace/config/Android.\ Add below lines
<uses-feature android:name="android.hardware.camera"/>
<uses-feature android:name="android.hardware.camera.autofocus"/>
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.FLASHLIGHT"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
You should ensure that required permissions are granted before starting live streaming. Please visit this guide. It helps you to request permissions at runtime.
You can listen the changes using onChange callback. Events are listed below.
LiveMediaPublisher Events
- 2000: Connecting to server
- 2001: Connect successfully and start streaming
- 2002: Streaming failed
- 2003: Connecting to server
- 2004: Streaming ended
- 2005: Network anomaly
- 2100: Network congestion
- 2101: Network recovery, smooth release
The components in the example are added from the code for better showcase purposes. To learn more about the subject you can refer to:
Adding Component From CodeAs a best practice, Smartface recommends using the WYSIWYG editor in order to add components and styles to your page or library. To learn how to use UI Editor better, please refer to this documentation
UI Editor Basics- Page UI
- Page.ts
{
"components": [
{
"className": ".page",
"id": "7217-e1e8-2b27-3fd5",
"initialized": true,
"props": {
"children": [
"9e06-5a62-c5ae-1020",
"5417-cebd-8cb0-7525",
"d52d-9531-149a-7e98",
"0990-1183-785d-3eca",
"8087-565d-44e6-b0b1"
],
"name": "pgLiveMediaPublisher",
"orientation": "PORTRAIT",
"parent": null
},
"type": "Page",
"userProps": {},
"version": "0.0.1.a"
},
{
"className": ".statusBar",
"id": "9e06-5a62-c5ae-1020",
"props": {
"children": [],
"isRemovable": false,
"name": "statusBar",
"parent": "7217-e1e8-2b27-3fd5"
},
"type": "StatusBar",
"userProps": {}
},
{
"className": ".headerBar",
"id": "5417-cebd-8cb0-7525",
"props": {
"children": [],
"isRemovable": false,
"name": "headerBar",
"parent": "7217-e1e8-2b27-3fd5",
"title": "LiveMediaPublisher"
},
"type": "HeaderBar",
"userProps": {
"title": "LiveMediaPublisher"
}
},
{
"className": ".flexLayout",
"id": "d52d-9531-149a-7e98",
"oldName": "flLiveMediaPlayer",
"props": {
"children": [],
"name": "flLiveMediaPublisher",
"parent": "7217-e1e8-2b27-3fd5",
"usePageVariable": true
},
"type": "FlexLayout",
"userProps": {
"testId": "HTr6MvpkD",
"usePageVariable": true
}
},
{
"className": ".button",
"id": "0990-1183-785d-3eca",
"oldName": "button1",
"props": {
"children": [],
"name": "btnAction",
"parent": "7217-e1e8-2b27-3fd5",
"text": "Start Streaming",
"usePageVariable": true
},
"type": "Button",
"userProps": {
"testId": "UZyRbzYL43",
"text": "Start Streaming",
"usePageVariable": true
}
},
{
"className": ".button",
"id": "8087-565d-44e6-b0b1",
"oldName": "btnPause",
"props": {
"children": [],
"name": "btnCamera",
"parent": "7217-e1e8-2b27-3fd5",
"text": "Switch Camera",
"usePageVariable": true
},
"type": "Button",
"userProps": {
"testId": "7pd13al_Wz",
"text": "Switch Camera",
"usePageVariable": true
}
}
]
}
import PgLiveMediaPublisherDesign from "generated/pages/pgLiveMediaPublisher";
import { withDismissAndBackButton } from "@smartface/mixins";
import { Router, Route } from "@smartface/router";
import LiveMediaPublisher from "@smartface/native/ui/livemediapublisher";
import Color from "@smartface/native/ui/color";
import { ScaleType } from "@smartface/native/ui/livemediaplayer/livemediaplayer";
import { styleableComponentMixin } from "@smartface/styling-context";
class StyleableLiveMediaPublisher extends styleableComponentMixin(
LiveMediaPublisher
) {}
export default class PgLiveMediaPublisher extends withDismissAndBackButton(
PgLiveMediaPublisherDesign
) {
private liveMediaPublisher: StyleableLiveMediaPublisher;
private _streaming = false;
constructor(private router?: Router, private route?: Route) {
super({});
this.btnAction.on("press", () => this.startStreaming());
this.btnCamera.on("press", () => this.switchCamera());
}
switchCamera() {
this.liveMediaPublisher?.switchCamera();
}
startStreaming() {
this._streaming = !this._streaming;
this._streaming
? this.liveMediaPublisher?.start()
: this.liveMediaPublisher?.stop();
this.btnAction.text = this._streaming
? "Stop Streaming"
: "Start Streaming";
}
/**
* To test media publisher we need to create a rtmp server in our local network.
* https://github.com/aler9/rtsp-simple-server
*
* Streaming
*
* Install the server above to your pc. Make sure your pc and phone is on the same network.
* Update the outputUrl below with your pc ip address
*
* Watching
*
* To watch it you can use VLC Media Player
* Open VLC Player, select Media on top left and click Open Network Stream
* Write down this address rtmp://localhost/mystream
* Make sure stream path is same as in outputUrl (mystream).
*/
initLiveMediaPublisher() {
this.liveMediaPublisher;
this.liveMediaPublisher = new StyleableLiveMediaPublisher({
flexGrow: 1,
backgroundColor: Color.BLACK,
onChange: (params) => {
console.info(params.message, params);
},
outputUrl: "rtmp://192.168.1.106/mystream",
});
this.flLiveMediaPublisher.addChild(
this.liveMediaPublisher,
"liveMediaPublisher",
".grow-relative"
);
this.liveMediaPublisher.startPreview();
}
onShow() {
super.onShow();
this.initBackButton(this.router);
}
onLoad() {
super.onLoad();
this.initLiveMediaPublisher();
}
}