0

我正嘗試在視頻Android視頻中播放YouTube視頻。這是給不能播放這個視頻。我試圖像下面..Android VideoView YouTube視頻 - 無法播放此視頻錯誤

MainActivity.java

public class MainActivity extends AppCompatActivity { 

    // Widgets 
    private VideoView vv_youtube; 

    // Variables 
    private String base_url = "https://www.youtube.com/watch?v=K8lppDEOkcM"; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     vv_youtube = (VideoView) findViewById(R.id.vv_youtube); 

     Uri video = Uri.parse(base_url); 
     MediaController mediaController = new MediaController(this); 
     mediaController.setAnchorView(vv_youtube); 
     vv_youtube.requestFocus(); 
     vv_youtube.setMediaController(mediaController); 
     vv_youtube.setVideoURI(video); 

     vv_youtube.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { 
      @Override 
      public void onPrepared(MediaPlayer arg0) { 
       vv_youtube.start(); 
      } 
     }); 

     vv_youtube.setOnErrorListener(new MediaPlayer.OnErrorListener() { 
      @Override 
      public boolean onError(MediaPlayer mp, int what, int extra) { 
       Log.e("Error is: ", String.valueOf(extra)); 
       return false; 
      } 
     }); 

     vv_youtube.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { 
      @Override 
      public void onCompletion(MediaPlayer mp) { 
       Log.e("Complete is: ", mp.toString()); 
      } 
     }); 
    } 
} 

logcat的

二月11日至19日:21:45.741 21929-21961/com.example.inbridge08。 videoviewyoutube I/OpenGLRenderer:初始化的EGL,版本1.4 11-19 02:21:45.772 21929-21961/com.example.inbridge08.videoviewyoutube D/OpenGLRenderer:啓用調試模式0 11-19 02:21:45.799 21929-21961/com .example.inbridge08.videoviewyoutube W/EGL_emulation:eglSurfaceAttrib not implemented 11-19 02:21: 45.799 21929-21961/com.example.inbridge08.videoviewyoutube W/OpenGLRenderer:Failed to set EGL_SWAP_BEHAVIOR on surface 0xe2b19220,error = EGL_SUCCESS 11-19 02:21:45.898 21929-21929/com.example.inbridge08.videoviewyoutube D/MediaPlayer:無法在客戶端打開文件,嘗試服務器端11-19 02:21:46.421 21929-21947/com.example.inbridge08.videoviewyoutube E/MediaPlayer:error(1,-2147483648)11-19 02:21:46.421 21929-21929/com.example.inbridge08.videoviewyoutube E/MediaPlayer:錯誤(1,-2147483648)11-19 02:21:46.421 21929-21929/com.example.inbridge08.videoviewyoutube D/VideoView:Error:1, - 2147483648 11-19 02:21:46.421 21929-21929/com.example.inbridge08.videoviewyoutube E/Error is :: -2147483648

[11-19 02:21:46.513 82:82 D /]插座斷開連接

我在谷歌搜索了兩天,然後決定必須使用YouTube Android播放器API在視頻視圖中播放YouTube視頻。如果我將網址替換爲其他網址(不是YouTube視頻網址)它工作正常。

我在模擬器和設備上都試過。不適用於兩者。

在此先感謝!

回答

0

嘗試使用MediaPlayer class在VideoView中使用播放視頻。下面是使用MediaPlayer guide從URI的播放視頻拍攝的片段:

通過HTTP流從遠程URL播放看起來是這樣的:

String url = "http://........"; // your URL here 
MediaPlayer mediaPlayer = new MediaPlayer(); 
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); 
mediaPlayer.setDataSource(url); 
mediaPlayer.prepare(); // might take long! (for buffering, etc) 
mediaPlayer.start(); 

Note: If you're passing a URL to stream an online media file, the file must be capable of progressive download.

您還可以檢查此SO thread額外的參考。

相關問題