Remote resources

All the audio or video resources the MIDlet needs may not fit into the JAR archive. To overcome this limit, the MIDlet may download content for the MMAPI use from an HTTP server. The downloaded media can be stored to the persistent storage the device supports, for example, RecordStore or a file. In addition, using an external HTTP server as a media content source allows the media to be updated more easily in a centralized way.

Recognition in the Series 40 platform and in S60 3rd Edition is done in the following way:

  1. Trying to recognize the type from the URL. In this case with .3gp (this is the most likely to be recognized).

  2. If there is no URL, recognition is done with the provided content type (InputStream and MIME).

If there is no recognition with the above cases, or in the second case null was given as content type, recognition will be tried with header data in S60. Header data cannot be used for recognition in Series 40.

The second alternative uses buffer technique. It gives the MIDlet the possibility to handle the reading process in a customized way. When a separate thread is used in reading, the media can be loaded in a non-blocking way. To simplify the reading example, thread usage is skipped. The following is an example of how to use the buffering technique:

private InputStream urlToStream(String url) throws IOException
{
   // Open connection to the http url...
   HttpConnection connection = (HttpConnection) Connector.open(url);
   DataInputStream dataIn = connection.openDataInputStream();
   byte[] buffer = new byte[1000];
   int read = -1;
   // Read the content from url.
   ByteArrayOutputStream byteout = new ByteArrayOutputStream();
   while ((read=dataIn.read(buffer))>=0)
   {
     byteout.write(buffer, 0, read);
   }
   dataIn.close();
   connection.close();
   // Fill InputStream to return with content read from the URL.
   ByteArrayInputStream byteIn = 
      new ByteArrayInputStream(byteout.toByteArray());
   return byteIn;
}}

In the example, HttpConnection is used to open a networking connection. In the code snippet, byte data is read from the server to the 1000 bytes length buffer by using DataInputStream (dataIn). The read operation continues in a while loop until there is no more data left to read from the server. Inside the loop, the buffered data is written to ByteArrayOutputStream (byteout). Finally, after exiting the while loop, the byte data from ByteOutputStream is converted to ByteArrayInputStream (byteIn). Now, InputStream returned by the example's urltoStream(String url) method above can be used to create a Player instance with the createPlayer method of the Manager class:

createPlayer(java.io.InputStream stream, java.lang.String type)

Note: HTTP streaming is supported for the content type "audio/amr" from the S60 3rd Edition, Feature Pack 1 onwards. RTSP streaming is supported from S60 2nd Edition, Feature Pack 3 onwards. The list of supported protocols can be queried using the Manager.getSupportedProtocols(null) method.