2.0.2 • Published 6 years ago

react-native-hue-player v2.0.2

Weekly downloads
3
License
ISC
Repository
github
Last release
6 years ago

react-native-hue-player

HUE Player is intended to projects that need to deal with both offline/local and online/streaming audio. Now you can play, pause, skip and seek audios from diferent sources on the same playlist.

This project is based on 'ract-native-audio-streamer', 'react-native-sound' and uses 'react-native-music-control'.

print

Getting Started

In order to use this component you have to set up a playlist of audio files. Those audio files must have some attributes, as shown on the schema bellow:

const playlistSample = [
  {key: 'audio01', title:'Irineu', url: 'http://vocenaosabe.nem/eu.mp3'}, 
  {key: 'audio02', title:'SerjaoBerranteiro', url: 'http://aquitem.corage', path: 'matadorDeOnca.mp3'}
];

Note that the audio objects may have other fields, but "key", "title" and "url" are required. By default the first track to be played is the first one on playlist array.

Installing

Install on your project root using:

yarn add react-native-hue-player

or

npm install react-native-hue-player

This component is based on three other projects, so make sure you run the commands to link Android and iOS.

react-native link react-native-audio-streamer
react-native link react-native-sound
react-native link react-native-music-control

Usage

You have to list your audio files on your playlist array, as showed before. The 'url' field is meant for streaming audio files, while the 'path' field is for the local files.

To handle local files you need to follow some rules defined on the React Native Sound documentation:

Save your sound clip files under the directory android/app/src/main/res/raw. Note that files in this directory must be lowercase and underscored (e.g. my_file_name.mp3) and that subdirectories are not supported by Android.

Now, all you need to control your audio files is provided by the AudioController class, but if you want to use or customize our ready-to-go interface you can import the AudioControls component.

AudioControls

Basic usage:

import { AudioControls } from 'react-native-hue-player';

...
<AudioControls
  initialTrack={1} // starts on second audio file
  playlist={playlistSample}
/>

Props:

NameTypeDefaultDescription
activeColorcolor'#FFF'Main color of the compontent, used as default when buttons are active
inactiveColorcolor'#888'Secondary color of the compontent, used as default when buttons are inactive/disabled
hasButtonSkipSecondsbooleanfalseDoes you player has buttons to skip secods?
timeToSkipinteger15How many seconds you want your skip on your buttons
thumbnailSizeimage style props-Size of the cover thumbnail
titleStyletext style props-Custom style for the audio title
authorStyletext style props-Custom style for the author name
activeButtonColorcoloractiveColorCustom color for the buttons (play, pause, skip...) when they are active
inactiveButtonColorcolorinactiveColorCustom color for the buttons (play, pause, skip...) when they are inactive/disabled
sliderMinimumTrackTintColorcoloractiveColorCustom color on the left side of the slider
sliderMaximumTrackTintColorcoloractiveColorCustom color on the right side of the slider
sliderThumbTintColorcoloractiveColorCustom color of the slider circle

AudioController

If you don't want to use our component you are free to implement one of your own. The AudioController is a singleton class that provides data from the current audio, including its status, and functions to manipulate the playlist and to control the player.

To use the AudioController class just import it on your component (if you do it in more than one component it will be the same instance).

import { AudioController } from 'react-native-hue-player';

...
function wheneverYouNeed(){
  AudioController.init(playlistSample, initialTrack, this.onChangeStatus, this.updateCurrentTime);
}

Possible status: PLAYING, LOADING, LOADED, PAUSED, STOPPED, SEEKING, ERROR.

FunctionParamsDescriptions
initplaylist: array, track: integer, onChangeStatus: function, onChangeCurrentTime: functionInitiate the audioController with the given playlist. Has optional initial track and two callback functions, called when the audio status changes and every second when currentTime changes.
loadaudio: object, isLoaded: functionLoads an audio file and returns a callback when its loaded.
playnonePlays the 'currentAudio', defined with load function
pausenonePauses the 'currentAudio
seekseconds: integerSeeks to the target second.
skipseconds: integerJumps to the position resulting from currentTime + seconds.
hasTrackindex: integerChecks if the target index exists in playlist.
hasNextnoneChecks if there is an audio on next position of the playlist.
hasPreviousnoneChecks if there is an audio on previous position of the playlist.
playNextnonePlays the audio on next position of the playlist.
playPreviousnonePlays the audio on previous position of the playlist.
playAnotherTrackindex: integerPlays the audio on target position of the playlist.

Example

The project is linked already.