将打字稿翻译成javascript

时间:2017-06-06 10:56:27

标签: javascript typescript nativescript translate

我需要此代码在 Javascript

我不擅长语言打字稿。

这是使用nativescript播放音乐和录制音频的代码

不要认为 GitHub 快速给出答案

in github issues

import { TNSPlayer } from 'nativescript-audio';

export class YourClass {
    private _player: TNSPlayer;

    constructor() {
        this._player = new TNSPlayer();
        this._player.initFromFile({
            audioFile: '~/audio/song.mp3', // ~ = app directory
            loop: false,
            completeCallback: this._trackComplete.bind(this),
            errorCallback: this._trackError.bind(this)
        }).then(() => {

            this._player.getAudioTrackDuration().then((duration) => {
                // iOS: duration is in seconds
                // Android: duration is in milliseconds
                console.log(`song duration:`, duration);
            });
        });
    }

    public togglePlay() {
        if (this._player.isAudioPlaying()) {
            this._player.pause();
        } else {
            this._player.play();
        }
    }

    private _trackComplete(args: any) {
        console.log('reference back to player:', args.player);

        // iOS only: flag indicating if completed succesfully
        console.log('whether song play completed successfully:', args.flag);
    }

    private _trackError(args: any) {
        console.log('reference back to player:', args.player);
        console.log('the error:', args.error);

        // Android only: extra detail on error
        console.log('extra info on the error:', args.extra);
    }
}

3 个答案:

答案 0 :(得分:3)

您可以使用TypeScript编译器(tsc)来执行此操作。对于您的代码段,输出为:

qTimer = new QTimer(0);

connect(qTimer, SIGNAL(timeout()), this, SLOT(RestartStoppedProcess()), Qt::DirectConnection);
connect(qTimer, SIGNAL(destroyed()), qTimer, SLOT(deleteLater()), Qt::DirectConnection);

qTimer->setTimerType(Qt::VeryCoarseTimer);
qTimer->start(5000);


void ProcessMonitor::RestartStoppedProcess()
{
    try
    {
        QProcess *objMonitorProcess = new QProcess(this);
        connect(objMonitorProcess, SIGNAL(finished(int,QProcess::ExitStatus)), objMonitorProcess, SLOT(deleteLater()));
        for(int i = 0; i < ui->twShowProcess->rowCount(); i++)
        {
#ifdef Q_WS_LIN
            //            objMonitorProcess->start("pidof", QStringList() << (ui->twShowProcess->item(i, 0)->text()), QIODevice::ReadOnly);
            objMonitorProcess->start("pidof "+ (ui->twShowProcess->item(i, 0)->text()), QIODevice::ReadOnly);
#elif Q_WS_WIN
            QString cmd("wmic process where ");
            QString qszFilters("\"processid=\'");
            QString qszSubFilters = qszFilters.append(ui->twShowProcess->item(i, 1)->text().append("\'").append("\"").append(" get name"));
            cmd.append(qszSubFilters);
            objMonitorProcess->start(cmd.toStdString().c_str(), QIODevice::ReadOnly);
#else
            QString qszProcessName = ui->twShowProcess->item(i, 0)->text().split(".").takeFirst();
            objMonitorProcess->start("pgrep", QStringList() << qszProcessName, QIODevice::ReadOnly);
#endif
            if((objMonitorProcess->waitForStarted(4000) == true) && (objMonitorProcess->state() == QProcess::Running))
            {
                if(objMonitorProcess->waitForFinished(3000) == false)
                    continue;
                else
                {
                    QString qszProcessStatus = ui->twShowProcess->item(i, 3)->text();
                    if(qszProcessStatus.compare("Running") == 0)
                    {
                        QString qszStandardOutput, qszStandardError;
                        qszStandardOutput.clear(); qszStandardError.clear();
                        qszStandardOutput = QString(objMonitorProcess->readAllStandardOutput());
                        qszStandardError = QString(objMonitorProcess->readAllStandardError());
                        if(qszStandardError.simplified().compare("No Instance(s) Available.", Qt::CaseInsensitive) == 0 && qszStandardOutput.simplified().isEmpty() == true)
                        {
                            ui->twShowProcess->selectRow(i);
                            if(ui->twShowProcess->selectedItems().at(0)->text().compare("LTtagdb.exe", Qt::CaseInsensitive) == 0 ||
                                    ui->twShowProcess->selectedItems().at(0)->text().compare("LTmessagebroker.exe", Qt::CaseInsensitive) == 0)
                                this->StopAllApplicationsForcefully();
                            else
                                this->on_actionStop_triggered();

                            if(this->ptrobjContextMenu->isVisible())
                                this->ptrobjContextMenu->close();

                            ui->twShowProcess->clearSelection();

                            if(mobj_ProcessLogMap.mobj_ProcessAndLogMapping.contains(ui->twShowProcess->item(i, 8)->text()) == true)
                            {
                                mobj_ProcessLogMap.mobj_ProcessAndLogMapping.remove(ui->twShowProcess->item(i, 8)->text());
                                mobj_ProcessLogMap.mobj_ProcessAndLogMapping.insert(ui->twShowProcess->item(i, 8)->text(), QString(""));
                            }
                            else
                                this->mobj_ProcessLogMap.mobj_ProcessAndLogMapping.insert(ui->twShowProcess->item(i, 8)->text(), QString(""));
                        }

                        if(ui->twShowProcess->item(i, 0)->text() == "LThistdb.exe")
                        {
                            if(IsServiceRunning(QString("LThistdb.exe")) == false)
                                this->on_actionStopPostgreSQL_DB_Server_triggered();
                        }
                        qszStandardOutput.clear();
                    }
                }
            }
            else
                break;
#ifdef Q_WS_WIN
            cmd.clear();
            qszFilters.clear();
            qszSubFilters.clear();
#endif
        }
    }
    catch(QProcess::ProcessError pError)
    {
        QMessageBox objCrashExit;
        objCrashExit.information(this, "Crashed", "Process error returned code: " + QString::number(pError));
    }
    catch(...)
    {
        QMessageBox objCrashExit;
        objCrashExit.information(this, "Unknown Crash", "Unknown crash exit-child process may not be running correctly!");
    }
}

答案 1 :(得分:2)

您可以使用typescript playground从typescript到javascript的即时转换。 这是网址http://www.typescriptlang.org/play

或者您可以在机器中安装typescript编译器并运行tsc。

答案 2 :(得分:1)

如果U R使用webpack或任何角度(2+)种子项目,甚至是angular-cli,那么所有UR打字稿代码都将被编译并且#34;到ES和U可以选择哪一个(从5到7)。

只需打开tsconfig.json, &#34;目标&#34;将给出ES U需要的具体版本,

 "compilerOptions": {
"outDir": "./dist/out-tsc",
"baseUrl": "src",
"sourceMap": true,
"declaration": false,
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"target": "es5",
"typeRoots": [
  "node_modules/@types"
],

运行&#34;构建&#34;并从outDir获取UR JS文件