2011-02-10 10 views
0

mp4 비디오를 재생하고 싶습니다. 그래서 나는 아래의 방법을 시도했다스트리밍 및 mp4 비디오 재생

private void playVideo() { 
     try { 

      final String path = s; 
      Log.v(TAG, "path: " + path); 
      if (path == null || path.length() == 0) { 
      Toast.makeText(VideoPlay1.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) { 
        MediaController mediaController = new MediaController(this); 
        mediaController.setAnchorView(mVideoView); 
        Uri video = Uri.parse(getDataSource(path)); 
        Log.e("Uri video",video.toString()); 
        mVideoView.setMediaController(mediaController); 
        mVideoView.setVideoURI(video);  
        mVideoView.setVideoPath(getDataSource(path)); 
        mVideoView.setOnPreparedListener(new OnPreparedListener() { 

         public void onPrepared(MediaPlayer arg0) { 
          dialog.dismiss(); 
          mVideoView.start(); 
         } 
        }); 
        mVideoView.requestFocus(); 
       return; 
      } 
      current = path; 

      MediaController mediaController = new MediaController(this); 
      Uri video = Uri.parse(getDataSource(path)); 

      Log.e("Uri video",video.toString()); 
      mVideoView.setMediaController(mediaController); 
      mVideoView.setVideoURI(video);  
      mVideoView.setVideoPath(getDataSource(path)); 
       mVideoView.setOnPreparedListener(new OnPreparedListener() { 

        public void onPrepared(MediaPlayer arg0) { 
         dialog.dismiss(); 
         mVideoView.start(); 
        } 
       }); 
      mVideoView.requestFocus(); 
    } 
     } catch (Exception e) { 
      Log.e(TAG, "error123: " + e.getMessage(), e); 
      if (mVideoView != null) { 
       mVideoView.stopPlayback(); 
      } 
     } 
    } 
    @Override 
    public boolean onKeyDown(int keyCode, KeyEvent event) { 
     if (Integer.parseInt(android.os.Build.VERSION.SDK) < 5 
       && keyCode == KeyEvent.KEYCODE_BACK 
       && event.getRepeatCount() == 0) { 
      Log.d("CDA", "onKeyDown Called"); 
      onBackPressed(); 
     } 
     return super.onKeyDown(keyCode, event); 
    } 

    public void onBackPressed() { 

     Log.d("CDA", "onBackPressed Called"); 
     mVideoView.stopPlayback(); 
     Intent setIntent = new Intent(VideoPlay1.this,VideoPage.class); 
     startActivity(setIntent); 
     finish(); 
     return; 
    } 
    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; 
     } 
    } 

위의 작업은 비디오를 재생하는 데 더 많은 시간이 걸리므로 다른 방법이 있는지 말해주십시오.

감사합니다.

좋습니다.

답변

1

죄송합니다. 휴대 기기로 동영상을 스트리밍하는 데는 시간이 걸립니다. 코드가 아닌 연결로 제한됩니다.

+0

동시에 스트리밍 및 비디오 재생을위한 다른 방법이 있습니다 – Ramakrishna

+0

MediaPlayer를 사용하여 자신 만의 비디오 플레이어를 만들 수 있지만 VideoView는 그 무엇입니까. 기기가 충분히 빨리 재생되도록 스트리밍하는 즉시 VideoView가 시작되므로 동영상을 재생할 수있는 동영상이 많지 않습니다. – Cameron

0

이미 답변을 받았지만 도움이 될 수있는 추가 기준, 특히 파일 인코딩이있을 수 있습니다.

MP4 비디오는 일반적으로 기본적으로 MOOV ATOM이 끝에 있습니다. "영화 아톰이라고도하는 무 오브 원자는 영화의 각 트랙에 대한 정보가 들어있는 아 바텀뿐만 아니라 영화의 시간 척도, 지속 시간, 표시 특성을 정의합니다." (http://www.adobe.com/devnet/video/articles/mp4_movie_atom.html). 처음에 moov 아톰을 사용하여 비디오를 인코딩하면 (FFMPEG로이 작업을 수행 할 수 있음) 스트리밍 환경이 향상 될 수 있습니다.