Android:视频无法从非硬编码的URL播放

时间:2012-04-14 02:56:03

标签: android

我正在尝试流式传输视频并使用VideoView播放它。我使用setVideoURI()为视图的源URL提供视图,如下所示。使用像urlString =“www.myvideoserver.com/videos/bigbuckbunny.mp4”这样的硬编码值,视频播放效果很好。但是,当urlString被赋予来自intent的值(来自用户选择视频的上一个活动)时,我收到消息:“抱歉,视频无法播放”。我已经读过,其中一个常见原因是视频格式,如herehere所述。我几乎可以肯定它不是格式问题,因为我可以在修复URL时播放视频(我知道这是因为我可以从Log.d(“PVurl”,urlString)看到; urlString的值是与我修复它的方法完全相同。也就是说,在LogCat中,我将值复制粘贴到行urlString = getIntent()... // "www.myvideoserver.com/videos/bigbuckbunny.mp4"中,但是当urlString设置为intent返回值时它不起作用.LogCat Errror面板给出以下内容:

04-13 17:35:32.786: ERROR/MediaPlayer(2620): error (1, -1007)
04-13 17:35:32.786: ERROR/MediaPlayer(2620): Error (1,-1007)

我在互联网上搜索过但似乎没有人遇到过这样的错误代码。

如果有人知道可能出现什么问题,我将非常感激。谢谢!

public void playvideo () { // obtain URL of the requested video from the intent in previous activity

    try
    {
        urlString =  getIntent().getStringExtra("mypackage.fulladdr");

        if (urlStr != null)
        {
            Log.d("PVurl", urlString);

            VideoView v = (VideoView) findViewById(R.id.videoView1);

            // play video

            v.setVideoURI(Uri.parse(urlString));
            v.setMediaController(new MediaController(this));
            v.start();

            v.setOnCompletionListener(new OnCompletionListener() {
                public void onCompletion(MediaPlayer mp) {
                    showRatingDialog();
                }
            });
        }
    }
    catch (Exception e)
    {
        Log.d("PV_TAG", +e.getMessage());
        e.printStackTrace();
    }
}

4 个答案:

答案 0 :(得分:1)

你没有添加Uri需要找出的方案(http,在你的情况下)。

 urlString = "www.myvideoserver.com/videos/bigbuckbunny.mp4"将其更改为urlString = "http://www.myvideoserver.com/videos/bigbuckbunny.mp4"

答案 1 :(得分:1)

(1,-1007)错误意味着: MEDIA_ERROR_UNKNOWN - "文件或网络相关的操作错误。"

这可能来自一个损坏的文件

另见android.media.MediaPlayer.OnErrorListener#onError的javadoc http://developer.android.com/reference/android/media/MediaPlayer.OnErrorListener.html#onError

@param what the type of error that has occurred:
MEDIA_ERROR_UNKNOWN
MEDIA_ERROR_SERVER_DIED

@param extra an extra code, specific to the error. Typically implementation dependent.
MEDIA_ERROR_IO
MEDIA_ERROR_MALFORMED
MEDIA_ERROR_UNSUPPORTED
MEDIA_ERROR_TIMED_OUT

答案 2 :(得分:0)

我尝试了同样的事情并且能够让它发挥作用。您的代码中可能还有其他内容。这就是我所拥有的:

// Intent to start the activity that launches the video player
Intent i = new Intent(this, ExampleActivity.class);
i.putExtra("url", "http://www.yourvideo.com/yourvid.m4v");
startActivity(i);

以下是处理播放器的代码:

private MediaController mController;
private Uri videoUri; 
private int percentBuffered= 0;
private VideoView videoView;

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

    setContentView(R.layout.your_layout);

    mController = new MediaController(this);

    videoView = (VideoView) findViewById(R.id.videoView1);

    // access String from the intent that launched this Activity
    videoUri = Uri.parse(getIntent().getStringExtra("url"));
}

@Override
public void onResume() {
    super.onResume();
    videoView.setOnCompletionListener(this);
    try {
        videoView.setVideoURI(videoUri);
        mController.setMediaPlayer(videoView);
        videoView.setMediaController(mController);
        videoView.requestFocus();
        videoView.start();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

@Override
protected void onPause() {
    super.onPause();
    finish();
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    mController.show();
    return super.onTouchEvent(event);
}

@Override
public int getBufferPercentage() {
    return percentBuffered;
}

@Override
public int getCurrentPosition() { 
    return videoView.getCurrentPosition();
}

@Override
public int getDuration() {
    return videoView.getDuration();
}

@Override
public boolean isPlaying() {
    return videoView.isPlaying();
}

@Override
public void pause() {
    videoView.pause();
}

@Override
public void seekTo(int pos) {
    videoView.seekTo(pos);
}

@Override
public void start() {
    videoView.start();
}

@Override
public void onBufferingUpdate(MediaPlayer mp, int percent) {
    percentBuffered = percent;
}

@Override
public void onCompletion(MediaPlayer mp) {

}

public boolean canPause() {
    return true;
}

public boolean canSeekBackward() {
    return true;
}

public boolean canSeekForward() {
    return true;
}

答案 3 :(得分:0)

好吧,经过几个徒劳的挖掘工作后发现MediaPlayer错误(1,-1007)的含义,我发现了这个问题。

实际上,这与线路相当不幸的事实有关 。getIntent()getStringExtra( “mypackage.fulladdr”); 返回一个末尾带有额外空格的字符串。我认为这是getIntent()的实现问题。 任何人都知道在哪里报告这些错误,这将是伟大的。

感谢@Akhil和@javaMe的尝试!