我无法从https链接播放视频

时间:2013-01-28 07:39:02

标签: android android-mediaplayer

我在Android应用程序中使用Media Player。来自http链接的视频正在所有版本的Android中播放,但https链接仅在Android 4.1.2中播放,但在较低版本中它不播放。显示以下错误..

java.io.IOException: Prepare failed.: status=0x1 Error(1,-18).

我正在使用以下代码

   MediaPlayerplayer = new MediaPlayer();
    player.setOnPreparedListener(this);
player.setOnCompletionListener(this);
player.setOnBufferingUpdateListener(this);
player.setOnSeekCompleteListener(this);
player.setScreenOnWhilePlaying(true);
player.setDisplay(holder);

    try {
String videoUrl = getIntent().getExtras().get("url").toString();
    File mediaFile = new File(fileName);
if (mediaFile.exists()) {
    FileInputStream fi = new FileInputStream(mediaFile);
    player.setDataSource(fi.getFD());                   
    }
else{
    player.setDataSource(videoUrl);
}

3 个答案:

答案 0 :(得分:2)

尝试这个

  try {
        setContentView(R.layout.videodisplay);
        String link="Yourvideo link";
        VideoView videoView = (VideoView) findViewById(R.id.VideoView);
        MediaController mediaController = new MediaController(this);
        mediaController.setAnchorView(videoView);
        Uri video = Uri.parse(link);
        videoView.setMediaController(mediaController);
        videoView.setVideoURI(video);
        videoView.start();
    } catch (Exception e) {
        // TODO: handle exception
        Toast.makeText(this, "Error connecting", Toast.LENGTH_SHORT).show();
    }

并在android manifest.xml中提供互联网权限,如果您想使用媒体播放器播放,请点击here

答案 1 :(得分:1)

3.1以上的Android版本仅支持HTTPS媒体文件。请参阅official link

答案 2 :(得分:1)

经过长时间的努力,我找到了解决方案,这就是我与你分享的原因,因为它需要太多时间才能找到答案

  

覆盖您的videoview类及以下代码。

 @Override
public void setVideoURI(Uri uri) {
    super.setVideoURI(uri);
    try {
        KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
        trustStore.load(null, null);
        MySSLSocketFactory sf = new MySSLSocketFactory(trustStore);
        sf.setHostnameVerifier(MySSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        sf.fixHttpsURLConnection();
        HostnameVerifier hostnameVerifier = org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;
        HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier);
    } catch (Exception e) {
        e.printStackTrace();
    }
}