The SAP Mobile Development Kit (MDK) empowers developers to create cross-platform mobile applications with ease. However, there are times when the standard features of MDK need to be extended to incorporate native functionalities such as audio playback. This is where NativeScript plugins come into play. In this blog post, we will explore how to consume a NativeScript plugin for playing audio/sound within an MDK application on both Android and iOS platforms.
Before we dive into the implementation, ensure you have the following prerequisites:
To add the NativeScript plugin for audio playback in an MDK project, follow these steps:
Install Plugin: Run this command in the terminal within your .mdkproject folder:
npm i nativescript-audio
{
"AppDisplayName": "Base MDK Project",
"AppName": "BaseMDKProject",
"AppVersion": "1.0.0",
"BundleID": "com.sap.csd.mdk.basemdkproject",
"Externals": ["nativescript-audio"],
"NSPlugins": ["nativescript-audio"],
"NPMPlugins": ["nativescript-audio"],
"UrlScheme": "basemdkproject",
}
To expose the audio playback functionality to your MDK app, you need to create an MDK rule that interacts with the NativeScript plugin code. Create a new rule file in your MDK project, e.g., OnPressPlaySound.js.
import { TNSPlayer } from 'nativescript-audio';
const fs = require('@nativescript/core/file-system');
/**
* Play beep sound
*/
export default function OnPressPlaySound(clientAPI) {
const player = new TNSPlayer();
if (clientAPI.nativescript.platformModule.isIOS) { // For iOS
const folder = fs.knownFolders.currentApp();
const file = folder._path.slice(0, -3) + 'audio/beep.mp3';
player.debug = true; // set true to enable TNSPlayer console logs for debugging.
player.initFromFile({
audioFile: file, // ~ = app directory
loop: false,
}).then(() => {
player.getAudioTrackDuration().then((duration) => {
console.log(`song duration:`, duration);
player.play();
});
});
} else if (clientAPI.nativescript.platformModule.isAndroid) { // For Android
const logger = clientAPI.getLogger();
console.log('Current Log Level: ' + logger.getLevel());
player.playFromFile({
audioFile: '~/audio/beep.mp3',
loop: false,
}).then(() => {
console.log('[Audio Player] Done');
}).catch((err) => {
console.log('[Audio Player] Error: ' + err);
});
}
}
To utilize the audio playback functionality defined in OnPressPlaySound.js across different rules in your MDK application, follow these steps:
In the example below, consider the name of the rule as SoundCheck.js.
In your SoundCheck.js rule file, import the OnPressPlaySound function from OnPressPlaySound.js.
import OnPressPlaySound from './OnPressPlaySound';
Wherever audio playback is required within the SoundCheck.js rule, call the imported OnPressPlaySound function.
export default function SoundCheck(clientAPI) {
// Example usage of OnPressPlaySound
OnPressPlaySound(clientAPI);
// Additional logic specific to SoundCheck rule
// ...
}
The overall SoundCheck.js would look like this:
import OnPressPlaySound from './OnPressPlaySound';
/**
* Method to check audio playback
*/
export default function SoundCheck(clientAPI) {
// Example usage of OnPressPlaySound
OnPressPlaySound(clientAPI);
// Additional logic specific to SoundCheck rule
// ...
}
To utilize the audio playback functionality defined in OnPressPlaySound.js directly from a page in your MDK application, follow these steps:
In the example below, consider a page which has a toolbar item.
"ToolBar": {
"Items": [
{
"Position": "Right",
"_Type": "Control.Type.ToolbarItem",
"_Name": "ToolbarItem",
"Caption": "Play audio",
"Enabled": true,
"Visible": true,
"Clickable": true,
"Style": "",
"OnPress": "/BaseMDKProject/Rules/OnPressPlaySound.js"
}
]
}
To play sound in your MDK application, you need to include the audio file (e.g., beep.mp3) within your project. Follow these steps to ensure the audio file is correctly placed:
Binary files such as .mp3 cannot be placed arbitrarily within the MDK project. For instance, placing them inside the metadata folder can lead to project build failures. To avoid such issues, the audio files must be located in a specific directory structure within your project.
Create the App_Resources folder within your .mdkproject and organize the audio files as shown below:
| Platform | Path |
| Android | App_Resources/Android/src/main/assets/app/audio/beep.mp3 |
| iOS | App_Resources/iOS/src/beep.mp3 |
Deploy your MDK app to your desired environment. Make sure to test the audio playback functionality on both Android and iOS devices to ensure compatibility and performance.
tns run android
tns run ios
A customized mobile development client is required for distributing this application on Android and iOS platforms. For more details on how to generate the customized MDK client refer to step 4 in the tutorial: Build Your Mobile Development Kit Client Using Cloud Build Service
Integrating NativeScript plugins into your SAP MDK application allows you to extend the functionality of your mobile apps with native capabilities. By following the steps outlined in this blog post, you can seamlessly integrate audio playback into your MDK app using the nativescript-audio plugin. This approach not only enhances the user experience but also leverages the power of NativeScript to bridge the gap between standard MDK features and native device functionalities.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
| User | Count |
|---|---|
| 136 | |
| 40 | |
| 38 | |
| 19 | |
| 16 | |
| 12 | |
| 12 | |
| 11 | |
| 11 | |
| 9 |