Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android live streaming of TV before Android 2.1

I am developing app in which I have to implement live TV streaming. My Google search has lead me to believe that live streaming is not possible till 2.1 Android.

Is it right?

As I get code of streaming music of mediaplayer and I can use type of it by setting below method:

mp.setAudioStreamType(2);

But i want to know is it sufficient for streaming just code like that and save file like below method:

private void setDataSource(String path) throws IOException {
        if (!URLUtil.isNetworkUrl(path)) {
            mp.setDataSource(path);
        } else {
            Log.i("enter the setdata","enter the setdata");
            URL url = new URL(path);
            URLConnection cn = url.openConnection();
            cn.connect();
            InputStream stream = cn.getInputStream();
            if (stream == null)
                throw new RuntimeException("stream is null");
            File temp = File.createTempFile("mediaplayertmp", "dat");
            String tempPath = temp.getAbsolutePath();
            FileOutputStream out = new FileOutputStream(temp);
            byte buf[] = new byte[128];
            do {
                int numread = stream.read(buf);
                if (numread <= 0)
                    break;
                out.write(buf, 0, numread);
            } while (true);
            mp.setDataSource(tempPath);

            try {
                stream.close();
                Log.i("exit the setdata","exit the setdata");
            }
            catch (IOException ex) {
                Log.e(TAG, "error: " + ex.getMessage(), ex);
            }
        }
    }

Is there any extra stuff needed for live TV streaming?

like image 247
chikka.anddev Avatar asked Feb 10 '11 06:02

chikka.anddev


1 Answers

Adressing "Is it sufficient" : absolutely not.

You're saving all the data from the URL to the device, then playing it back. This works if you can guarantee it's a small clip, but 'live tv streaming' implies we're talking about a stream of unknown length sent at a real-time rate.

The impact of this is :

  1. A N-minute long program will take N-minutes to stream to the device before playback starts.
  2. A long broadcast has the potential to fill up all available storage.

The MediaPlayer.setDataSource(FileDescriptor fd) method should read data from any source you can get a FileDescriptor for, including sockets.

The exact details of how to use this will vary based on the protocol you're using, but essentially you need to read data from the broadcast source, transcode it to a suitable form, and pipe it to the fd.

like image 189
Phil Lello Avatar answered Oct 15 '22 13:10

Phil Lello