Creating a Music App using Angular + Ionic + Capacitor is a great way to build a cross-platform mobile app (iOS, Android, and Web) with a single codebase. Below is a step-by-step guide to help you create a basic music player app.
🧰 Prerequisites
Make sure you have the following installed:
- Node.js (>=14.x)
- npm / yarn
- Android Studio or Xcode (for native builds)
Install Ionic CLI and Capacitor globally:
npm install -g @ionic/cli capacitor
🚀 Step 1: Create an Ionic Angular Project
ionic start musicApp blank --type=angular
cd musicApp
Select y when asked to integrate Capacitor for native functionality.
📱 Step 2: Add Capacitor Platforms
Add iOS and/or Android support:
npx cap add ios
npx cap add android
You can also run the web version in the browser:
ionic serve
🎵 Step 3: Implement Music Player UI
1. Generate Pages/Components
Let's create a page for the music player:
ionic generate page pages/music-player
Update src/app/app-routing.module.ts:
const routes: Routes = [
{
path: '',
redirectTo: '/music-player',
pathMatch: 'full'
},
{
path: 'music-player',
loadChildren: () => import('./pages/music-player/music-player.module').then(m => m.MusicPlayerPageModule)
}
];
🎶 Step 4: Basic Audio Playback
We'll use the HTML5 <audio> tag for simple playback. For advanced features like background audio, media notifications, etc., we’ll use Capacitor plugins later.
Update music-player.page.html
<ion-header>
<ion-toolbar>
<ion-title>My Music App</ion-title>
</ion-toolbar>
</ion-header>
<ion-content [fullscreen]="true">
<div class="player">
<h2>{{ currentTrack?.title }}</h2>
<ion-img [src]="currentTrack?.artwork" alt="Artwork"></ion-img>
<audio #audioPlayer controls>
<source [src]="currentTrack?.url" type="audio/mpeg">
Your browser does not support the audio element.
</audio>
<div class="controls">
<ion-button (click)="play()">Play</ion-button>
<ion-button (click)="pause()">Pause</ion-button>
</div>
</div>
</ion-content>
Update music-player.page.ts
import { Component, ViewChild, ElementRef, AfterViewInit } from '@angular/core';
@Component({
selector: 'app-music-player',
templateUrl: './music-player.page.html',
styleUrls: ['./music-player.page.scss'],
})
export class MusicPlayerPage implements AfterViewInit {
@ViewChild('audioPlayer') audioPlayer!: ElementRef;
currentTrack = {
title: 'Sample Song',
url: 'assets/audio/sample.mp3', // Place your MP3 file here
artwork: 'assets/images/cover.jpg' // Optional cover image
};
ngAfterViewInit() {
// You can load or autoplay here if needed
}
play() {
this.audioPlayer.nativeElement.play();
}
pause() {
this.audioPlayer.nativeElement.pause();
}
}
📁 Step 5: Add Assets
Put your sample audio file in:
src/assets/audio/sample.mp3
And a sample album art in:
src/assets/images/cover.jpg
🔌 Step 6: Use Capacitor Plugins (Optional but Recommended)
For better control on devices, especially background playback and media controls, consider using these Capacitor plugins:
Install Capacitor Audio Plugin
There’s no official Capacitor audio plugin for full media control, but you can use community ones like:
Or custom native code if needed.
Example using capacitor-media:
npm install @robingenz/capacitor-media
npx cap sync
Then in your component:
import { Media } from '@robingenz/capacitor-media';
async playMedia() {
await Media.createPlaylist({
playlistId: 'my_playlist',
items: [
{
id: 'track1',
title: 'Sample Song',
artist: 'Artist Name',
album: 'Album Name',
duration: 200,
url: 'https://example.com/sample.mp3',
artworkUrl: 'https://example.com/artwork.jpg'
}
]
});
await Media.play({ playlistId: 'my_playlist' });
}
🧪 Step 7: Build and Run on Devices
ionic build
npx cap copy
npx cap open ios # or android
Now you can run the app via Xcode or Android Studio.
✅ Features You Can Add Later
- Playlist management
- Background audio
- Equalizer
- Offline mode
- Music streaming (Spotify-like API integration)
- Local storage for favorites
- Notifications and lock screen controls
📦 Summary of Stack
| Tech | Purpose |
|---|
| Angular | Framework |
| Ionic | UI Components & Routing |
| Capacitor | Native device access |
| HTML5 Audio | Simple playback |
| capacitor-media | Advanced media controls (optional) |
Would you like me to provide a GitHub repo template or help implement background audio with Capacitor?