2013-04-11 186 views
1

我正在做一個項目,其中有多個產品類別,每個產品類別包含一個視頻。 我通過從網絡服務獲取網址來播放視頻。當我第一次播放該視頻時,應該只從該網址下載該視頻一次。當沒有互聯網連接時,它應該從下載地點播放(SD卡)。假設我已經爲所有三種產品下載了3個視頻,現在我必須在沒有互聯網的情況下播放這些視頻,即從SD卡播放。這裏來的問題是如何獲得各自產品的特定視頻路徑。我不必打開畫廊。我必須直接播放該產品的視頻。以及如何限制多次視頻下載。 我播放視頻的代碼是如何從SD卡播放視頻

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 
    } 
+0

請參閱此鏈接:http://stackoverflow.com/questions/10083609/playing-video-from-sd-card – krishna 2013-04-11 10:37:45

+0

這些提供了特定視頻的鏈接。我的問題是別的 – Avinash 2013-04-11 10:41:15

回答

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

這是一種方式,你可以做到這一點,不一定做的最好的方式。