2013-02-26 50 views
5

我想在android視頻視圖流視頻表單url。我使用示例API代碼,做小的修改在實現我的需要。我的代碼是Android視頻查看另一個線程&問題與Android 2.1

public class VideoViewDemo extends Activity { 

private static final String TAG = "VideoViewDemo"; 
private String current; 

/** 
* TODO: Set the path variable to a streaming video URL or a local media 
* file path. 
*/ 
private String path = "http://www.boisestatefootball.com/sites/default/files/videos/original/01%20-%20coach%20pete%20bio_4.mp4"; 
private VideoView mVideoView; 

@Override 
public void onCreate(Bundle icicle) { 
    super.onCreate(icicle); 
    setContentView(R.layout.videoview); 
    mVideoView = (VideoView) findViewById(R.id.surface_view); 
    runOnUiThread(new Runnable() { 
     public void run() { 
      playVideo(); 
     } 
    }); 
} 

private void playVideo() { 
    try { 
     // final String path = path; 
     Log.v(TAG, "path: " + path); 
     if (path == null || path.length() == 0) { 
      Toast.makeText(VideoViewDemo.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) { 
       mVideoView.start(); 
       mVideoView.requestFocus(); 
       return; 
      } 
      current = path; 
      mVideoView.setVideoPath(getDataSource(path)); 
      mVideoView.start(); 
      mVideoView.setMediaController(new MediaController(this)); 
      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; 
    } 
} 
} 

在這裏你可以看到我使用uithread的視頻流通過我的上線。就是有什麼辦法來處理這

我想什麼是

new Thered(new Runnable() { 
     public void run() { 
      playVideo(); 
     } 
    }).start(); 

但它無法

而且還同時在Android 2.2的第一個代碼運行運行,它顯示error(-38,0)我n Android 2.1這個錯誤是什麼?我檢查了This File,但無法找出這是什麼錯誤?

有人可以指導我嗎?

+0

有一個按鈕說'播放視頻',並在其點擊功能放置您的Runnable線程代碼。在OnCreate()函數中擁有自己的線程可能會變得混亂,因爲活動創建必須始終保持良好狀態。順便說一下,你是在Android模擬器還是手機上試過這個? – 2013-04-12 18:22:06

+0

@RahulSundar我試過在手機上。我不想使用按鈕。我希望在進入活動時直接播放視頻。這就是爲什麼我沒有使用任何按鈕 – edwin 2013-04-13 14:32:32

+0

好吧。只是爲了檢查線程是否一切正常,從一個按鈕啓動它。然後在OnCreate()函數中使用相同的。爲了確保活動創建完成後視頻呈現開始,請給予足夠的延遲睡眠(5秒)並檢查一切是否正常。 – 2013-04-13 15:32:02

回答

2

您不需要獲取整個視頻,並將其保存在文件系統中,然後運行它。您提到的視頻具有32Mb大小,並且需要很長時間才能通過網絡獲取。相反,您可以直接鏈接到videoview,它會逐步獲取/緩衝視頻並播放它。您試圖在UI線程中獲取視頻數據,這是不可接受的。這裏是更正的代碼,你可以檢查它。

public class VideoViewDemo extends Activity { 

private static final String TAG = "VideoViewDemo"; 
private String current; 

/** 
* TODO: Set the path variable to a streaming video URL or a local media 
* file path. 
*/ 
private String path = "http://www.boisestatefootball.com/sites/default/files/videos/original/01%20-%20coach%20pete%20bio_4.mp4"; 
private VideoView mVideoView; 

private Handler handler; 

@Override 
public void onCreate(Bundle icicle) { 
    super.onCreate(icicle); 
    handler = new Handler(); 
    setContentView(R.layout.videoview); 
    mVideoView = (VideoView) findViewById(R.id.surface_view); 
/* runOnUiThread(new Runnable() { 
     public void run() { 
      playVideo(); 
     } 
    }); 
*/  
    playVideo(); 
    Log.v(TAG, "activity oncreate finished"); 
} 

private void playVideo() { 
    try { 
     // final String path = path; 
     Log.v(TAG, "path: " + path); 
     if (path == null || path.length() == 0) { 
      Toast.makeText(VideoViewDemo.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) { 
       mVideoView.start(); 
       mVideoView.requestFocus(); 
       return; 
      } 
      current = path; 

      mVideoView.setVideoPath(path); 
      mVideoView.start(); 
      mVideoView.setMediaController(new MediaController(VideoViewDemo.this)); 
      mVideoView.requestFocus(); 

     } 
    } catch (Exception e) { 
     Log.e(TAG, "error: " + e.getMessage(), e); 
     if (mVideoView != null) { 
      mVideoView.stopPlayback(); 
     } 
    } 
} 

} 
+0

@edwin,我的回答有用嗎? – Suji 2013-08-08 05:19:16

+0

感謝您的代碼!我還有一個問題,你知道視頻是否會在完全觀看後保存在設備的某個位置?那裏可以嗎?請在這個問題上看到我的[問題](http://stackoverflow.com/questions/23842450/android-where-do-videoview-and-mediacontroller-store-media-downloaded-with-htt) – nburk 2014-05-24 08:52:41