0

我在我的 Android 应用程序中使用媒体播放器。来自 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);
}
4

3 回答 3

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 中授予 Internet 权限,如果您想使用 mediaplayer 播放,请单击此处

于 2013-01-28T08:58:58.193 回答
1

Android 3.1 以上版本仅支持 HTTPS 媒体文件。见官方链接

于 2013-01-28T11:58:55.093 回答
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();
    }
}
于 2016-11-09T14:24:29.127 回答