如何从网址播放视频

时间:2010-04-12 05:27:20

标签: android video-streaming android-videoview

我是Android开发的初学者,尝试从链接播放视频。但它给出了错误"sorry,we can't play this video"。我尝试了很多链接但是对于所有链接它显示相同的错误。

我的代码如下

public class VideoDemo extends Activity {

        private static final String path ="http://demo.digi-corp.com/S2LWebservice/Resources/SampleVideo.mp4";
 private VideoView video;
 private MediaController ctlr;
 @Override
 public void onCreate(Bundle icicle) {
            super.onCreate(icicle);
            getWindow().setFormat(PixelFormat.TRANSLUCENT);
            setContentView(R.layout.videoview);

            video = (VideoView) findViewById(R.id.video);
            video.setVideoPath(path);

            ctlr = new MediaController(this);
            ctlr.setMediaPlayer(video);
            video.setMediaController(ctlr);
            video.requestFocus();
     }
}

Logcat显示以下错误消息:

04-12 15:04:54.245: ERROR/PlayerDriver(554): HandleErrorEvent: PVMFErrTimeout

9 个答案:

答案 0 :(得分:47)

它与您的链接和内容有关。请尝试以下两个链接:

    String path="http://www.ted.com/talks/download/video/8584/talk/761";
    String path1="http://commonsware.com/misc/test2.3gp";

    Uri uri=Uri.parse(path1);

    VideoView video=(VideoView)findViewById(R.id.VideoView01);
    video.setVideoURI(uri);
    video.start();

从“path1”开始,它是一个小的轻量级视频流,然后尝试“路径”,它的分辨率高于“path1”,是手机的完美高分辨率。

答案 1 :(得分:6)

试试这个:

String LINK = "type_here_the_link";
setContentView(R.layout.mediaplayer);
VideoView videoView = (VideoView) findViewById(R.id.video);
MediaController mc = new MediaController(this);
mc.setAnchorView(videoView);
mc.setMediaPlayer(videoView);
Uri video = Uri.parse(LINK);
videoView.setMediaController(mc);
videoView.setVideoURI(video);
videoView.start();

答案 2 :(得分:6)

pDialog = new ProgressDialog(this);

    // Set progressbar message
    pDialog.setMessage("Buffering...");
    pDialog.setIndeterminate(false);
    pDialog.setCancelable(false);
    // Show progressbar
    pDialog.show();

    try {
        // Start the MediaController
        MediaController mediacontroller = new MediaController(this);
        mediacontroller.setAnchorView(mVideoView);      

        Uri videoUri = Uri.parse(videoUrl);
        mVideoView.setMediaController(mediacontroller);
        mVideoView.setVideoURI(videoUri);

    } catch (Exception e) {

        e.printStackTrace();
    }

    mVideoView.requestFocus();
    mVideoView.setOnPreparedListener(new OnPreparedListener() {
        // Close the progress bar and play the video
        public void onPrepared(MediaPlayer mp) {
            pDialog.dismiss();
            mVideoView.start();
        }
    });
    mVideoView.setOnCompletionListener(new OnCompletionListener() {

        public void onCompletion(MediaPlayer mp) {
            if (pDialog.isShowing()) {
                pDialog.dismiss();
            }
            finish();               
        }
    });

答案 3 :(得分:5)

您可以使用FullscreenVideoView课程来完成此操作。它是一个小型图书馆项目。它的视频进度对话框是内置的。它的gradle是:

compile 'com.github.rtoshiro.fullscreenvideoview:fullscreenvideoview:1.1.0'

你的VideoView xml就像这样

<com.github.rtoshiro.view.video.FullscreenVideoLayout
        android:id="@+id/videoview"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

在您的活动中,使用以下方式初始化它:

    FullscreenVideoLayout videoLayout;

videoLayout = (FullscreenVideoLayout) findViewById(R.id.videoview);
        videoLayout.setActivity(this);

        Uri videoUri = Uri.parse("YOUR_VIDEO_URL");
        try {
            videoLayout.setVideoURI(videoUri);

        } catch (IOException e) {
            e.printStackTrace();
        }

就是这样。快乐的编码:)

如果想了解更多信息,请访问here

修改 gradle路径已更新。现在编译

compile 'com.github.rtoshiro.fullscreenvideoview:fullscreenvideoview:1.1.2'

答案 4 :(得分:1)

请检查此链接: http://developer.android.com/guide/appendix/media-formats.html

videoview 无法支持某些编解码器

我建议你使用媒体播放器,对不起,不能播放视频&#34;

答案 5 :(得分:1)

我也遇到了这个问题。 我得到了服务器的正确回复,但无法播放视频。很长一段时间后,我找到了解决方案here。 也许,将来此链接无效。 所以,这是我正确的代码

{{1}}

答案 6 :(得分:1)

尝试Exoplayer2

https://github.com/google/ExoPlayer

高度可定制

class QuestionForm(forms.ModelForm):
    class Meta:
        model = Question
        fields = ['company', 'question_text']
        labels = {
            'company': ('Company'),
            'question_text': ('Question'),

        }
        widgets = {
            'question_text': forms.TextInput(attrs={'placeholder': 'Enter Question'}),
            'company': forms.TextInput(attrs={'id': 'companiesList'}),
        }


ChoiceInlineFormSet = inlineformset_factory(Question,
                                            Choice,
                                            fields=['question', 'choice_text'],
                                            exclude=[], can_delete=False)

查看此网址以获取更多详细信息

https://codelabs.developers.google.com/codelabs/exoplayer-intro/#2

答案 7 :(得分:0)

还可以通过控制器轻松简单地检查此UniversalVideoView库。

这是播放视频的代码
在build.gradle

中添加此依赖项
 implementation 'com.linsea:universalvideoview:1.1.0@aar'

Java代码

        UniversalVideoView mVideoView = findViewById(R.id.videoView);
        Uri uri=Uri.parse("https://firebasestorage.googleapis.com/v0/b/contactform-d9534.appspot.com/o/Vexento%20-%20Masked%20Heroes.mp4?alt=media&token=74c2e448-5b1b-47b7-b761-66409bcfbf56");
        mVideoView.setVideoURI(uri);
        UniversalMediaController mMediaController = findViewById(R.id.media_controller);
        mVideoView.setMediaController(mMediaController);
        mVideoView.start();   

Xml代码

<FrameLayout
            android:id="@+id/video_layout"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:background="@android:color/black">

            <com.universalvideoview.UniversalVideoView
                android:id="@+id/videoView"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_gravity="center"
                app:uvv_autoRotation="true"
                app:uvv_fitXY="false" />

            <com.universalvideoview.UniversalMediaController
                android:id="@+id/media_controller"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                app:uvv_scalable="true" />

        </FrameLayout>

答案 8 :(得分:-1)

检查您的手机是否支持视频格式。即使我在播放3gp文件时遇到问题,但它也能完美播放mp4文件。