5

I want stream video form url in android video view . I use the sample api code and done little modification in that to achieve my need .My code is

public class VideoViewDemo extends Activity {

private static final String TAG = "VideoViewDemo";
private String current;

/**
 * TODO: Set the path variable to a streaming video URL or a local media
 * file path.
 */
private String path = "http://www.boisestatefootball.com/sites/default/files/videos/original/01%20-%20coach%20pete%20bio_4.mp4";
private VideoView mVideoView;

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.videoview);
    mVideoView = (VideoView) findViewById(R.id.surface_view);
    runOnUiThread(new Runnable() {
        public void run() {
            playVideo();
        }
    });
}

private void playVideo() {
    try {
        // final String path = path;
        Log.v(TAG, "path: " + path);
        if (path == null || path.length() == 0) {
            Toast.makeText(VideoViewDemo.this, "File URL/path is empty",
                    Toast.LENGTH_LONG).show();

        } else {
            // If the path has not changed, just start the media player
            if (path.equals(current) && mVideoView != null) {
                mVideoView.start();
                mVideoView.requestFocus();
                return;
            }
            current = path;
            mVideoView.setVideoPath(getDataSource(path));
            mVideoView.start();
            mVideoView.setMediaController(new MediaController(this));
            mVideoView.requestFocus();

        }
    } catch (Exception e) {
        Log.e(TAG, "error: " + e.getMessage(), e);
        if (mVideoView != null) {
            mVideoView.stopPlayback();
        }
    }
}

private String getDataSource(String path) throws IOException {
    if (!URLUtil.isNetworkUrl(path)) {
        return path;
    } else {
        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");
        temp.deleteOnExit();
        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);
        try {
            stream.close();
        } catch (IOException ex) {
            Log.e(TAG, "error: " + ex.getMessage(), ex);
        }
        return tempPath;
    }
}
}

Here you can see i am using uithread to stream video .Is there any way to handle this by my on thread

What i tried is

new Thered(new Runnable() {
        public void run() {
            playVideo();
        }
    }).start();

But it Fails

And also While running in Android 2.2 The First code runs and it shows error(-38,0) in Android 2.1 what is this error?? I checked This File but cannot find out what error is this??

Can anybody Guide me??

edwin
  • 7,985
  • 10
  • 51
  • 82
  • Have a button say 'Play Video', and in its click function place your Runnable Thread code. Having your own thread in OnCreate() function could get messy if not handled appropriately as the activity creation has to go fine always. By the way did you tried this in Android emulator or Phone? – Rahul Sundar Apr 12 '13 at 18:22
  • @RahulSundar i tried on phone. i didn't want to use button. i wish to play the video directly when i enter to activity . thats why i didn't use any button – edwin Apr 13 '13 at 14:32
  • Ok. Just to check if everything works fine with thread, launch it from a button. Then use the same in OnCreate() function. To make sure video rendering starts after completion of Activity creation, give enough delay Sleep(5 seconds) and check if everything works fine. – Rahul Sundar Apr 13 '13 at 15:32
  • @RahulSundar i will try that . I use the same logic to play audio. and it works without any issues . – edwin Apr 18 '13 at 09:38

1 Answers1

2

You do not need to fetch the entire video, and save it in file system, then run it..The video you have mentioned is having 32Mb size, and it will take so much time to fetch via network. Instead you can give the direct link to videoview, it will progressively fetch/buffer the video and play it. You were trying to fetch the video data in UI thread, that is not acceptable. Here is the corrected code, you can check it.

public class VideoViewDemo extends Activity {

private static final String TAG = "VideoViewDemo";
private String current;

/**
 * TODO: Set the path variable to a streaming video URL or a local media
 * file path.
 */
private String path = "http://www.boisestatefootball.com/sites/default/files/videos/original/01%20-%20coach%20pete%20bio_4.mp4";
private VideoView mVideoView;

private Handler handler;

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    handler = new Handler();
    setContentView(R.layout.videoview);
    mVideoView = (VideoView) findViewById(R.id.surface_view);
/*    runOnUiThread(new Runnable() {
        public void run() {
            playVideo();
        }
    });
*/    
    playVideo();
    Log.v(TAG, "activity oncreate finished");
}

private void playVideo() {
    try {
        // final String path = path;
        Log.v(TAG, "path: " + path);
        if (path == null || path.length() == 0) {
            Toast.makeText(VideoViewDemo.this, "File URL/path is empty",
                    Toast.LENGTH_LONG).show();

        } else {
            // If the path has not changed, just start the media player
            if (path.equals(current) && mVideoView != null) {
                mVideoView.start();
                mVideoView.requestFocus();
                return;
            }
            current = path;

            mVideoView.setVideoPath(path);
            mVideoView.start();
            mVideoView.setMediaController(new MediaController(VideoViewDemo.this));
            mVideoView.requestFocus();

        }
    } catch (Exception e) {
        Log.e(TAG, "error: " + e.getMessage(), e);
        if (mVideoView != null) {
            mVideoView.stopPlayback();
        }
    }
}

}
Suji
  • 6,044
  • 2
  • 19
  • 17
  • @edwin, was my answer useful? – Suji Aug 08 '13 at 05:19
  • Thanks for the code! I have another question, do you know whether the video is going to be saved somewhere on the device after it has been completely watched? Is it accessible there? Please also see my [question](http://stackoverflow.com/questions/23842450/android-where-do-videoview-and-mediacontroller-store-media-downloaded-with-htt) on that issue – nburk May 24 '14 at 08:52
  • @Suji, Your code is not being started in a separate thread. So it cannot be killed in progress by using a volatile boolean interrupter. What do you propose as code in stopPlayback()? – JAW Aug 02 '22 at 13:54
  • @Suji, Sorry, never mind. This posting clarified my confusion: https://stackoverflow.com/questions/15767742/android-videoview-clear-display-after-stopplayback – JAW Aug 02 '22 at 14:07