流AAC音频与Android [英] Streaming AAC audio with Android

查看:317
本文介绍了流AAC音频与Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,Android将只能播放AAC格式的音频,如果它是连接codeD的MPEG-4或3GPP。

As I understand it, Android will only play AAC format audio if it's encoded as MPEG-4 or 3GPP.

我能够播放AAC音频连接codeD作为M4A时,它的本地应用程序,但它从服务器获取时失败。

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.

下面的作品,作为M4A文件在res / raw目录本地保存。

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();

以下不起作用。 (但随着MP3的那样)。

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();

任何人都可以解释为什么它失败时M4A音频文件不是本地任何光线?

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)   
...

我针对SDK 1.6。

I'm targeting SDK 1.6.

推荐答案

解决方法,您可以从网上(和其他容器,如MP4与放大器的AAC文件播放M4A等文件; 3GP )。它只是下载该文件,并从缓存中播放。

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();
    }
}

这篇关于流AAC音频与Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆