1.1.7 • Published 7 years ago

nativescript-ezaudio v1.1.7

Weekly downloads
2
License
MIT
Repository
github
Last release
7 years ago

alt text

A NativeScript plugin for the simple, intuitive audio framework for iOS. EZAudio

Install

npm install nativescript-ezaudio --save

Usage

IMPORTANT: Make sure you include xmlns:ez="nativescript-ezaudio" on the Page element

// main-page.xml
<Page xmlns="http://schemas.nativescript.org/tns.xsd" 
  xmlns:ez="nativescript-ezaudio"
  loaded="pageLoaded">
  <AbsoluteLayout width="100%" height="100%">
    <ez:AudioPlot 
      class="audioPlot" 
      plotColor="{{audioPlotColor}}" 
      plotType="{{audioPlotType}}" 
      fill="{{audioPlotFill}}" 
      mirror="{{audioPlotMirror}}" 
      bufferData="{{audioPlotBufferData}}" />
    <Button text="{{btnTxt}}" tap="{{toggleCurrentTrack}}" />
  </AbsoluteLayout>
</Page>

// app.css
.audioPlot {
  width:100%;
  height:100%;
  background-color: #000;
  top:0;
  left:0;
}
button {
  font-size: 22;
  horizontal-align: center;
  margin:20px 0;
  color:#FFF803;
  top:20;
  left:0;
  width:100%;
}

// main-page.ts
import {AudioDemo} from "./main-view-model";

function pageLoaded(args) {
  var page = args.object;
  page.bindingContext = new AudioDemo(page);
}
exports.pageLoaded = pageLoaded;

// main-view-model.ts
import {Observable} from 'data/observable';
import {TNSEZAudioPlayer} from 'nativescript-ezaudio';

export class AudioDemo extends Observable {
  public btnTxt: string = 'Play Track';
  
  // AudioPlot
  public audioPlotColor: string = '#FFF803';
  public audioPlotType: string = 'buffer';
  public audioPlotFill: boolean = true;
  public audioPlotMirror: boolean = true;
  public audioPlotBufferData: any;
  
  // internal
  private _player: any;
  private _currentTrackIndex: number = 0;
  private _tracks: Array<string> = [
    `any-mp3-you-like.mp3`,
  ];

  constructor(page: any) {
    super();
    this._player = new TNSEZAudioPlayer(true);
    this._player.delegate().audioEvents.on('audioBuffer', (eventData) => {
      this.set('audioPlotBufferData', {
        buffer: eventData.data.buffer,
        bufferSize: eventData.data.bufferSize
      });
    });
  }

  public toggleCurrentTrack() {
    this._player.togglePlay(this._tracks[this._currentTrackIndex]);
    this.toggleBtn();  
  }
  
  private toggleBtn() {
    this.set(`btnTxt`, `${this._player.isPlaying() ? 'Stop' : 'Play'} Track`);
  }
}

Screenshots

Sample 1Sample 2
Sample1Sample2
Sample 3Sample 4
Sample3Sample4

TNSEZAudioPlayer

AudioPlayer based on EZAudioPlayer.

Creating:

// Option 1: simple
this._player = new TNSEZAudioPlayer();

// Option 2: advanced
// passing true to constructor will let the player know it should emit events
this._player = new TNSEZAudioPlayer(true);

// it allows you to listen to events like so:
this._player.delegate().audioEvents.on('audioBuffer', (eventData) => {
  this.set('audioPlotBufferData', {
    buffer: eventData.data.buffer,
    bufferSize: eventData.data.bufferSize
  });
});

Methods

EventDescription
togglePlay(fileName?: string, reloadTrack?: boolean): voidAllows toggle play/pause on a track as well as reloading the current track or reloading in a new track. First time will always load the track and play. fileName represents the path to the file in your resources. reloadTrack can be used to reload current track or load a new track.
pause(): voidPause track
isPlaying(): booleanDetermine whether player is playing a track
duration(): numberLength in seconds
formattedDuration(): stringFormatted duration in '00:00'
totalFrames: numberTotal number of frames in the loaded track
formattedCurrentTime: stringFormatted current time in '00:00'
setCurrentTime(time: number): voidSet the current time via a frame number
seekToFrame(frame: number): voidSeek playhead to a given frame number
volume(): numberGet the current volume
setVolume(volume: number): voidSet the volume. Must be between 0 - 1.
pan(): numberGet current pan settings
setPan(pan: number): voidSet pan left/right. Must be between -1 (left) and 1 (right). 0 is default (center).
device(): anyGet current output device

Events

EventDescription
audioBufferWhen audio file is playing, get the buffer and bufferSize to set an AudioPlot's bufferData
positionCurrent frame number
reachedEndWhen the end of the file is reached
changeAudioFileWhen the audio file is changed or set
changeOutputWhen the output device is changed
changePanWhen the pan is changed
changeVolumeWhen the volume is changed
changePlayStateWhen the player state changes, ie. play/pause
seekedWhen the audio file has been seeked to a certain frame number

TNSEZRecorder

Recorder based on EZRecorder.

Creating:

this._recorder = new TNSEZRecorder();

// it allows you to listen to events like so:
this._recorder.delegate().audioEvents.on('audioBuffer', (eventData) => {
  this.set('audioPlotBufferData', {
    buffer: eventData.data.buffer,
    bufferSize: eventData.data.bufferSize
  });
});

Methods

EventDescription
record(filePath: string): voidRecord a .m4a file. Pass in an absoulte filePath.
stop(): voidStop recording
isRecording(): booleanDetermine whether recorder is recording
deviceInputs(): Array<any>Collection of input devices
setDevice(device:any): voidSet the input device

Events

EventDescription
audioBufferWhile recording, get the buffer and bufferSize to set an AudioPlot's bufferData
recordTimeCurrent recording time

UI Components

AudioPlot

Displays an audio waveform and provides attributes to modify it's display.

Example:

<ez:AudioPlot plotColor="#fff" plotType="buffer" fill="true" mirror="true" bufferData="{{audioPlotBufferData}}" />

Attributes

PropertyValue
plotColor: stringColor of waveform. Any rgb hex value, ie. #fff
plotType: stringbuffer or rolling
fill: booleanMakes waveform solid with color. When false, it appears more like lines.
mirror: booleanWhether to mirror the waveform top/bottom.
bufferData: ObjectAn Object representing the audio file's buffer and bufferSize. See example implementation

Contributors

Why the TNS prefixed name?

TNS stands for Telerik NativeScript

iOS uses classes prefixed with NS (stemming from the NeXTSTEP days of old): https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/

To avoid confusion with iOS native classes, TNS is used instead.

License

MIT

1.1.7

7 years ago

1.1.6

8 years ago

1.1.5

8 years ago

1.1.4

8 years ago

1.1.3

8 years ago

1.1.2

8 years ago

1.1.1

8 years ago

1.1.0

8 years ago

1.0.2

8 years ago

1.0.1

8 years ago

1.0.0

8 years ago