Search code examples
javascripttypescriptnativescripttranslate

translate typescript to javascript


I need this code is in Javascript

I am not specialized in language typescript.

this is code for play music and record audio in mobile with nativescript

do not think GitHub quickly give answer

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);
    }
}

Solution

  • If you use Webpack or any Angular (2+) seed project, even angular-cli, all your TypeScript code will "compiled" to ECMAScript and you can choose the version (from 5 to 7).

    Just open tsconfig.json. target will give a concrete version of ECMAScript you need.

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

    Run build and get your JavaScript file from the outDir directory.