微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在Android中播放远程网址的实时视频流?

我创建了一个函数,它以特定的时间间隔触发通知服务.通知可以是文本,图像或视频……

现在,对于视频我首先下载它然后播放它需要更多的时间…所以有任何机制,我可以直接从远程网址播放视频???

请帮我 …
我迫切需要答案……

提前致谢 ……..

看看我的代码片段::

public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.notificationvideo);

    mVideoView = (VideoView) findViewById(R.id.video);
    //pd=ProgressDialog.show(this,"Loading...","Please Wait...",true,false);
    playVideo();
    //pd.dismiss();

    img_back = (ImageView) findViewById(R.id.img_back); 
    img_back.setonClickListener(new View.OnClickListener() 
     {          
        public void onClick(View v) 
        {
                Intent int_back=new Intent(NotificationsVideoActivity.this,MyChannelsActivity.class);
                startActivity(int_back);
                finish();
        }               
    });  
}

private void playVideo() 
{
    try {
         path = getIntent().getStringExtra("url");

        Log.v(TAG,"path: " + path);
        if (path == null || path.length() == 0) {
            Toast.makeText(NotificationsVideoActivity.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.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","mp4");
        temp.deleteOnExit();
        String tempPath = temp.getAbsolutePath();
        FileOutputStream out = new FileOutputStream(temp);
        byte buf[] = new byte[128];
        //byte buf[] = new byte[8192];

        do 
        {
            int numread = stream.read(buf);
            if (numread <= 0)
                break;
            out.write(buf,numread);
        } while (true);

        try 
        {
            stream.close();
        }
        catch (IOException ex) 
        {
            Log.e(TAG,"error: " + ex.getMessage(),ex);
        }
        return tempPath;

    }
}

解决方法

试试这个 –

String path="http://www.ted.com/talks/download/video/8584/talk/761";
String path1="http://commonsware.com/misc/test2.3gp";

Uri uri=Uri.parse(path1);

VideoView video=(VideoView)findViewById(R.id.VideoView01);
video.setVideoURI(uri);
video.start();

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐