Downloading Audio from the Internet
In addition to the standard methods of downloading data using the Connection, HttpConnection, or Downloader objects, the MoSync SDK provides a special downloader, the AudioDownloader class, for retrieving audio files from the Internet. The AudioDownloader is a useful wrapper for the Downloader class and is specifically for audio files.
Usually, when you create an audio resource on a server you set its MIME type. The MIME type determines how the device should play the sound file. You can find a list of common MIME types on Wikipedia. You can also specify a MIME type for the audio file when you download it.
It is very important to understand how the MIME type set on the server interacts with the MIME type set on the download to determine the final MIME type of the file. This is because the server MIME type has precedence.
- If you do not set a MIME type for a download, and a MIME type has been set on the server file, the server MIME type will be used.
- If you do specify a MIME type, and the Web server provides another, then Web server's MIME type will be used and the value you specify will be ignored.
- If neither you nor the web server have set a MIME type, it will cause a panic.
The conclusion from this is that you should always set the MIME type for the download.
This code below will download and play an MP3 file from the web and set its MIME type to 'audio/mpeg', which is suitable for MP3 files.
/**
* @file DownloadAudio.cpp
*
* This program downloads and plays an audio file from the Internet.
*
* Todo: You need to goto Project -> Properties -> Build Settings and
* add MAUtil.lib, MAUI.lib libraries in Additional Libraries.
*
* @author Sam Pickard, Naveed Asif
*/
#include <MAUtil/Moblet.h>
#include <MAUI/Screen.h>
#include <MAUtil/Downloader.h>
#include <conprint.h>
using namespace MAUtil;
using namespace MAUI;
/*
* Class that downloads and plays audio file from
* the Internet.
*/
class MyScreen :
public Screen,
public DownloadListener
{
public:
MyScreen()
{
// Message: Starting application.
printf("Starting application\n");
// A new instance of AudioDownloader is created.
mAudioDownloader = new AudioDownloader();
mAudioDownloader->addDownloadListener(this);
mAudioResource = maCreatePlaceholder();
// Message: Starting download.
printf("Starting download\n");
// The audio resource starts downloading here.
mAudioDownloader->beginDownloading(
"http://downloads.bbc.co.uk/doctorwho/sounds/exterminate.mp3",
mAudioResource,
"audio/mpeg",
true);
}
/*
* The destructor.
*/
virtual ~MyScreen()
{
delete mAudioDownloader;
maDestroyObject(mAudioResource);
}
/*
* The function is fired in case of download cancellation.
*/
void downloadCancelled(Downloader* downloader)
{
lprintfln("Cancelled");
}
/*
* Method displays error code in case of error in downloading.
*/
void error(Downloader* downloader, int code)
{
lprintfln("Error: %d", code);
}
/*
* On successful download completion, the audio file is played.
*/
void finishedDownloading(Downloader* downloader, MAHandle data)
{
lprintfln("Completed download");
// Plays the downloaded file.
maSoundPlay(mAudioResource, 0, maGetDataSize(mAudioResource));
}
/*
* Notifies download progress.
*/
void notifyProgress(
Downloader* downloader,
int downloadedBytes,
int totalBytes)
{
lprintfln("Downloaded %d of %d bytes", downloadedBytes, totalBytes);
}
private:
AudioDownloader* mAudioDownloader;
MAHandle mAudioResource;
};
/*
* Moblet for the download audio.
*/
class MAUIMoblet : public Moblet
{
public:
MAUIMoblet()
{
// Create the screen.
mScreen = new MyScreen();
mScreen->show();
}
/*
* Key press events are handled here.
*/
void keyPressEvent(int keyCode)
{
if(keyCode == MAK_0 || keyCode == MAK_BACK)
{
maExit(0);
}
}
/*
* The destructor.
*/
virtual ~MAUIMoblet()
{
delete mScreen;
}
private:
MyScreen* mScreen;
};
/**
* Main function that starts the program.
*/
extern "C" int MAMain()
{
Moblet::run(new MAUIMoblet());
return 0;
}
- Printer-friendly version
- Login or register to post comments
Share on Facebook