一切都尝试了,但无法找到任何解决方案。请提供 ionic 5 的工作示例
问问题
2018 次
1 回答
3
在安卓中工作
创建 IONIC 项目
ionic start myApp blank
在Index.html添加两个脚本
<script src="https://rtcmulticonnection.herokuapp.com/dist/RTCMultiConnection.min.js"></script>
<script src="https://rtcmulticonnection.herokuapp.com/socket.io/socket.io.js"></script>
添加两个 Cordova 插件
https://github.com/patwaswapnil/cordova-opentok-android-permissions
ionic cordova plugin add cordova-opentok-android-permissions
这个插件将为android添加一些权限
https://ionicframework.com/docs/native/diagnostic
转到上面的链接以获取有关诊断的更多详细信息
ionic cordova plugin add cordova.plugins.diagnostic
npm install @ionic-native/diagnostic
app.module.ts
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { RouteReuseStrategy } from '@angular/router';
import { IonicModule, IonicRouteStrategy } from '@ionic/angular';
import { SplashScreen } from '@ionic-native/splash-screen/ngx';
import { StatusBar } from '@ionic-native/status-bar/ngx';
import { AppComponent } from './app.component';
import { AppRoutingModule } from './app-routing.module';
import { Diagnostic } from '@ionic-native/diagnostic/ngx';
@NgModule({
declarations: [AppComponent],
entryComponents: [],
imports: [BrowserModule, IonicModule.forRoot(), AppRoutingModule],
providers: [
StatusBar,
SplashScreen,
Diagnostic,
{ provide: RouteReuseStrategy, useClass: IonicRouteStrategy }
],
bootstrap: [AppComponent]
})
export class AppModule {}
主页.html
<ion-header [translucent]="true">
<ion-toolbar>
<ion-title>
Blank
</ion-title>
</ion-toolbar>
</ion-header>
<ion-content id="myContent" [fullscreen]="true">
<button (click)="join()">Join</button>
<br>
<br>
</ion-content>
主页.ts
使用此插件需要相机和麦克风权限
await this.diagnostic.requestCameraAuthorization()
await this.diagnostic.requestMicrophoneAuthorization()
import { Component } from '@angular/core';
import { Diagnostic } from '@ionic-native/diagnostic/ngx';
import { Platform } from '@ionic/angular';
declare var RTCMultiConnection;
@Component({
selector: 'app-home',
templateUrl: 'home.page.html',
styleUrls: ['home.page.scss'],
})
export class HomePage {
connection: any;
constructor(private diagnostic: Diagnostic,
private platform: Platform,) {
}
ngAfterViewInit() {
console.log('ngAfterViewInit');
this.platform.ready().then(() => {
console.log('ready');
this.getPermission();
});
}
getPermission() {
console.log('getPermission');
this.diagnostic.requestCameraAuthorization()
.then((res) => {
console.log('res 0');
console.log(res);
return this.diagnostic.requestMicrophoneAuthorization()
})
.then((res) => {
console.log('res');
console.log(res);
}).catch((err) => {
console.log('err', err);
}).finally(() => {
console.log('finally');
this.webrtc();
});
}
join() {
var predefinedRoomId = 'test123';
this.connection.openOrJoin(predefinedRoomId);
}
webrtc() {
let content = document.querySelector('#myContent') as HTMLElement;
console.log('ngAfterViewInit');
this.connection = new RTCMultiConnection();
console.log('connection', this.connection);
// this line is VERY_important
this.connection.socketURL = 'https://rtcmulticonnection.herokuapp.com:443/';
// all below lines are optional; however recommended.
this.connection.session = {
audio: true,
video: true
};
this.connection.onMediaError = function (error) {
console.log('error', error);
console.log('error', JSON.stringify(error));
};
this.connection.sdpConstraints.mandatory = {
OfferToReceiveAudio: true,
OfferToReceiveVideo: true
};
this.connection.onstream = function (event) {
console.log('onstream', event);
content.appendChild(event.mediaElement);
};
}
}
于 2020-06-27T13:39:34.587 回答