As I understand it, Android will only play AAC format audio if it's encoded as MPEG-4 or 3GPP.
I'm able to play AAC audio encoded as M4A when it's local to the app, but it fails when obtaining it from a server.
The following works, as the m4a file is held locally in the res/raw directory.
MediaPlayer mp = MediaPlayer.create(this, R.raw.*file*); mp.start();
The following doesn't work. (But does with MP3's).
Uri uri = Uri.parse("http://*example.com*/blah.m4a"); MediaPlayer mp = MediaPlayer.create(this, uri); mp.start();
Can anyone shed any light on why it fails when the m4a audio file is not local?
Here's (some of) the error...
ERROR/PlayerDriver(542): Command PLAYER_INIT completed with an error or info UNKNOWN PVMFStatus ERROR/MediaPlayer(769): error (200, -32) WARN/PlayerDriver(542): PVMFInfoErrorHandlingComplete DEBUG/MediaPlayer(769): create failed: DEBUG/MediaPlayer(769): java.io.IOException: Prepare failed.: status=0xC8 DEBUG/MediaPlayer(769): at android.media.MediaPlayer.prepare(Native Method) DEBUG/MediaPlayer(769): at android.media.MediaPlayer.create(MediaPlayer.java:530) DEBUG/MediaPlayer(769): at android.media.MediaPlayer.create(MediaPlayer.java:507) ...
I'm targeting SDK 1.6.
Several music apps and services for Android support most of the audio file types used by the iTunes software, including D.R.M. -free AAC, MP3 and WMA (Windows Media Audio).
The Google Play Music in computers can be accessed through the Google play while on smartphones and androids music can be listened through the Google Play app for Android. The supported files for upload in Google Play music include AAC, MP3, FLAC, OGG, or ALAC.
Along with SBC and Qualcomm's aptX, AAC is one of the most commonly supported Bluetooth codecs in the wireless headphone and speaker markets. It's also the default audio data compression used by Apple's iTunes and Google's YouTube, and is supported across both iPhone and Android smartphones.
For Windows, choose either Add File to Library or Add Folder to Library to add the AAC files to your iTunes Library. Another way to play AAC files with VLC, Media Player Classic (MPC-HC), Windows Media Player, MPlayer, Audials One, and likely many other multi-format media players.
This work-around allows you to play M4A files from the net (and AAC files in other containers such as MP4 & 3GP). It simply downloads the file and plays from the cache.
private File mediaFile; private void playAudio(String mediaUrl) { try { URLConnection cn = new URL(mediaUrl).openConnection(); InputStream is = cn.getInputStream(); // create file to store audio mediaFile = new File(this.getCacheDir(),"mediafile"); FileOutputStream fos = new FileOutputStream(mediaFile); byte buf[] = new byte[16 * 1024]; Log.i("FileOutputStream", "Download"); // write to file until complete do { int numread = is.read(buf); if (numread <= 0) break; fos.write(buf, 0, numread); } while (true); fos.flush(); fos.close(); Log.i("FileOutputStream", "Saved"); MediaPlayer mp = new MediaPlayer(); // create listener to tidy up after playback complete MediaPlayer.OnCompletionListener listener = new MediaPlayer.OnCompletionListener() { public void onCompletion(MediaPlayer mp) { // free up media player mp.release(); Log.i("MediaPlayer.OnCompletionListener", "MediaPlayer Released"); } }; mp.setOnCompletionListener(listener); FileInputStream fis = new FileInputStream(mediaFile); // set mediaplayer data source to file descriptor of input stream mp.setDataSource(fis.getFD()); mp.prepare(); Log.i("MediaPlayer", "Start Player"); mp.start(); } catch (Exception e) { e.printStackTrace(); } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With