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 is done in the following way:
Trying to recognize
the type from the URL. In this case with .3gp
(this is the most likely to be recognized).
If there is
no URL, recognition is done with the provided content type (InputStream
and MIME
).
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)