1

我正在做一个项目,我有许多产品类别,每个产品类别都包含一个视频。我通过从 Web 服务获取 url 来播放视频。当我第一次播放该视频时,它应该只从该 URL 下载该视频一次。并且当没有互联网连接时,它应该从下载位置(sdcard)播放。假设我已经为所有三种产品下载了 3 个视频,现在我必须在没有互联网的情况下播放这些视频,即来自 sdcard。这里出现了如何为各个产品获取特定视频路径的问题。我不必打开画廊。我必须直接播放该产品的视频。以及如何限制视频下载不止一次。我播放视频的代码是

    private void playVideo(String path) {

    dialog = new Dialog(this,android.R.style.Theme_Translucent_NoTitleBar);
    dialog.setContentView(R.layout.layout_video_popup);

    ImageView imgViewClose = (ImageView)dialog.findViewById(R.id.imgViewClose);
    imgViewClose.setOnClickListener(this);

    btnPlayPause = (Button) dialog.findViewById(R.id.button1);
    btnPlayPause.setOnClickListener(this);

    seekBar = (SeekBar) dialog.findViewById(R.id.seekBar1);
    seekBar.setOnClickListener(this);
    linearlayout = (LinearLayout) dialog.findViewById(R.id.linerlayout_bottom);
    mVideoView = (VideoView)dialog.findViewById(R.id.videoView);
    mVideoView.setOnTouchListener(this);
    mVideoView.setOnPreparedListener(this);
    mVideoView.setOnCompletionListener(this);
    dialog.show();

    playVideo(path, mVideoView);
    AppLog.d("Video", "playVideo()", path);


}

private void playVideo(String path, VideoView mVideoView) {
    if(path == null || path.isEmpty()) {
        return;
    }

    try {
        pDialpg = ProgressDialog.show(this, "Video", "Please wait..", false, true);
        mVideoView.setVideoPath(path);
        mVideoView.requestFocus();
        RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);  
        relativeParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
        relativeParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
        mVideoView.setLayoutParams(relativeParams);
        mVideoView.invalidate();
    } catch (Exception e) {
        // TODO: handle exception
    }
4

1 回答 1

0

这只是你如何做到这一点的一个例子......

您可以检查是否有连接:

public Boolean checkConnectivity(Context context) {

    Log.d(TAG, "Checking connectivity");
    try {
        connectivityManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        wifiInfo = connectivityManager
                .getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        mobileInfo = connectivityManager
                .getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

        if (wifiInfo.isConnected() || mobileInfo.isConnected()) {
            Log.d(TAG, "Wifi/mobile Connection found");
            return true;
        }
    } catch (Exception e) {
        Log.w(TAG, "PROBLEM " + e);
    }
    Log.d(TAG, "No connection found - working offline");
    return false;
}

第一次,如果没有连接,您可以设置一个默认警告,例如:“无法在没有互联网连接的情况下播放视频”

如果有连接。你可以像这样播放流媒体:

    public Void playVideoStream(Context context,
        String url, final VideoView videoView) {

    Log.d(TAG, "Playing video stream");

    MediaController mediaController = new MediaController(context);
    mediaController.setAnchorView(videoView);

    Uri video = Uri.parse(url);
    videoView.setMediaController(mediaController);
    videoView.setVideoURI(video);

    videoView.setOnPreparedListener(new OnPreparedListener() {

        public void onPrepared(MediaPlayer mp) {
            videoView.start();
        }
    });

    return null;
}

如果您已经下载了它:

    public Void playVideoOffline(Context context,
        String filePath, VideoView videoView) {

    Log.d(TAG, "Playing video offline");

    videoView.setVideoPath(filePath);

    MediaController mediaController = new MediaController(context);
    mediaController.setMediaPlayer(videoView);

    videoView.setMediaController(mediaController);
    videoView.requestFocus();

    videoView.start();

    return null;
}

这是您可以做到的一种方式,不一定是最好的方式。

于 2013-04-11T13:33:54.767 回答