如何播放url中的视频

发布于 2024-08-29 01:15:40 字数 944 浏览 2 评论 0原文

我是 android 开发的初学者,尝试从链接播放视频。但它给出错误“抱歉,我们无法播放此视频”。我尝试了很多链接,但所有链接都显示相同的错误。

我的代码如下

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

I am beginner in android development and try to play video from link. But it's giving error "sorry,we can't play this video". I tried so many links but for all links its show same error.

My code is the following

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 shows following error message:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

欢烬 2024-09-05 01:15:40

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

    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”开始,它是一个小型轻量级视频流,然后尝试“path”,它的分辨率比“path1”更高,对于手机来说是完美的高分辨率。

It has something to do with your link and content. Try the following two links:

    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();

Start with "path1", it is a small light weight video stream and then try the "path", it is a higher resolution than "path1", a perfect high resolution for the mobile phone.

舟遥客 2024-09-05 01:15:40

试试这个:

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();

Try this:

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();
怎会甘心 2024-09-05 01:15:40
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();               
        }
    });
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();               
        }
    });
夜灵血窟げ 2024-09-05 01:15:40

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

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

你的 VideoView xml 是这样的


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

 FullscreenVideoLayout videoLayout;

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

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

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

如果想了解更多信息,请访问此处,上面的代码片段也来自此处。归功于 Toshiro

编辑:
gradle 路径已更新。现在编译

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

You can do it using FullscreenVideoView class. Its a small library project. It's video progress dialog is build in. it's gradle is :

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

your VideoView xml is like this

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

In your activity , initialize it using this way:

    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();
        }

If want to know more then visit here, where the code snippets above also come from. Credits to Toshiro

Edit:
gradle path has been updated. compile it now

compile 'com.github.rtoshiro.fullscreenvideoview:fullscreenvideoview:1.1.2'
灯角 2024-09-05 01:15:40

尝试 Exoplayer2

https://github.com/google/ExoPlayer

它是高度可定制的

    private void initializePlayer() {
         player = ExoPlayerFactory.newSimpleInstance(
             new DefaultRenderersFactory(this),
             new DefaultTrackSelector(), new DefaultLoadControl());

         playerView.setPlayer(player);

         player.setPlayWhenReady(playWhenReady);
         player.seekTo(currentWindow, playbackPosition);

Uri uri = Uri.parse(getString(R.string.media_url_mp3));
     MediaSource mediaSource = buildMediaSource(uri);
     player.prepare(mediaSource, true, false);
    }

private MediaSource buildMediaSource(Uri uri) {
  return new ExtractorMediaSource.Factory(
      new DefaultHttpDataSourceFactory("exoplayer-codelab")).
      createMediaSource(uri);
}

@Override
public void onStart() {
 super.onStart();
 if (Util.SDK_INT > 23) {
   initializePlayer();
 }
}

查看此网址了解更多信息详细信息

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

Try Exoplayer2

https://github.com/google/ExoPlayer

It is highly customisable

    private void initializePlayer() {
         player = ExoPlayerFactory.newSimpleInstance(
             new DefaultRenderersFactory(this),
             new DefaultTrackSelector(), new DefaultLoadControl());

         playerView.setPlayer(player);

         player.setPlayWhenReady(playWhenReady);
         player.seekTo(currentWindow, playbackPosition);

Uri uri = Uri.parse(getString(R.string.media_url_mp3));
     MediaSource mediaSource = buildMediaSource(uri);
     player.prepare(mediaSource, true, false);
    }

private MediaSource buildMediaSource(Uri uri) {
  return new ExtractorMediaSource.Factory(
      new DefaultHttpDataSourceFactory("exoplayer-codelab")).
      createMediaSource(uri);
}

@Override
public void onStart() {
 super.onStart();
 if (Util.SDK_INT > 23) {
   initializePlayer();
 }
}

Check this url for more details

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

只为一人 2024-09-05 01:15:40

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

videoview 无法支持某些编解码器

我建议您使用媒体播放器,当出现“抱歉,无法播放视频”时

please check this link :
http://developer.android.com/guide/appendix/media-formats.html

videoview can't support some codec .

i suggested you to use mediaplayer , when get "sorry , can't play video"

染火枫林 2024-09-05 01:15:40

我也被这个问题困扰了。
我从服务器得到了正确的响应,但无法播放视频。经过很长时间,我在这里找到了解决方案。
也许,将来这个链接将失效。
所以,这是我的正确代码

    Uri video = Uri.parse("Your link should be in this place "); 
    mVideoView.setVideoURI(video); 

   mVideoView.setZOrderOnTop(true); //Very important line, add it to Your code
    mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { 
        @Override 
        public void onPrepared(MediaPlayer mediaPlayer) {
  // here write another part of code, which provides starting the video
  }}

I also got stuck with this issue.
I got correct response from server, but couldn`t play video. After long time I found a solution here.
Maybe, in future this link will be invalid.
So, here is my correct code

    Uri video = Uri.parse("Your link should be in this place "); 
    mVideoView.setVideoURI(video); 

   mVideoView.setZOrderOnTop(true); //Very important line, add it to Your code
    mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { 
        @Override 
        public void onPrepared(MediaPlayer mediaPlayer) {
  // here write another part of code, which provides starting the video
  }}
口干舌燥 2024-09-05 01:15:40

检查这个 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>

Check this UniversalVideoView library its simple and straight forward with controller as well.

Here is the code to play the video
Add this dependancyy in build.gradle

 implementation 'com.linsea:universalvideoview:1.1.0@aar'

Java Code

        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 Code

<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>
絕版丫頭 2024-09-05 01:15:40

检查你的手机是否支持该视频格式。即使我在播放3gp文件时出现问题,但播放mp4文件却完美。

Check whether your phone supports the video format or not.Even I had the problem when playing a 3gp file but it played a mp4 file perfectly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文