尝试播放某些流时,Android MediaPlayer返回错误(1,-1004)

时间:2014-07-01 19:05:03

标签: android media-player audio-streaming

正如标题中所说,试图从这样的流中流式传输:

 http://173.192.137.34:8050

LogCat抛出错误(1,-1004),转换为:

  

MEDIA_ERROR_IO:文件或网络相关的操作错误。

同时,这个小样本可以播放许多其他流。 是因为流类型还是别的什么?

package com.example.strm;

import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;

public class EntryActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        try {
            InitializeStream();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void InitializeStream() throws Exception{
        String url = "http://173.192.137.34:8050"; 
        MediaPlayer mediaPlayer = new MediaPlayer();
        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mediaPlayer.setDataSource(url);

        mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                mp.start();
            }
        });
        mediaPlayer.prepareAsync();
    }
}

3 个答案:

答案 0 :(得分:0)

我遇到了类似的问题。对我来说,某些流(基于https的流)需要HTTP基本身份验证。即使我在使用:

SharedPreferences shared_prefs = getApplicationContext().getSharedPreferences("***_prefs", 0);
final String username = shared_prefs.getString("username", null);
final String password = shared_prefs.getString("password", null);

Authenticator.setDefault(new Authenticator() {
    protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(username, password.toCharArray());
    }
});

video_view.setVideoURI(Uri.parse(enclosure_url));

要进行身份验证,setVideoURI期间实际上并未进行身份验证。我必须在协议和域名之间插入基本的HTTP身份验证凭据:

// in this app, only https urls required authentication, your logic may differ
if (enclosure_url.contains("https")) {
    SharedPreferences shared_prefs = getApplicationContext().getSharedPreferences("***_prefs", 0);
    final String username = shared_prefs.getString("username", null);
    final String password = shared_prefs.getString("password", null);

    enclosure_url = enclosure_url.replaceFirst("https://", "https://"+username+":"+password+"@");
}
video_view.setVideoURI(Uri.parse(enclosure_url));

答案 1 :(得分:-2)

替换

mediaPlayer.prepareAsync(); 

mediaPlayer.prepare();

答案 2 :(得分:-2)

我希望此代码能为您提供帮助

MediaPlayer mediaPlayer = new MediaPlayer();
      player.setAudenter code hereioStreamType(AudioManager.STREAM_MUSIC);
            player.setOnCompletionListener(this);
            player.setOnErrorListener(this);
            player.setOnInfoListener(this);
            player.setOnPreparedListener(this);
            player.setOnSeekCompleteListener(this);
            player.setOnVideoSizeChangedListener(this);/*
                player.setWakeMode(getApplicationContext(),PowerManager.PARTIAL_WAKE_LOCK);*/

            try {

                player.setDataSource(this, Uri.parse(url));
                player.setOnPreparedListener(myContext);

            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (SecurityException e) {
                e.printStackTrace();
            } catch (IllegalStateException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }