2012-01-30 79 views
12

我正在使用以下庫將YouTube視頻傳輸到Android應用程序。使用三星電話時,流式傳輸到VideoView只能在Wifi上播放

http://code.google.com/p/android-youtube-player/source/browse/trunk/OpenYouTubeActivity/src/com/keyes/youtube/OpenYouTubePlayerActivity.java?r=3

我能夠成功地發揮對HTC和摩托羅拉手機的視頻通過3G和Wifi。但是,在Samsung Galaxy(Epic 4G)和Samsung Galaxy II手機上,我只能使用Wifi進行遊戲。 3G給了我這個錯誤:「無法播放視頻,很抱歉,無法播放此視頻。」

我試過強迫低質量的YouTube流媒體,但這並沒有幫助。我在我的日誌中看到Start()在兩種情況下都被調用(3G/Wifi)。這是VideoView的問題嗎?有沒有解決方法?

編輯2

視頻會從YouTube API的到來。我嘗試過使用嵌入式和普通流,以及最低質量的可用流(每個視頻不同)。此外,我不認爲這是一個編碼問題,因爲相同的視頻使用Wifi正確播放。

編輯1

我也收到以下輸出不管閹視頻的播放使用WiFi或不使用3G。

01-30 15:22:38.305: E/MediaPlayer(3831): error (1, -1) 
01-30 15:22:38.305: E/MediaPlayer(3831): callback application 
01-30 15:22:38.305: E/MediaPlayer(3831): back from callback 
01-30 15:22:38.309: E/MediaPlayer(3831): Error (1,-1) 

根據該Link,這些錯誤意味着以下(我認爲):

/* 
Definition of first error event in range (not an actual error code). 
*/ 
const PVMFStatus PVMFErrFirst = (-1); 
/* 
Return code for general failure 
*/ 
const PVMFStatus PVMFFailure = (-1); 
/* 

/* 
Return code for general success 
*/ 
const PVMFStatus PVMFSuccess = 1; 
/* 

另外添加的混亂。

+1

似乎這個問題影響[其他設備](http://code.google.com/p/android-youtube-player/issues/detail?id=7) – Zakaria 2012-01-30 20:27:13

+0

@Zakaria啊,是的。謝謝你指出。 – 2012-01-30 20:32:55

+0

你能告訴我們你的設備的API級別是什麼?這項工作,並沒有奏效。 – 2012-02-14 22:23:52

回答

4

是的,正如您所想的那樣,這是VideoView中的問題,類似的問題也出現在MediaPlayer中,並且我遇到過類似和奇怪的問題,當我只在3G上播放視頻時遇到問題,不在Wi-Fi上。這通常發生在2.1和2.2左右的設備上,但不是像我目前看到的那樣在更高的API級別上發生。

所以,我可以推薦是做到以下幾點:

首先檢查是否正在運行的設備可以是一種可以有問題,這樣的事情:

//Define a static list of known devices with issues 
static List sIssueDevices=Arrays.asList(new String[]{"HTC Desire","LG-P500","etc"}); 

if(Build.VERSION.SDK_INT<9){ 
    if(sIssueDevices.contains(Build.Device){ 
     //This device may have issue in streaming, take appropriate actions 
    } 
} 

因此,這是最簡單的一部分,檢測運行中的設備在流式傳輸視頻時是否存在問題。現在,我所做的也可能對您有所幫助的是,將Youtube中的視頻緩存在SDCard的文件中,並將該文件設置爲您的VideoViewsource。我會寫一些代碼片段,看看我的做法是如何:當VideoView已經停止文件結束前打會出現

private class GetYoutubeFile extends Thread{ 
    private String mUrl; 
    private String mFile; 
    public GetYotubeFile(String url,String file){ 
     mUrl=url; 
     mFile=file; 
    } 

    @Override 
    public void run() { 
     super.run(); 
     try { 

      File bufferingDir=new File(Environment.getExternalStorageDirectory() 
        +"/YoutubeBuff"); 

      File bufferFile=new File(bufferingDir.getAbsolutePath(), mFile); 
      //bufferFile.createNewFile(); 
      BufferedOutputStream bufferOS=new BufferedOutputStream(
             new FileOutputStream(bufferFile)); 

      URL url=new URL(mUrl); 
      URLConnection connection=url.openConnection(); 
      connection.setRequestProperty("User-Agent", "Mozilla"); 
      connection.connect(); 
      InputStream is=connection.getInputStream(); 
      BufferedInputStream bis=new BufferedInputStream(is,2048); 

      byte[] buffer = new byte[16384]; 
      int numRead; 
      boolean started=false; 
      while ((numRead = bis.read(buffer)) != -1 && !mActivityStopped) { 
       //Log.i("Buffering","Read :"+numRead); 
       bufferOS.write(buffer, 0, numRead); 
       bufferOS.flush(); 
       mBuffPosition += numRead; 
       if(mBuffPosition>120000 &&!started){ 
        Log.e("Player","BufferHIT:StartPlay"); 
        setSourceAndStartPlay(bufferFile); 
        started=true; 
       } 

      } 
      Log.i("Buffering","Read -1?"+numRead+" stop:"+mActivityStopped); 
     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
} 

public void setSourceAndStartPlay(File bufferFile) { 
    try { 
     mPlayer.setVideoPath(bufferFile.getAbsolutePath()); 
     mPlayer.prepare(); 
     mPlayer.start(); 
    } catch (IllegalArgumentException e) { 
     e.printStackTrace(); 
    } catch (IllegalStateException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

另一個問題,因爲沒有足夠的是文件的緩衝。爲此,您需要設置一個onCompletionListener(),如果你是不是在視頻的結尾,則應該從最後一個位置再次啓動視頻播放:

public void onCompletion(MediaPlayer mp) { 
    mPlayerPosition=mPlayer.getCurrentPosition(); 
    try { 
     mPlayer.reset(); 
     mPlayer.setVideoPath(
      new File("mnt/sdcard/YoutubeBuff/"+mBufferFile).getAbsolutePath()); 
     mPlayer.seekTo(mPlayerPosition); 
     mPlayer.start(); 
    } catch (IllegalArgumentException e) { 
     e.printStackTrace(); 
    } catch (IllegalStateException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

} 

最後,當然GetYoutubeFile線程啓動在onCreate()方法:

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    //init views,player,etc 
    new GetYoutubeFile().start(); 
} 

一些修改和調整,我認爲將有此代碼來完成,它可能不是最好的方法,但它幫助了我,我找不到任何替代品。

+0

沒有測試,但絕對是邏輯+1!我只是瀏覽了這個YouTubePlayer代碼,並注意到一些WIFI的檢查。會導致問題嗎?我的意思是你確定它是一個VideoView問題? – 2012-02-15 16:35:31

+0

我不能說我100%確定。但我當然知道,同樣的視頻正在使用3G,而不是在某些設備上使用WiFi,反之亦然。所以我只能認爲這是'VideoView'裏面的東西,而不是視頻格式問題。 – 2012-02-15 17:13:25

0

此消息通常來自視頻的不適當編碼(「無法播放視頻,對不起這個視頻無法播放」)。我一直在苦苦尋找一段時間,現在正確的編碼視頻可以在所有測試過的設備上播放,即使在使用Wifi或3G時也是如此。讓他們知道你是否想知道如何編碼視頻。爲了流式傳輸視頻,我使用了android sdk apis中的演示程序,它的工作原理完美無瑕。

+0

如果是這種情況,那麼爲什麼相同的視頻在wifi上工作? – 2012-02-07 15:18:59

1

我有我自己的way.First這個問題解決,每次讀你的日誌cat.If你有

Error (1,-1) 

,這意味着你會得到sorry,this video can not play message。所以在這種情況下完成這一活動,給自進度條和下載視頻。然後下載後保存在臨時文件夾然後播放它。播放後刪除該文件夾。

閱讀日誌貓---

try { 
    Process process = Runtime.getRuntime().exec("logcat -d"); 
    BufferedReader bufferedReader = new BufferedReader(
    new InputStreamReader(process.getInputStream())); 
    StringBuilder log=new StringBuilder(); 
    String line; 
    while ((line = bufferedReader.readLine()) != null) { 
    log.append(line); 
    } 
} catch (IOException e) { 
} 

閱讀這個答案太。雖然它只是用戶體驗沒有鏈接到應用程序,但它有時在默認應用程序也發生

視頻堆,即使我有完整的3G網絡覆蓋,會說「對不起,這個視頻不能播放」。有一天,我非常生氣,我只是不停地按「好」來解散信息,然後再次按下視頻,只看到「對不起,這個視頻無法播放」的消息。我重複了這個過程(在盲目的憤怒中),但最終在大約5次嘗試後,視頻決定奇蹟般地發揮!

這種方法每次都適合我。大多數視頻不會想要第一次播放,但最終如果我只是堅持不懈,並繼續告訴它播放,即使它告訴我'不能'播放視頻,它會播放!雖然,一些視頻我不得不按「好」,按下視頻,按「好」,然後按視頻等... 20次,然後才真正決定播放。那段時間,我非常接近將手機放在地板上,因爲我對YouTube的工作方式感到很沮喪。

我希望有一種方法可以解決這個問題。似乎沒有人提出解決方案。每個人都只是說「噢,我有同樣的問題」,但沒有人貢獻任何東西。 GOOGLE,在您的手機上解決這個問題。這種情況將在全球範圍內發生,在一系列的安卓手機上。

+0

你對我的回答滿意嗎? – Sameer 2012-02-16 09:51:47

+0

你不必讀取logcat你可以附上onErrorListner回調方法,這會給你這些錯誤 – 2012-07-03 08:27:41

+0

你也可以這樣做..但有時它不會進入onErrorListner – Sameer 2012-07-04 04:33:38

相關問題