如何从SD卡播放视频

时间:2013-04-11 10:07:36

标签: android

我正在进行一个项目,其中我有多个产品类别,每个产品类别包含一个视频。 我正在通过网络服务获取网址来播放视频。当我第一次播放该视频时,它应该只从该网址下载该视频一次。当没有互联网连接时,它应该从下载的地方播放(SD卡)。假设我已经为所有三种产品下载了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
    }

1 个答案:

答案 0 :(得分: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;
}

首次,如果没有连接,您可以设置默认警告,例如:“无法播放没有网络连接的视频”

如果有conn。你可以像以下一样播放流媒体:

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

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