Streaming AAC audio with Android

廉价感情. 提交于 2019-11-29 20:43:34

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

There is a the open source OpenMXPlayer that illustrates how to handle audio both for local files or online/streaming sources. It supports not only AAC, but also the other popular formats.

The idea behind it , is to use the MEdiaCodec API, introduced in Android 4.1 .

Hope this provides a good starting point, for anyone needing a quick and hassle free audio player implementation.

There is a Freeware Advanced Audio (AAC) Decoder for Android. You can use this for help and Guidance or use Multiplayer class in it to playback aac audio. There is a sample app and library in it to help you understand

Hope this helps

I tried it too but I could not find out the solution!

At the last Google I/O I saw something that helped me a lot. It is Extending from MediaPlayer and improve a lot of things! Take a look.

EXOPLAYER CAN HELP YOU A LOT

Check this part of the example:

private static final int BUFFER_SEGMENT_SIZE = 64 * 1024;
private static final int BUFFER_SEGMENT_COUNT = 256;
...

// String with the url of the radio you want to play
String url = getRadioUrl();
Uri radioUri = Uri.parse(url);
// Settings for exoPlayer
Allocator allocator = new DefaultAllocator(BUFFER_SEGMENT_SIZE);
String userAgent = Util.getUserAgent(context, "ExoPlayerDemo");
DataSource dataSource = new DefaultUriDataSource(context, null, userAgent);
ExtractorSampleSource sampleSource = new ExtractorSampleSource(
radioUri, dataSource, allocator, BUFFER_SEGMENT_SIZE * BUFFER_SEGMENT_COUNT);
audioRenderer = new MediaCodecAudioTrackRenderer(sampleSource);
// Prepare ExoPlayer
exoPlayer.prepare(audioRenderer);

EXOPLAYER- I can play anything from streamings (video and audio)!

LET ME KNOW IF YOU NEED HELP TO IMPLEMENT IT! :)

This is a wild shot in the dark, but I have seen similar behavior with the flash player where it actually ignores the file name and only relies on the MIME type sent by the server. Any idea what headers are being sent down from example.com? You might want to try wrapping your blah.m4a in a page that can set the headers and then stream the binary data. Give these types a shot and the community would appreciate a confirmation of what works:

audio/mpeg audio/mp4a audio/mp4a-latm audio/aac audio/x-aac

I wrote a smal blog on a cloud-based solution I use for a music player I'm developing. It's based on the young Android Cloud Services (ACS) project, which isn't available yet, but still, it shows a potential solution using cloud-based transcoding to stream any media files. Have a look : http://positivelydisruptive.blogspot.com/2010/08/streaming-m4a-files-using-android-cloud.html

Joel

subbarao

try --

1) MP.prepareAsync()

2) onPrepared() { mp.start() }

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!