freinds,
我正在使用以下代码在我的应用程序中显示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();
任何人都指导我这个问题的解决方案是什么 任何帮助将不胜感激。
答案 0 :(得分:2)
试试此链接
http://r00tsecurity.org/forums/topic/12059-android-videoview-example/
http://www.helloandroid.com/tutorials/how-play-video-and-audio-android
答案 1 :(得分:0)
您可以使用您的文件创建输出流并获取流的绝对路径,然后将路径放入视频视图
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
}
如果警告此视频无法播放,请使用错误列表器
答案 2 :(得分:0)
没有显示来自网站(互联网)的链接。 如果你想播放特定的视频。然后制作原始文件夹并提供以下路径
String path1 =“android.resource:// your package name /”+ R.raw.video name;
Uri uri = Uri.parse(path1);
videoView.setVideoURI(URI);