将打字稿翻译成 javascript
translate typescript to javascript
我需要这个代码在Javascript
我不是语言打字专家。
这是使用 nativescript 在移动设备中播放音乐和录制音频的代码
没想到GitHub快给答案
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);
}
}
您可以使用 typescript playground 从 typescript 即时转换为 javascript。
这里是urlhttp://www.typescriptlang.org/play
或者你可以在你的机器上安装 typescript 编译器和 运行 tsc .
如果您使用 Webpack 或任何 Angular (2+) 种子项目,甚至 angular-cli,您所有的 TypeScript 代码都会 "compiled" 到 ECMAScript,您可以选择版本(从 5 到 7)。
只需打开 tsconfig.json。 target 将提供您需要的 ECMAScript 的具体版本。
"compilerOptions": {
"outDir": "./dist/out-tsc",
"baseUrl": "src",
"sourceMap": true,
"declaration": false,
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"target": "es5",
"typeRoots": [
"node_modules/@types"
]
运行 build
并从 outDir 目录中获取 JavaScript 文件。
您可以使用 TypeScript 编译器 (tsc) 来执行此操作。对于您的代码片段,输出是这样的:
"use strict";
exports.__esModule = true;
var nativescript_audio_1 = require("nativescript-audio");
var YourClass = (function () {
function YourClass() {
var _this = this;
this._player = new nativescript_audio_1.TNSPlayer();
this._player.initFromFile({
audioFile: '~/audio/song.mp3',
loop: false,
completeCallback: this._trackComplete.bind(this),
errorCallback: this._trackError.bind(this)
}).then(function () {
_this._player.getAudioTrackDuration().then(function (duration) {
// iOS: duration is in seconds
// Android: duration is in milliseconds
console.log("song duration:", duration);
});
});
}
YourClass.prototype.togglePlay = function () {
if (this._player.isAudioPlaying()) {
this._player.pause();
}
else {
this._player.play();
}
};
YourClass.prototype._trackComplete = function (args) {
console.log('reference back to player:', args.player);
// iOS only: flag indicating if completed succesfully
console.log('whether song play completed successfully:', args.flag);
};
YourClass.prototype._trackError = function (args) {
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);
};
return YourClass;
}());
exports.YourClass = YourClass;
我需要这个代码在Javascript
我不是语言打字专家。
这是使用 nativescript 在移动设备中播放音乐和录制音频的代码
没想到GitHub快给答案
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);
}
}
您可以使用 typescript playground 从 typescript 即时转换为 javascript。 这里是urlhttp://www.typescriptlang.org/play
或者你可以在你的机器上安装 typescript 编译器和 运行 tsc .
如果您使用 Webpack 或任何 Angular (2+) 种子项目,甚至 angular-cli,您所有的 TypeScript 代码都会 "compiled" 到 ECMAScript,您可以选择版本(从 5 到 7)。
只需打开 tsconfig.json。 target 将提供您需要的 ECMAScript 的具体版本。
"compilerOptions": {
"outDir": "./dist/out-tsc",
"baseUrl": "src",
"sourceMap": true,
"declaration": false,
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"target": "es5",
"typeRoots": [
"node_modules/@types"
]
运行 build
并从 outDir 目录中获取 JavaScript 文件。
您可以使用 TypeScript 编译器 (tsc) 来执行此操作。对于您的代码片段,输出是这样的:
"use strict";
exports.__esModule = true;
var nativescript_audio_1 = require("nativescript-audio");
var YourClass = (function () {
function YourClass() {
var _this = this;
this._player = new nativescript_audio_1.TNSPlayer();
this._player.initFromFile({
audioFile: '~/audio/song.mp3',
loop: false,
completeCallback: this._trackComplete.bind(this),
errorCallback: this._trackError.bind(this)
}).then(function () {
_this._player.getAudioTrackDuration().then(function (duration) {
// iOS: duration is in seconds
// Android: duration is in milliseconds
console.log("song duration:", duration);
});
});
}
YourClass.prototype.togglePlay = function () {
if (this._player.isAudioPlaying()) {
this._player.pause();
}
else {
this._player.play();
}
};
YourClass.prototype._trackComplete = function (args) {
console.log('reference back to player:', args.player);
// iOS only: flag indicating if completed succesfully
console.log('whether song play completed successfully:', args.flag);
};
YourClass.prototype._trackError = function (args) {
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);
};
return YourClass;
}());
exports.YourClass = YourClass;