澳门银河网上官方赌场_老品牌值得信赖

ITKeyword,专注技术干货聚合推荐

注册 | 登录

解决android - Streaming to VideoView only plays on Wifi when using Samsung phones

itPublisher 分享于

2021腾讯云限时秒杀,爆款1核2G云服务器298元/3年!(领取2860元代金券),
地址https://cloud.tencent.com/act/cps/redirect?redirect=1062

2021阿里云最低价产品入口+领取代金券(老用户3折起),
入口地址https://www.aliyun.com/minisite/goods

I am using the following library to stream YouTube videos to an Android application.

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

I am successfully able to play videos on HTC and Motorola phones over 3G and Wifi. However, on Samsung Galaxy (Epic 4G) and Samsung Galaxy II phones I am only able to play using Wifi. 3G gives me this error: "Cannot play video. Sorry this video cannot be played."

I have tried forcing low quality YouTube streaming, but this did not help. I see in my log that Start() is being called in both cases (3G/Wifi). Is this an issue with VideoView? Is there a workaround?

Edit 2

The videos are coming from YouTube API. I have attempted using embedded and normal streams, as well as lowest quality stream available (varying per video). Also, I do not think it is an encoding issue since the same videos play correctly using Wifi.

Edit 1

I also receive the following output regardless of wether video plays using Wifi or does not using 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)

According to this Link, these errors means the following (I think):

/*
 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;
/*

Further adding confusion.

android streaming videoview
|
  this question
edited Feb 7 '12 at 15:18 asked Jan 30 '12 at 20:21 Oh Danny Boy 1,828 7 47 83 1   it seems that this issue affects other devices –  Zakaria Jan 30 '12 at 20:27      @Zakaria Ah, yes. Thank you for pointing that out. –  Oh Danny Boy Jan 30 '12 at 20:32      Can you also tell us what are the API levels of your devices? That work and that don't work. –  Ovidiu Latcu Feb 14 '12 at 22:23      Have you tried play the video over 3G by using the default web browser on Samsung phone, Does it work correctly? –  yorkw Feb 15 '12 at 0:27

 | 

3 Answers
3

解决方法 +200

Yes, as you are thinking, this is a issue in VideoView, similar issues also appear in MediaPlayer, and I've encountered similar and strange issues as you did, I had problems when the video was played only on 3G and not on Wi-Fi. This usually happens on 2.1 and some 2.2 devices, but not on higher API levels as I've seen so far.

So what I can recommend is do the following :

First check if the running device may be one that can have issues, something like this :

//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
     }
}

So this was the simplest part, to detect if the running device may have issues in streaming the video. Now, what I did and may also help you, is buffer the video from Youtube in a file on the SDCard and set that file as the source for your VideoView. I will write some code snippets to see how my approach was :

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

Another issue will arise when the VideoView has stopped playing before the end of file, because not enough was buffered in the file. For this you need to set an onCompletionListener() and if you are not at the end of the video, you should start again the video playback from the last position :

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

}

In the end, of course the GetYoutubeFile thread is started in the onCreate() method :

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

Some modifications and adaptation I think will have to be done for this code, and it may not be the best approach, but it helped me, and I couldn't find any alternative.


|
  this answer
edited Feb 15 '12 at 17:15 answered Feb 15 '12 at 8:38 Ovidiu Latcu 38.8k 11 63 78      did not test but definitely a +1 to the logic! I just skimmed through this YouTubePlayer code and noticed some checks for WIFI. Would that cause a problem? I mean are you certain it is a VideoView problem? –  Sherif elKhatib Feb 15 '12 at 16:35      I can't say that I am 100% sure. But I certainly know that the same video was working on 3G and not on WiFi on some devices, and viceversa on some other devices. So I can only think that is something inside VideoView, and not a video format problem. –  Ovidiu Latcu Feb 15 '12 at 17:13

 | 

I have tackle with this problem in my own way.First every time read your log cat.If you got

Error (1,-1) 

that means you will get sorry,this video can not play message.So in this case finish that activity, give custom progress bar and download video.Then after downloading save it in temporary folder then play it.After playing delete that folder.

To reading log cat---

 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) {
}

Read this answer too. although it is just user experience not linked to app.but it happens sometimes in default application also

Heaps of videos, even if I have full 3G network coverage, will say "Sorry, this video cannot be played". One day, I got so pissed off with it, I just kept pressing 'Okay' to dismiss the message, and then pressed the video again only to see the "Sorry, this video cannot be played" message again. I repeated this process (in my blind anger), but eventually, after about 5 tries, the video decided to miraculously play!

This method pretty much works for me every time. Most videos won't want to play the first time, but eventually if I am just persistent, and keep telling it to play even though it tells me it 'can't' play the video, it will play! Although, some videos i've had to press 'Okay', press the video, press 'Okay', press the video etc... for like 20 times before it actually decided to play. Those times, I have been incredibly close to getting my phone and throwing it down on the floor because of how shitty I am with how youtube won't work.

I wish there was a way to fix this problem. No one seems to have come up with a solution. Everyone just says "oh yeah I have the same problem" but no one contributes anything. GOOGLE, SOLVE THIS PROBLEM ON YOUR PHONES. THIS SEEMS TO BE HAPPENING WORLDWIDE, ON A RANGE OF ANDROID PHONES.


|
  this answer
answered Feb 16 '12 at 5:11 Sameer 9,519 2 49 79      Are you satisfied with my answer? –  Sameer Feb 16 '12 at 9:51      You don't have to read logcat you can attach onErrorListner callback method which will give you these error –  Yahya Arshad Jul 3 '12 at 8:27      You can do it also..but sometimes it does not come into onErrorListner –  Sameer Jul 4 '12 at 4:33      thanks sameer for your information i was planning to use onError listtener for my application but you updated me :) –  Yahya Arshad Jul 6 '12 at 11:04      please tell me is there any callback method for log reading or i have to poll your algo in a thread –  Yahya Arshad Jul 6 '12 at 11:05  |  show more comment

This message often cames from the inappropriate encoding of the video ("Cannot play video. Sorry this video cannot be played.") I was struggling with videoview for a while , now the correct encoded videos play on all tested devices, even when using Wifi or 3G. Let em know if you want to know how to encode the videos. And for streaming the videos I used the demo from android sdk apis and it works flawless.


|
  this answer
answered Feb 7 '12 at 14:27 Iulia Barbu 1,314 9 22      If this is the case, then why do the same videos work on wifi? –  Oh Danny Boy Feb 7 '12 at 15:18

 | 


相关阅读排行


相关内容推荐

最新文章

×

×

请激活账号

为了能正常使用评论、编辑功能及以后陆续为用户提供的其他产品,请激活账号。

您的注册邮箱: 修改

重新发送激活邮件 进入我的邮箱

如果您没有收到激活邮件,请注意检查垃圾箱。

澳门银河网上官方赌场