Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Video View in another Thread & Issue with android 2.1

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??

like image 436
edwin Avatar asked Nov 04 '22 03:11

edwin


1 Answers

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

}
like image 105
Suji Avatar answered Nov 11 '22 13:11

Suji