Skip to content

Commit 302164e

Browse files
committed
feat(iOS): Player using AVAudioPlayer, Recorder using AVAudioRecorder
1 parent 4e7e992 commit 302164e

16 files changed

+443
-229
lines changed

LICENSE

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
The MIT License (MIT)
22

3-
nativescript-yourplugin
4-
Copyright (c) 2016, Your Name
3+
nativescript-audio
4+
Copyright (c) 2016, Brad Martin & Nathan Walker
55

66
Permission is hereby granted, free of charge, to any person obtaining a copy of
77
this software and associated documentation files (the "Software"), to deal in

README.md

+34-29
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
# NativeScript-Audio
2-
NativeScript plugin to play and record audio files.
2+
NativeScript plugin to play and record audio files for Android and iOS.
33

4-
*Currently Android only, iOS is in the works.*
4+
Uses the following native classes:
55

6-
[Android Media Recorder Docs](http://developer.android.com/reference/android/media/MediaRecorder.html)
6+
#### Android
7+
8+
* [Player](http://developer.android.com/reference/android/media/MediaPlayer.html)
9+
* [Recorder](http://developer.android.com/reference/android/media/MediaRecorder.html)
10+
11+
#### iOS
12+
13+
* [Player](https://developer.apple.com/library/ios/documentation/AVFoundation/Reference/AVAudioPlayerClassReference/)
14+
* [Recorder](https://developer.apple.com/library/ios/documentation/AVFoundation/Reference/AVAudioRecorder_ClassReference/)
715

816
## Installation
917
`npm install nativescript-audio`
@@ -12,41 +20,38 @@ NativeScript plugin to play and record audio files.
1220

1321
![AudioExample](screens/audiosample.gif)
1422

15-
1623
## API
1724

18-
#### *Recording*
19-
20-
##### canDeviceRecord() - *Promise*
21-
- retruns: *boolean*
22-
23-
##### startRecorder( { filename: string, errorCallback?: Function, infoCallback?: Function } ) - *Promise*
24-
- returns: *recorder* (android.media.MediaRecorder)
25-
26-
##### stopRecorder(recorder: recorder object from startRecorder)
27-
25+
#### TNSRecorder
2826

29-
##### disposeRecorder(recorder: recorder object from startRecorder)
30-
- *Free up system resources when done with recorder*
27+
Method | Description
28+
-------- | ---------
29+
`TNSRecorder.CAN_RECORD()`: `boolean` | Determine if ready to record.
30+
`start({ filename: string, errorCallback?: Function, infoCallback?: Function })`: `Promise` | Start recording file.
31+
`stop()`: `void` | Stop recording.
32+
`dispose()`: `void` | Free up system resources when done with recorder.
3133

34+
#### TNSPlayer
3235

33-
#### *Playing*
36+
Method | Description
37+
-------- | ---------
38+
`playFromFile( { audioFile: string, completeCallback?: Function, errorCallback?: Function, infoCallback?: Function; } )`: `Promise` | Play from a file.
39+
`playFromUrl( { audioFile: string, completeCallback?: Function, errorCallback?: Function, infoCallback?: Function; } )`: `Promise` | Play from a url.
40+
`pause()`: `void` | Pause playback.
41+
`dispose()`: `void` | Free up resources when done playing audio.
42+
`isAudioPlaying()`: `boolean` | Determine if player is playing.
43+
`getAudioTrackDuration()`: `Promise` | duration of media file assigned to mediaPlayer
3444

35-
##### playFromFile( { audioFile: string, completeCallback?: Function, errorCallback?: Function, infoCallback?: Function; } ) - *Promise*
36-
- returns mediaPlayer (android.media.MediaPlayer)
45+
## Why the TNS prefixed name?
3746

38-
##### playFromUrl( { audioFile: string, completeCallback?: Function, errorCallback?: Function, infoCallback?: Function; } ) - *Promise*
39-
- returns mediaPlayer (android.media.MediaPlayer)
47+
`TNS` stands for **T**elerik **N**ative**S**cript
4048

41-
##### pausePlayer(mediaPlayer) - *Promise*
42-
- return boolean
49+
iOS uses classes prefixed with `NS` (stemming from the [NeXTSTEP](https://en.wikipedia.org/wiki/NeXTSTEP) days of old):
50+
https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/
4351

44-
##### disposePlayer(mediaPlayer)
45-
-- Free up resources when done playing audio with this instance of your mediaPlayer
52+
To avoid confusion with iOS native classes, `TNS` is used instead.
4653

47-
##### isAudioPlaying(mediaPlayer) - *Promise*
48-
- returns boolean
54+
# License
4955

50-
##### getAudioTrackDuration(mediaPlayer) - *Promise*
51-
- returns string - duration of media file assigned to mediaPlayer
56+
[MIT](/LICENSE)
5257

audio.android.ts

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/**
2+
* Option interfaces
3+
*/
4+
export * from './src/options';
5+
16
/**
27
* Player
38
*/

audio.d.ts

-117
This file was deleted.

audio.ios.ts

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/**
2+
* Option interfaces
3+
*/
4+
export * from './src/options';
5+
16
/**
27
* Player
38
*/

demo/app/App_Resources/iOS/Info.plist

+11
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,16 @@
4141
<string>UIInterfaceOrientationLandscapeLeft</string>
4242
<string>UIInterfaceOrientationLandscapeRight</string>
4343
</array>
44+
<key>NSAppTransportSecurity</key>
45+
<dict>
46+
<key>NSAllowsArbitraryLoads</key>
47+
<true />
48+
</dict>
49+
<key>UIBackgroundModes</key>
50+
<array>
51+
<string>audio</string>
52+
</array>
53+
<key>NSMicrophoneUsageDescription</key>
54+
<string>The Audio Recorder needs to access your Microphone to record.</string>
4455
</dict>
4556
</plist>

demo/app/main-page.xml

+35-45
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,47 @@
1-
<Page xmlns="http://schemas.nativescript.org/tns.xsd"
1+
<Page xmlns="http://schemas.nativescript.org/tns.xsd"
22
xmlns:sb="nativescript-statusbar"
3-
loaded="pageLoaded">
3+
loaded="pageLoaded">
44

5-
<sb:StatusBar ios:barStyle="default" android:barStyle="#303F9F" />
5+
<sb:StatusBar ios:barStyle="light" android:barStyle="#303F9F" />
66

77
<Page.actionBar>
88
<ActionBar title="NativeScript Audio" backgroundColor="#3F51B5" color="#fff" />
99
</Page.actionBar>
1010

11-
<TabView color="#f1f1f1" tabsBackgroundColor="#3F51B5" selectedColor="#FF4081">
12-
<TabView.items>
13-
<TabViewItem title="Recorder">
14-
<TabViewItem.view>
15-
<ScrollView>
16-
<stack-layout>
17-
<ActivityIndicator color="#3489db" busy="{{ isRecording }}" />
18-
<button row="0" col="0" class="darkBtn" text="START RECORDING" tap="startRecord" />
19-
<button class="darkBtn" text="STOP RECORDING" tap="stopRecord" />
20-
<button class="darkBtn" text="GET FILE" tap="getFile" />
11+
<TabView color="#f1f1f1" tabsBackgroundColor="#3F51B5" selectedColor="#FF4081" selectedIndexChanged="{{tabChange}}">
12+
<TabView.items>
13+
<TabViewItem title="Recorder">
14+
<TabViewItem.view>
15+
<ScrollView>
16+
<StackLayout>
17+
<ActivityIndicator color="#3489db" busy="{{ isRecording }}" />
18+
<button row="0" col="0" class="darkBtn" text="START RECORDING" tap="{{startRecord}}" />
19+
<button class="darkBtn" text="STOP RECORDING" tap="{{stopRecord}}" />
20+
<button class="darkBtn" text="GET FILE" tap="{{getFile}}" />
2121
<label text="{{ recordedAudioFile }}" class="gray" textWrap="true" />
22-
<button class="darkBtn" text="play recorded" tap="playRecordedFile" />
23-
<button class="darkBtn" text="STOP PLAYING" tap="pauseAudio" />
24-
</stack-layout>
25-
</ScrollView>
26-
</TabViewItem.view>
27-
</TabViewItem>
22+
<button class="darkBtn" text="play recorded" tap="{{playRecordedFile}}" />
23+
<button class="darkBtn" text="STOP PLAYING" tap="{{pauseAudio}}" />
24+
</StackLayout>
25+
</ScrollView>
26+
</TabViewItem.view>
27+
</TabViewItem>
2828

29-
<TabViewItem title="Player">
30-
<TabViewItem.view>
31-
<ScrollView>
32-
<StackLayout>
33-
34-
<!--<button class="darkBtn" text="Play Res Audio" tap="playResFile" />
35-
<label class="gray center" text="In The Night - The Weeknd" textWrap="true" />-->
36-
37-
<button class="darkBtn" text="Play Remote Audio" tap="playRemoteFile" />
38-
<label class="blue center" text="Welcome To Fight Club!" textWrap="true" />
39-
40-
<label text="REMINDER: Remote files can have a lag before playing due to processing and network speeds." class="gray" textWrap="true" />
41-
42-
43-
<button class="darkBtn" text="Play Local File" tap="playLocalFile" />
44-
<label class="blue center" text="Angel - Theory of a Deadman" textWrap="true" />
45-
46-
<button class="darkBtn" text="STOP" tap="stopPlaying" />
47-
48-
</StackLayout>
49-
50-
</ScrollView>
51-
</TabViewItem.view>
52-
</TabViewItem>
29+
<TabViewItem title="Player">
30+
<TabViewItem.view>
31+
<ScrollView>
32+
<StackLayout>
33+
<!--<button class="darkBtn" text="Play Res Audio" tap="{{playResFile}}" />
34+
<label class="gray center" text="In The Night - The Weeknd" textWrap="true" />-->
35+
<button class="darkBtn" text="Play Remote Audio" tap="{{playRemoteFile}}" />
36+
<label class="blue center" text="Welcome To Fight Club!" textWrap="true" />
37+
<label text="REMINDER: Remote files can have a lag before playing due to processing and network speeds." class="gray" textWrap="true" />
38+
<button class="darkBtn" text="Play Local File" tap="{{playLocalFile}}" />
39+
<label class="blue center" text="Angel - Theory of a Deadman" textWrap="true" />
40+
<button class="darkBtn" text="STOP" tap="{{stopPlaying}}" />
41+
</StackLayout>
42+
</ScrollView>
43+
</TabViewItem.view>
44+
</TabViewItem>
5345
</TabView.items>
5446
</TabView>
55-
56-
5747
</Page>

0 commit comments

Comments
 (0)