Android: how to play audio and video from app

I was looking for a way how to play video and audio streams from Internet from my app.
There are several ways how to do it and some of them worked for me.

1. Play in inner VideoView.
The solution I found here how to play video from url

String path="http://www.ted.com/talks/download/video/8584/talk/761";
        String path1="http://commonsware.com/misc/test2.3gp";
VideoView videoView = (VideoView) findViewById(R.id.VideoView01);
        MediaController mc = new MediaController(this);
        mc.setAnchorView(videoView);
        mc.setMediaPlayer(videoView);
        Uri video = Uri.parse(path2);
        videoView.setMediaController(mc);
        videoView.setVideoURI(video);
        videoView.start();

in your layout xml set the following:



    
    

This method didn’t work for me pretty well, because I tried to play 1 hour mp4 file, which was buffered too long time and then played really slow.

2. Play in installed Media Player.
This solution is pretty nice and very simple. Found here Launch ACTION_VIEW Intent to play mp4

Uri intentUri = Uri.parse(URL);
        
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_VIEW);
        intent.setDataAndType(intentUri, "video/mp4");
        startActivity(intent);

In this case external media player will be used. Additionally, user will be able to select preferred media player to play this video in case there are several of them installed.
To play mp3 file, do the following code.

Uri intentUri = Uri.parse(URL);
        
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_VIEW);
        intent.setDataAndType(intentUri, "audio/*");
        startActivity(intent);

1 comment

Leave a Reply

%d bloggers like this: