ARCore-在增强的图像表面播放YouTube视频

时间:2019-06-10 14:12:22

标签: android youtube augmented-reality arcore android-youtube-api

我正在尝试在使用Google ARCore库构建的android应用中通过增强的图像目标表面播放来自YouTube的视频。 对于本地视频文件,我没有问题,我可以使用MediaPlayer对象,如下所示:

MediaPlayer mediaPlayer = MediaPlayer.create(context, R.raw.my_vid_local_file);
mediaPlayer.setSurface(texture.getSurface());
mediaPlayer.setLooping(true);
if (!mediaPlayer.isPlaying()) {
  mediaPlayer.start();

  // Wait to set the renderable until the first frame of the  video becomes available.
  // This prevents the renderable from briefly appearing as a black quad before the video
  // plays.
  texture.getSurfaceTexture().setOnFrameAvailableListener(
        (SurfaceTexture surfaceTexture) -> {
          videoPlayerTexture.getNow(null).getMaterial().setExternalTexture("videoTexture", texture);
          videoPlayerTexture.getNow(null).getMaterial().setFloat4("keyColor", CHROMA_KEY_COLOR);
          mediaPlayerNode.setRenderable(videoPlayerTexture.getNow(null));
          texture.getSurfaceTexture().setOnFrameAvailableListener(null);
        });
} else {
  mediaPlayerNode.setRenderable(videoPlayerTexture.getNow(null));
}

一切正常,但我很想从YouTube获取视频数据源,因此无需再次从商店下载我的应用即可更改它。

我阅读了此主题: Android Java Youtube API v3.0 get video RTSP link 关于在YouTube API v3中建立RTSP链接,但是当我将其作为MediaPlayer对象数据源时,会出现异常。

我修改了以下代码:

MediaPlayer mediaPlayer = new MediaPlayer();
try {
  mediaPlayer.setDataSource(context, Uri.parse("https://r8---sn-nx5cvox-hpay.googlevideo.com/videoplayback?expire=...and so on"));
  mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
    @Override
    public void onPrepared(MediaPlayer mp) {
      if (!mediaPlayer.isPlaying()) {
        mediaPlayer.start();

        // Wait to set the renderable until the first frame of the  video becomes available.
        // This prevents the renderable from briefly appearing as a black quad before the video
        // plays.
        texture.getSurfaceTexture().setOnFrameAvailableListener(
                (SurfaceTexture surfaceTexture) -> {
                  videoPlayerTexture.getNow(null).getMaterial().setExternalTexture("videoTexture", texture);
                  videoPlayerTexture.getNow(null).getMaterial().setFloat4("keyColor", CHROMA_KEY_COLOR);
                  mediaPlayerNode.setRenderable(videoPlayerTexture.getNow(null));
                  texture.getSurfaceTexture().setOnFrameAvailableListener(null);
                });
      } else {
        mediaPlayerNode.setRenderable(videoPlayerTexture.getNow(null));
      }
    }
  });
  mediaPlayer.prepare();
}catch(Exception e) {
  e.printStackTrace();
  Log.e(TAG, e.getMessage());
}

错误是:

D/MediaHTTPConnectionEx: [response code] STATUS CODE:403
I/native: tracking.cc:3027 no history. confidence : 1.0
D/MediaHTTPConnectionEx: [ResponseMessage]:Forbidden
E/MediaHTTPConnectionEx: MediaHTTPConnectionEx
com.lge.media.MediaHTTPConnectionEx 411 seekTo exception toString: java.io.IOException
D/MediaHTTPConnectionEx: connect
D/MediaHTTPConnectionEx: disconnecting
D/MediaHTTPConnectionEx: disconnected
D/MediaHTTPConnectionEx: [seekToEx] offset:0/mCurrentOffset:-1
D/MediaHTTPConnectionEx: proxy null port 0
I/native: tracking.cc:3027 no history. confidence : 1.0
D/MediaHTTPConnectionEx: [response code] STATUS CODE:403
D/MediaHTTPConnectionEx: [ResponseMessage]:Forbidden
E/MediaHTTPConnectionEx: MediaHTTPConnectionEx com.lge.media.MediaHTTPConnectionEx 411 seekTo exception toString: java.io.IOException
D/MediaHTTPConnectionEx: connect
D/MediaHTTPConnectionEx: disconnecting
D/MediaHTTPConnectionEx: disconnected
...

会有一种使用YouTube API KEY的方法,或者我是完全错误的方法吗?

谢谢你的建议

Carlo Tassi

1 个答案:

答案 0 :(得分:0)

YouTube可以使用自己的网页,通过嵌入式HTML5在另一个网页上或使用YouTube移动API播放。

关键的共同因素是它们都保留了YouTube品牌和外观。

有一些获取原始视频URL的方法示例,因此您可以直接播放原始视频URL(例如https://stackoverflow.com/a/9740907/334402),但是如果您使用原始视频URL和MediaPlayer,则会失去Youtube的外观和感觉,通常不是他们支持的东西。

如果您只是想播放自己有其他来源或可以自己托管的视频,那么您的方法就不错了。