在v2.2上不支持Android Video View

时间:2013-07-03 10:31:50

标签: android android-videoview

我开发了一个直播视频的应用程序,但它不支持Android版2.2,2.3等只在Android 4.1版(三星Galaxy Grand)上播放视频。 我在我的项目中使用videoview。 所以你能告诉我确切的原因吗​​? 我的代码如下:

mPath.setText("http://iptvshqip.dyndns.tv:8090");       
    mPlay = (ImageButton) findViewById(R.id.play);
    mPause = (ImageButton) findViewById(R.id.pause);
    mReset = (ImageButton) findViewById(R.id.reset);
    mStop = (ImageButton) findViewById(R.id.stop);
            mPlay.setOnClickListener(new OnClickListener() {
        public void onClick(View view) {
            playVideo();
        }
    });
    mPause.setOnClickListener(new OnClickListener() {
        public void onClick(View view) {
            if (mVideoView != null) {
                mVideoView.pause();
            }
        }
    });
    mReset.setOnClickListener(new OnClickListener() {
        public void onClick(View view) {
            if (mVideoView != null) {
                mVideoView.seekTo(0);
            }
        }
    });
    mStop.setOnClickListener(new OnClickListener() {
        public void onClick(View view) {
            if (mVideoView != null) {
                current = null;
                mVideoView.stopPlayback();
            }
        }
    });
    runOnUiThread(new Runnable(){
        public void run() {
            playVideo();
        }
    });
  }
 private void playVideo() {
    try {
        final String path = mPath.getText().toString();
        Log.v(TAG, "path: " + path);
        if (path == null || path.length() == 0) {
            Toast.makeText(MainActivity.this, "File URL/path is empty",
                    Toast.LENGTH_LONG).show();
        } 
        else {
            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", "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;
    }
}

2 个答案:

答案 0 :(得分:0)

Android从3.0开始支持HTTP Live Stream(HLS)。阅读以下链接。

http://www.longtailvideo.com/blog/31646/the-pain-of-live-streaming-on-android/

他们已解决了解决方法。

答案 1 :(得分:0)

如上所述,自API级别1以来Android VideoView已存在。

除了:

  • 标记STATUS_BAR_HIDDENSTATUS_BAR_VISIBLE - 已在API 14中弃用
  • API 16中弃用的函数setBackgroundDrawable(Drawable background)
  • API 5中添加的公开方法 - canPause()canSeekBackward()canSeekForward()
  • 在API 8中添加了
  • resume()suspend()
  • 在API 14中添加了
  • onInitializeAccessibilityEvent (AccessibilityEvent event)onInitializeAccessibilityNodeInfo (AccessibilityNodeInfo info)
  • 在API 17中添加了
  • setOnInfoListener (MediaPlayer.OnInfoListener l)

你使用其中任何一种吗?无论如何,你的问题可能在其他地方。

请粘贴一些代码并详细说明这是如何起作用的。