@voicethread/nativescript-audio-player
Nativescript 应用程序的音频播放器插件。
npm i --save @voicethread/nativescript-audio-player

@voicethread/nativescript-audio-player

Nativescript 音频播放器 apple android

npm

@voicethread/nativescript-audio-player

此插件为 Android 和 iOS 提供音频播放器,支持播放本地文件和远程 URL 音频文件。对于 Android,MediaPlayer 会在首次准备/播放后将远程文件内部缓存。对于 iOS,插件将在首次准备/播放时下载并缓存远程文件。

内容

安装

npm install @voicethread/nativescript-audio-player --save

或者

ns plugin add @voicethread/nativescript-audio-player

用法

了解如何使用此插件的最佳方式是研究本存储库中包含的演示应用程序。您可以通过查看 apps/demo/src/plugin-demos/nativescript-audio-player.ts 了解如何在 TypeScript 应用程序中使用插件。

  1. 导入插件并创建播放器实例。
import { AudioPlayer, AudioPlayerOptions } from '@voicethread/nativescript-audio-player';
this.player = new AudioPlayer();
this.player.on(AudioPlayer.completeEvent, () => {
console.log('playback complete');
});
  1. 播放音频文件。
protected _playOptions: AudioPlayerOptions = {
audioFile: knownFolders.currentApp().path + '/audio/example.m4a';,
loop: false,
audioMixing: false,
completeCallback: async result => {
console.log('AudioPlayer - Audio file playback complete.', result);
},
errorCallback: errorObject => {
console.error('AudioPlayer error!', JSON.stringify(errorObject));
},
infoCallback: infoObject => {
console.info('AudioPlayer info: ', JSON.stringify(infoObject));
},
};
this.player.prepareAudio(this._playOptions).then(status => {
if (status) {
this.player.play();
} else {
console.log('ERROR! Unable to prepare audio!');
}
});

注意:如果您想播放 HTTP URL,您还需要对应用程序进行一些调整以允许不安全连接,否则 URL 访问将无声失败。对于 iOS,请在应用程序的 Info.plist 中添加以下内容

<key>NSAppTransportSecurity</key>  
<dict>
<key>NSAllowsArbitraryLoads</key>
<true />
</dict>

对于 Android,请确保您的应用程序标签在 App_Resources/Android/src/main/AndroidManifest.xml 中具有以下内容

android:usesCleartextTraffic="true"

支持的音频播放器选项

export interface AudioPlayerOptions {
/**
* Gets or sets the audio file url.
*/

audioFile: string;

/**
* Gets or sets the callback when the currently playing audio file completes.
* @returns {Object} An object containing the native values for the callback.
*/

completeCallback?: Function;

/**
* Get or sets the player to loop playback.
*/

loop: boolean;

audioMixing?: boolean;

/**
* Gets or sets the callback when an error occurs with the audio player.
* @returns {Object} An object containing the native values for the error callback.
*/

errorCallback?: Function;

/**
* Gets or sets the callback to be invoked to communicate some info and/or warning about the media or its playback.
* @returns {Object} An object containing the native values for the info callback.
*/

infoCallback?: Function;
}

音频播放器导出

export interface AudioPlayer {
/**
* native instance getters
*/

readonly ios?: any;
readonly android?: any;

/**
* Volume supports values ranging from 0.0 for silence to 1.0 for full volume
*/

volume: any;

/**
* Duration getter in milliseconds
* Returns 0 if there is no audio file loaded
* Returns -1 if there is an issue getting duration (Android)
*/

duration: number;

/**
* Prepare Audio player by preloading an audio file from file oath or URL
* @function prepareAudio
* @param options
*/

prepareAudio(options: AudioPlayerOptions): Promise<boolean>;

/**
* Play current audio file that has been prepared by calling prepareAudio(options)
*/

play(): Promise<boolean>;

/**
* Pauses playing audio file.
*/

pause(): Promise<boolean>;

/**
* Seeks to specific time in ms
*/

seekTo(time: number): Promise<boolean>;

/**
* Releases resources from the audio player.
*/

dispose(): Promise<boolean>;

/**
* Check if the audio is actively playing.
*/

isAudioPlaying(): boolean;

/**
* Get the duration of the audio file playing, in ms (Promise version of duration property getter)
*/

getAudioTrackDuration(): Promise<number>;

/**
* Get the current time position of the audio file playing in ms
*/

readonly currentTime: number;

/**
* Sets the player playback speed rate. On Android this only works on API 23+.
* @param speed [number] - The speed of the playback.
* speed should be a float from 0.0 - X.X, and is a scale factor
*/

changePlayerSpeed(speed: number): void;

/**
* ** iOS ONLY ** - Begins playback at a certain delay, relative to the current playback time.
* @param time [number] - The time to start playing the audio track at.
*/

playAtTime(time: number);
/**
* Events
*/

public static seekEvent = 'seekEvent';
public static pausedEvent = 'pausedEvent';
public static startedEvent = 'startedEvent';
public static completeEvent = 'completeEvent';
public static errorEvent = 'errorEvent'; //will pass the error object
}

辅助工具

  /*
* Utility to find the duration in milliseconds of the mp4 file at `mp4Path`
*/

export function getDuration(mp4Path: string): number;

已在 Android API 25-34 上测试并工作。已在 iOS 12.x-17.x 上测试并工作。

致谢

此插件基于 https://github.com/nstudio/nativescript-audio

许可证

Apache 许可版 2.0