AppStars

Building the world's first radio streaming app for mobile phones

In 2006 we built B-Live Radio for Bacardi, the world's first radio streaming app for mobile phones: a hand-written Shoutcast client, a threaded buffer and two players taking turns.

In 2006 we built B-Live Radio for Bacardi: the world's first radio streaming application for mobile phones. It played live internet radio on Motorola, Sony Ericsson and Nokia Series 60 handsets, in English and German.

Phones of that time could play a ringtone. Getting them to play a radio station that never ends took a hand-written network client, a threaded buffer and a trick with two media players. This post is how it worked.

Why it was hard in 2006

  • Slow, variable networks. Most handsets were on GPRS, with 3G only starting to appear. Bandwidth was low, latency was high and the connection could stall for seconds at a time.
  • Paying by the kilobyte. Users paid for every kilobyte of data, so the stream had to be low bit-rate and the app couldn't waste a byte on anything else.
  • A media API built for clips. The Mobile Media API (MMAPI) on Java phones expected a complete, finite stream: a sound file, a ringtone, a short clip. Hand it a live socket and most phones either refused or waited forever for the end.
  • Different on every phone. Each manufacturer's MMAPI implementation buffered, started and reported events in its own way.
  • Tiny heaps. A few hundred kilobytes had to hold the app, its screens and the audio in flight.

Architecture

B-Live was built on MIAD, our own SDK for phone apps whose screens are described in an XML markup (MML) and rendered by a J2ME client. The radio player was one more MIAD widget.

 radio station (Shoutcast)
   |  ICY response: headers, then MP3 with metadata blocks every N bytes
   v
 network thread: hand-written ICY client
   |  strips metadata -> "Now playing" text on screen
   v
 Mp3Buffer (bounded, wait/notify)
   |  one "piece" at a time (pieceSize bytes)
   v
 Player A  <-- takes turns -->  Player B      (MMAPI, audio/mpeg)
  • The widget is configured in MML. The station address, buffer size, piece size and two timeouts were attributes on the Shoutcast widget in the page, not constants in code.
  • Pages from the server. The main radio page was fetched from our server, so settings could change without a new jar.
  • One code base, many builds. B-Live shipped as 18 jars: six handset groups, each in English, German and a rest-of-world build, between 84 KB and 93 KB each.

The engineering

A hand-written ICY client

Shoutcast servers spoke ICY, a close cousin of HTTP. Java ME's HTTP support was too inconsistent to trust with a stream that never ends, so we opened a raw socket and wrote the request ourselves:

String request = "GET " + mParams.mResString + " HTTP/1.0\r\nHost: " + mParams.mAddrString + "\r\n";
request += "Connection: keep-alive\r\nicy-metadata: " + a_metaData + "\r\n";
// ...plus a User-Agent naming a desktop player, then a blank line
SocketConnection msock = (SocketConnection)Connector.open(
    "socket://" + mParams.mAddrString + ":" + mParams.mPort, Connector.READ_WRITE);
msock.setSocketOption(msock.KEEPALIVE, 1);
msock.openOutputStream().write(request.getBytes());

Stations of the day expected to be talking to a desktop player like Winamp, so we identified as one. The client then read the response headers until the blank line, checked for ICY 200 OK, and picked out the station name and icy-metaint: how many bytes of audio would arrive between metadata blocks. If a station didn't offer metadata, the client reconnected without it and played the audio anyway.

Pulling the song title out of the stream

With metadata switched on, the server interleaves a small text block into the MP3 stream every metaint bytes. Its first byte gives its length in 16-byte units. Leave those bytes in and the player hears noise. Take them out and you get the current song.

The network thread counted audio bytes as they arrived. When a metadata block fell inside a 1 KB read, it split the chunk, passed the audio on either side to the buffer and read the text out of the middle. It then pulled the title from between the quotes and updated the screen only when it changed:

String metaInfoStr = new String(metaInfo);
int startI = metaInfoStr.indexOf("'")+1;
int endI = metaInfoStr.indexOf("'", startI);
metaInfoStr = metaInfoStr.substring(startI, endI);
if(metaInfoStr.compareTo(m_songInfo) != 0)
{
  m_songInfo = metaInfoStr;
  // find the "shoutcastoutput" text widget on the page,
  // set "Now playing:" + title, and mark the page dirty
}

"Now playing" came from a string table that the build swapped for "Jetzt spielend" in the German jars.

A bounded buffer between network and audio

The network and the player ran at different speeds, and GPRS made the network's speed unpredictable. Between them sat Mp3Buffer, a classic bounded producer and consumer. The network thread waited when the buffer was full. The player side waited until a full piece was ready:

public synchronized void writeData(byte [] buff,int len)throws InterruptedException
{
  while((mPos+len > mBuffSize) && bRun)
    wait(mTimeout);
  System.arraycopy(buff, 0, mBuffer, mPos, len);
  mPos+=len;
  notifyAll();
}

public synchronized ByteArrayInputStream getData() throws InterruptedException
{
  while((mPos < mMinPlay) && bRun)
    wait(mTimeout);
  ByteArrayInputStream bIn = new ByteArrayInputStream(mBuffer,0,mPos);
  mPos = 0;
  notifyAll();
  return bIn;
}

Two numbers did the tuning. The maximum buffer (60,000 bytes by default) capped how much heap audio could use. The minimum play size (23,000 bytes by default) decided how much audio each piece carried. A bigger piece rode out longer network stalls but took longer to start and used more memory. A smaller one started quickly but left less margin. There was no single right answer, which is why both were settings.

Two players taking turns

MMAPI wanted a finished stream, so we never gave it the live one. Each piece from the buffer became its own small, finished MP3 stream with its own Player. Two players took turns. As soon as one started playing, the other was created, realised and prefetched with the next piece. When one reached the end of its piece, the other started:

public void playerUpdate(Player player,String event,Object eventData)
{
  if(event == PlayerListener.END_OF_MEDIA)
  {
    if(player == mFirstPlayer) mSecondPlayer.start();
    else                       mFirstPlayer.start();
  }
  else if(event == PlayerListener.STARTED)
  {
    if(player == mFirstPlayer) mSecondPlayer = createBufferedPlayer(mSecondPlayer);
    else                       mFirstPlayer  = createBufferedPlayer(mFirstPlayer);
  }
}

Preparing the next player while the current one played is what made gapless playback possible: when a piece ended, its successor was already loaded and waiting. Old players were explicitly deallocated and closed before being replaced, because on a phone with a tiny heap a leaked player soon became a crash.

Per-handset tuning in MML

Phone families handled that hand-over differently, and the right piece size, buffer size and pause before building the next player varied with them. Because buffer size, piece size and both timeouts were attributes on the widget in MML, a phone family could be tuned by changing a page on the server, not by rebuilding and redistributing 18 jars.

Where AI fits

There was no AI in B-Live Radio. What carried forward is the plumbing. AI that listens, such as speech recognition on a phone or on smart glasses, depends on exactly the same things: audio arriving at an unpredictable rate, a bounded buffer that protects memory, pieces sized for the model and a pipeline that keeps going when the network hiccups. Our wearables and edge AI work streams audio and camera frames from glasses to a paired device. The buffers are bigger and the models are new, but the discipline came from making a 2006 phone play the radio.

What it taught us

  • Work with the brand, build for the user. The client cared about the experience of their brand on a phone. The user cared that the music didn't stop. We had to satisfy both on hardware that wasn't designed for either.
  • Put the tuning where it can change. Moving buffer sizes and timeouts into server-side pages turned per-handset fixes into configuration, and meant a fix for one phone couldn't break another.
  • Test on real devices. Emulators couldn't tell us how a real handset behaves on a real network. Only real devices told us which settings each family needed.
  • Handle every failure with a message. No connection, no ICY headers, no metadata: each had its own clear message or fallback instead of a silent hang.

Why it still matters

Streaming over poor networks on constrained devices is still everyday work: field crews in patchy coverage, apps on cheap phones, wearables that hand their work to a paired device. The habits from B-Live still apply. Buffer deliberately, prepare the next step before you need it, keep tuning in configuration and test where your users really are. That's how we build mobile and field apps today. The Mobstar Games story has more from the era, and MIAD and our poker server covers the SDK B-Live was built on.

Bring us the hard problem.

A 45-minute discovery call with the engineer who'll build it.