【问题标题】:How to play live video streaming from remote url in android?如何在 android 中从远程 url 播放实时视频流?
【发布时间】:2023-04-03 16:15:02
【问题描述】:

我创建了一个在特定时间间隔触发通知服务的功能。通知可以是文本、图像或视频...

现在,对于视频,我先下载它然后播放它,这需要更多时间......所以有什么机制可以让我直接从远程 url 播放视频???

请帮帮我... 我迫切需要尽快得到答案...

在此先感谢........

看看我的代码 sn-ps ::

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, 0, numread);
        } while (true);

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

    }
}

【问题讨论】:

    标签: android video stream


    【解决方案1】:

    试试这个 -

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

    【讨论】:

    • 我已经尝试过了...我可以像这样增加缓冲区大小吗????对我有帮助吗?????byte buf[] = new byte[8192];
    【解决方案2】:

    尝试在http://code.google.com/p/html5webview/downloads/list 给出的webview 和示例,完整的源代码在http://html5webview.googlecode.com/svn/trunk/ 作为参考并尝试你想要的

    【讨论】:

      【解决方案3】:
      VideoView video = (VideoView) findViewById(R.id.video);
      ProgressDialog mProgressDialog = new ProgressDialog(this);
                  mProgressDialog.setMessage("Loading Video Please wait...");
                  mProgressDialog.setIndeterminate(true);
                  mProgressDialog.setCancelable(false);
                  mProgressDialog.show();
      
                  video.setMediaController(new MediaController(MainActivity.this));
                  uri = Uri.parse("live streaming url");
                  video.setVideoURI(uri);
      
      video.setOnPreparedListener(new OnPreparedListener() {
      
                  @Override
                  public void onPrepared(MediaPlayer mp) {
                      // TODO Auto-generated method stub
      
      
                      video.start();
                      mProgressDialog.dismiss();
                  }
              });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-07-11
        • 1970-01-01
        • 1970-01-01
        • 2011-09-28
        相关资源
        最近更新 更多