tav-media
Version:
Cross platform media editing framework
97 lines (96 loc) • 3.65 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import { tav } from '../tav';
import { makeClipAssetPath } from '../utils/clip-asset-path';
import { Asset } from './tav-asset';
/**
* AudioAsset is an asset that can be used to play audio.
* @hideconstructor
* @category Assets
*/
export class AudioAsset extends Asset {
constructor() {
super(...arguments);
/**
* Returns `'AudioAsset'`
*/
this.type = 'AudioAsset';
this.audioDuration = 0;
this.clipId = '';
}
/**
* Creates a TAVMedia Audio object from a specified range of a audio file, return null if the file does
* not exist or it's not a valid audio file.
* @param path The path of the audio file.
* @param duration Externally specified duration of the file, the default value 0 means to use the
* actual duration
*/
static MakeFromPath(path, duration = 0) {
return __awaiter(this, void 0, void 0, function* () {
const audioAsset = new AudioAsset(null, path);
audioAsset.audioDuration = duration;
yield audioAsset.prepare();
const asset = yield audioAsset.build();
if (!asset) {
audioAsset.release();
return null;
}
return audioAsset;
});
}
/**
* @ignore
*/
$getCopyOrSelfWillBeUsedByClip(clip) {
return __awaiter(this, void 0, void 0, function* () {
if (!this.clipId) {
this.clipId = clip.id;
return this;
}
const clipPath = makeClipAssetPath(clip.id, this.path);
const assetClone = yield AudioAsset.MakeFromPath(clipPath);
yield assetClone.build();
return assetClone;
});
}
doPrepare() {
const _super = Object.create(null, {
doPrepare: { get: () => super.doPrepare }
});
return __awaiter(this, void 0, void 0, function* () {
yield _super.doPrepare.call(this);
if (tav.PLATFORM === 'wx' || tav.PLATFORM === 'browser') {
try {
yield tav.TAVMovieAudioReader.preload(this.path);
}
catch (e) {
console.error('audio load failed', this.path);
}
}
});
}
/**
* Returns the duration of this asset.
*/
get duration() {
var _a;
return ((_a = this.nativeAsset) === null || _a === void 0 ? void 0 : _a.duration()) || this.audioDuration || 0;
}
createAsset() {
return __awaiter(this, void 0, void 0, function* () {
if (!this.path)
return undefined;
if (tav.PLATFORM === 'wx' || tav.PLATFORM === 'browser') {
return tav.AudioAsset.MakeFromUrl(this.path, this.audioDuration);
}
return tav.AudioAsset.MakeFromPath(this.path, this.audioDuration);
});
}
}