Files
AudioApp/server-api-example.md

2.0 KiB

Server API Example

Your server should provide an API endpoint that returns audio track information. Here's what the app expects:

Endpoint: GET /api/tracks

Returns a JSON array of audio tracks with their streaming URLs.

Response Format:

{
  "tracks": [
    {
      "id": "unique-track-id-1",
      "title": "Song Title",
      "artist": "Artist Name",
      "album": "Album Name",
      "duration": 180,
      "streamURL": "http://your-server.com/audio/track1.mp3",
      "artworkURL": "http://your-server.com/artwork/track1.jpg"
    },
    {
      "id": "unique-track-id-2",
      "title": "Another Song",
      "artist": "Another Artist",
      "album": "Another Album",
      "duration": 240,
      "streamURL": "http://your-server.com/audio/track2.mp3",
      "artworkURL": "http://your-server.com/artwork/track2.jpg"
    }
  ]
}

Field Descriptions:

  • id: Unique identifier for the track
  • title: Track title
  • artist: Artist name
  • album: Album name (optional)
  • duration: Track duration in seconds
  • streamURL: Direct URL to the audio file (mp3, m4a, etc.)
  • artworkURL: URL to album artwork image (optional)

Audio File Requirements:

  • Supported formats: MP3, M4A, AAC, WAV
  • Files should be accessible via HTTP/HTTPS
  • Consider using HTTP range requests for better streaming performance
  • HTTPS recommended for production

Example Server Implementation (Node.js/Express):

app.get('/api/tracks', (req, res) => {
  const tracks = [
    {
      id: '1',
      title: 'My Song',
      artist: 'My Artist',
      album: 'My Album',
      duration: 180,
      streamURL: 'http://localhost:3000/audio/song1.mp3',
      artworkURL: 'http://localhost:3000/artwork/song1.jpg'
    }
  ];
  
  res.json({ tracks });
});

app.use('/audio', express.static('path/to/audio/files'));
app.use('/artwork', express.static('path/to/artwork/files'));

Configuration:

Update the baseURL in CarPlayAudioApp/Services/AudioService.swift:

private let baseURL = "http://your-server.com/api"