2013-02-26 34 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