朋友,
我正在使用以下代码在我的应用程序中显示mp4视频并面临以下问题
我在Google和stackoverflow上看到了很多与此问题相关的帖子,但每个人都给出自己的建议,没有共同的答案。
1)我在模拟器中看不到视频2)有时在其他电话中很少播放视频,并且大多数情况下会显示上述消息。
我的代码
VideoView myVideoView = (VideoView)findViewById(R.id.videoview);
String viewSource ="http://dev.hpac.dev-site.org/sites/default/files/videos/about/mobile.mp4";
myVideoView.setVideoURI(Uri.parse(viewSource));
myVideoView.setMediaController(new MediaController(this));
myVideoView.requestFocus();
myVideoView.start();
任何人都可以指导我解决这个问题的方法是什么任何帮助将不胜感激。
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();
@SuppressWarnings("resource")
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;
}
}
和
public void initVideo() { try { if (!mVideoView.isPlaying()) { if (url > playList.size() - 1) { url = 0; } String[] playurl = (playList.get(url)).split("\\."); String urlExtention = playurl[playurl.length - 1]; if (urlExtention.equals("mp4")) { playVideo(playList.get(url)); } else if (urlExtention.equals("jpg") || urlExtention.equals("jpeg")) { Intent intentShedule = new Intent(Default_Player.this, ImagePlayer.class); intentShedule.putExtra("imagePath", playList.get(url)); intentShedule.putExtra("urlValue", url); intentShedule.putExtra("playlistType", playlistType); intentShedule.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION); startActivity(intentShedule); finish(); } else { Intent intentShedule = new Intent(Default_Player.this, WebContentView.class); intentShedule.putExtra("webPath", playList.get(url)); intentShedule.putExtra("urlValue", url); intentShedule.putExtra("playlistType", playlistType); intentShedule.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION); startActivity(intentShedule); finish(); } } mVideoView.setOnErrorListener(new MediaPlayer.OnErrorListener() { @Override public boolean onError(MediaPlayer mp, int what, int extra) { System.out.println("set on error listner"); //do somthing if alert this video can not be played return false; } }); mVideoView .setOnCompletionListener(new MediaPlayer.OnCompletionListener() { public void onCompletion(MediaPlayer vmp) { playnew(); } }); } catch (Exception e) { } // TODO Auto-generated method stub }
如果无法播放此视频,请在错误列表器上使用
String path1 =“ android.resource://您的包名称/” + R.raw.video名称;
Uri uri = Uri.parse(path1);videoView.setVideoURI(uri);