Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在检索you tube中的视频时出错_Android_Youtube - Fatal编程技术网

Android 在检索you tube中的视频时出错

Android 在检索you tube中的视频时出错,android,youtube,Android,Youtube,我正在制作一个应用程序,在其中我可以为您播放YouTube视频并在我的应用程序中显示,但我收到一条错误消息,在检索视频时出错。我的代码片段如下: public class Rads extends Activity { public static final String SCHEME_YOUTUBE_VIDEO = "ytv"; public static final String SCHEME_YOUTUBE_PLAYLIST = "ytpl"; static final String

我正在制作一个应用程序,在其中我可以为您播放YouTube视频并在我的应用程序中显示,但我收到一条错误消息,在检索视频时出错。我的代码片段如下:

public class Rads extends Activity {

public static final String SCHEME_YOUTUBE_VIDEO = "ytv";
public static final String SCHEME_YOUTUBE_PLAYLIST = "ytpl";

static final String YOUTUBE_VIDEO_INFORMATION_URL = "http://www.youtube.com/get_video_info?&video_id=";
static final String YOUTUBE_PLAYLIST_ATOM_FEED_URL = "http://gdata.youtube.com/feeds/api/playlists/";

protected ProgressBar mProgressBar;
protected TextView    mProgressMessage;
protected VideoView   mVideoView;

public final static String MSG_INIT = "com.keyes.video.msg.init";
protected String      mMsgInit       = "Initializing";

public final static String MSG_DETECT = "com.keyes.video.msg.detect";
protected String      mMsgDetect     = "Detecting Bandwidth";

public final static String MSG_PLAYLIST = "com.keyes.video.msg.playlist";
protected String      mMsgPlaylist   = "Determining Latest Video in YouTube Playlist";

public final static String MSG_TOKEN = "com.keyes.video.msg.token";
protected String      mMsgToken      = "Retrieving YouTube Video Token";

public final static String MSG_LO_BAND = "com.keyes.video.msg.loband";
protected String      mMsgLowBand    = "Buffering Low-bandwidth Video";

public final static String MSG_HI_BAND = "com.keyes.video.msg.hiband";
protected String      mMsgHiBand     = "Buffering High-bandwidth Video";

public final static String MSG_ERROR_TITLE = "com.keyes.video.msg.error.title";
protected String      mMsgErrorTitle = "Communications Error";

public final static String MSG_ERROR_MSG = "com.keyes.video.msg.error.msg";
protected String      mMsgError      = "An error occurred during the retrieval of the video.  This could be due to network issues or YouTube protocols.  Please try again later.";

/** Background task on which all of the interaction with YouTube is done */
protected QueryYouTubeTask mQueryYouTubeTask;

protected String mVideoId = null;

@Override
protected void onCreate(Bundle pSavedInstanceState) {
    super.onCreate(pSavedInstanceState);

    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

    // create the layout of the view
    setupView();

    // determine the messages to be displayed as the view loads the video
    extractMessages();

    // set the flag to keep the screen ON so that the video can play without the screen being turned off
    getWindow().setFlags(android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

    mProgressBar.bringToFront();
    mProgressBar.setVisibility(View.VISIBLE);
    mProgressMessage.setText(mMsgInit);

    // extract the playlist or video id from the intent that started this video

    Uri lVideoIdUri = this.getIntent().getData();

    if(lVideoIdUri == null){
        Log.i(this.getClass().getSimpleName(), "No video ID was specified in the intent.  Closing video activity.");
        finish();
    }
    String lVideoSchemeStr = lVideoIdUri.getScheme();
    String lVideoIdStr     = lVideoIdUri.getEncodedSchemeSpecificPart();
    if(lVideoIdStr == null){
        Log.i(this.getClass().getSimpleName(), "No video ID was specified in the intent.  Closing video activity.");
        finish();
    }
    if(lVideoIdStr.startsWith("//")){
        if(lVideoIdStr.length() > 2){
            lVideoIdStr = lVideoIdStr.substring(2);
        } else {
            Log.i(this.getClass().getSimpleName(), "No video ID was specified in the intent.  Closing video activity.");
            finish();
        }
    }

    ///////////////////
    // extract either a video id or a playlist id, depending on the uri scheme
    YouTubeId lYouTubeId = null;
    if(lVideoSchemeStr != null && lVideoSchemeStr.equalsIgnoreCase(SCHEME_YOUTUBE_PLAYLIST)){
        lYouTubeId = new PlaylistId(lVideoIdStr);
    }

    else if(lVideoSchemeStr != null && lVideoSchemeStr.equalsIgnoreCase(SCHEME_YOUTUBE_VIDEO)){
        lYouTubeId = new VideoId(lVideoIdStr);
    }

    if(lYouTubeId == null){
        Log.i(this.getClass().getSimpleName(), "Unable to extract video ID from the intent.  Closing video activity.");
        finish();
    }

    mQueryYouTubeTask = (QueryYouTubeTask) new QueryYouTubeTask().execute(lYouTubeId);
}

/**
 * Determine the messages to display during video load and initialization. 
 */
private void extractMessages() {
    Intent lInvokingIntent = getIntent();
    String lMsgInit = lInvokingIntent.getStringExtra(MSG_INIT);
    if(lMsgInit != null){
        mMsgInit = lMsgInit;
    }
    String lMsgDetect = lInvokingIntent.getStringExtra(MSG_DETECT);
    if(lMsgDetect != null){
        mMsgDetect = lMsgDetect;
    }
    String lMsgPlaylist = lInvokingIntent.getStringExtra(MSG_PLAYLIST);
    if(lMsgPlaylist != null){
        mMsgPlaylist = lMsgPlaylist;
    }
    String lMsgToken = lInvokingIntent.getStringExtra(MSG_TOKEN);
    if(lMsgToken != null){
        mMsgToken = lMsgToken;
    }
    String lMsgLoBand = lInvokingIntent.getStringExtra(MSG_LO_BAND);
    if(lMsgLoBand != null){
        mMsgLowBand = lMsgLoBand;
    }
    String lMsgHiBand = lInvokingIntent.getStringExtra(MSG_HI_BAND);
    if(lMsgHiBand != null){
        mMsgHiBand = lMsgHiBand;
    }
    String lMsgErrTitle = lInvokingIntent.getStringExtra(MSG_ERROR_TITLE);
    if(lMsgErrTitle != null){
        mMsgErrorTitle = lMsgErrTitle;
    }
    String lMsgErrMsg = lInvokingIntent.getStringExtra(MSG_ERROR_MSG);
    if(lMsgErrMsg != null){
        mMsgError = lMsgErrMsg;
    }
}

/**
 * Create the view in which the video will be rendered.
 */
private void setupView() {
    LinearLayout lLinLayout = new LinearLayout(this);
    lLinLayout.setId(1);
    lLinLayout.setOrientation(LinearLayout.VERTICAL);
    lLinLayout.setGravity(Gravity.CENTER);
    lLinLayout.setBackgroundColor(Color.BLACK);

    LayoutParams lLinLayoutParms = new LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT);
    lLinLayout.setLayoutParams(lLinLayoutParms);

    this.setContentView(lLinLayout);


    RelativeLayout lRelLayout = new RelativeLayout(this);
    lRelLayout.setId(2);
    lRelLayout.setGravity(Gravity.CENTER);
    lRelLayout.setBackgroundColor(Color.BLACK);
    android.widget.RelativeLayout.LayoutParams lRelLayoutParms = new android.widget.RelativeLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT);
    lRelLayout.setLayoutParams(lRelLayoutParms);
    lLinLayout.addView(lRelLayout);

    mVideoView = new VideoView(this);
    mVideoView.setId(3);
    android.widget.RelativeLayout.LayoutParams lVidViewLayoutParams = new android.widget.RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    lVidViewLayoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
    mVideoView.setLayoutParams(lVidViewLayoutParams);
    lRelLayout.addView(mVideoView);

    mProgressBar = new ProgressBar(this);
    mProgressBar.setIndeterminate(true);
    mProgressBar.setVisibility(View.VISIBLE);
    mProgressBar.setEnabled(true);
    mProgressBar.setId(4);
    android.widget.RelativeLayout.LayoutParams lProgressBarLayoutParms = new android.widget.RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    lProgressBarLayoutParms.addRule(RelativeLayout.CENTER_IN_PARENT);
    mProgressBar.setLayoutParams(lProgressBarLayoutParms);
    lRelLayout.addView(mProgressBar);

    mProgressMessage = new TextView(this);
    mProgressMessage.setId(5);
    android.widget.RelativeLayout.LayoutParams lProgressMsgLayoutParms = new android.widget.RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    lProgressMsgLayoutParms.addRule(RelativeLayout.CENTER_HORIZONTAL);
    lProgressMsgLayoutParms.addRule(RelativeLayout.BELOW, 4);
    mProgressMessage.setLayoutParams(lProgressMsgLayoutParms);
    mProgressMessage.setTextColor(Color.LTGRAY);
    mProgressMessage.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
    mProgressMessage.setText("...");
    lRelLayout.addView(mProgressMessage);
}

@Override
protected void onDestroy() {
    super.onDestroy();

    YouTubeUtility.markVideoAsViewed(this, mVideoId);

    if(mQueryYouTubeTask != null){
        mQueryYouTubeTask.cancel(true);
    }

    if(mVideoView != null){
        mVideoView.stopPlayback();
    }

    // clear the flag that keeps the screen ON 
    getWindow().clearFlags(android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

    this.mQueryYouTubeTask = null;
    this.mVideoView = null;

}

public void updateProgress(String pProgressMsg){
    try {
        mProgressMessage.setText(pProgressMsg);
    } catch(Exception e) {
        Log.e(this.getClass().getSimpleName(), "Error updating video status!", e);
    }
}

private class ProgressUpdateInfo {

    public String mMsg;

    public ProgressUpdateInfo(String pMsg){
        mMsg = pMsg;
    }
}

/**
 * Task to figure out details by calling out to YouTube GData API.  We only use public methods that
 * don't require authentication.
 * 
 */
private class QueryYouTubeTask extends AsyncTask<YouTubeId, ProgressUpdateInfo, Uri> {

    private boolean mShowedError = false;

    @Override
    protected Uri doInBackground(YouTubeId... pParams) {
        String lUriStr = null;
        String lYouTubeFmtQuality = "17";   // 3gpp medium quality, which should be fast enough to view over EDGE connection
        String lYouTubeVideoId = null;

        if(isCancelled())
            return null;

        try {

            publishProgress(new ProgressUpdateInfo(mMsgDetect));

            WifiManager lWifiManager = (WifiManager) Rads.this.getSystemService(Context.WIFI_SERVICE);
            TelephonyManager lTelephonyManager = (TelephonyManager) Rads.this.getSystemService(Context.TELEPHONY_SERVICE);

            ////////////////////////////
            // if we have a fast connection (wifi or 3g), then we'll get a high quality YouTube video
            if( (lWifiManager.isWifiEnabled() && lWifiManager.getConnectionInfo() != null && lWifiManager.getConnectionInfo().getIpAddress() != 0) ||
                ( (lTelephonyManager.getNetworkType() == TelephonyManager.NETWORK_TYPE_UMTS ||

                   /* icky... using literals to make backwards compatible with 1.5 and 1.6 */       
                   lTelephonyManager.getNetworkType() == 9 /*HSUPA*/  ||
                   lTelephonyManager.getNetworkType() == 10 /*HSPA*/  ||
                   lTelephonyManager.getNetworkType() == 8 /*HSDPA*/  ||
                   lTelephonyManager.getNetworkType() == 5 /*EVDO_0*/  ||
                   lTelephonyManager.getNetworkType() == 6 /*EVDO A*/) 

                 && lTelephonyManager.getDataState() == TelephonyManager.DATA_CONNECTED) 
               ){
                lYouTubeFmtQuality = "18";
            }


            ///////////////////////////////////
            // if the intent is to show a playlist, get the latest video id from the playlist, otherwise the video
            // id was explicitly declared.
            if(pParams[0] instanceof PlaylistId){
                publishProgress(new ProgressUpdateInfo(mMsgPlaylist));
                lYouTubeVideoId = YouTubeUtility.queryLatestPlaylistVideo((PlaylistId) pParams[0]);
            }

            else if(pParams[0] instanceof VideoId){
                lYouTubeVideoId = pParams[0].getId();
            }

            mVideoId = lYouTubeVideoId;

            publishProgress(new ProgressUpdateInfo(mMsgToken));

            if(isCancelled())
                return null;

            ////////////////////////////////////
            // calculate the actual URL of the video, encoded with proper YouTube token
            lUriStr = YouTubeUtility.calculateYouTubeUrl(lYouTubeFmtQuality, true, lYouTubeVideoId);

            if(isCancelled())
                return null;

            if(lYouTubeFmtQuality.equals("17")){
                publishProgress(new ProgressUpdateInfo(mMsgLowBand));
            } else {
                publishProgress(new ProgressUpdateInfo(mMsgHiBand));
            }

        } catch(Exception e) {
            Log.e(this.getClass().getSimpleName(), "Error occurred while retrieving information from YouTube.", e);
        }

        if(lUriStr != null){
            return Uri.parse(lUriStr);
        } else {
            return null;
        }
    }



    @Override
    protected void onPostExecute(Uri pResult) {
        super.onPostExecute(pResult);

        try {
            if(isCancelled())
                return;

            if(pResult == null){
                throw new RuntimeException("Invalid NULL Url.");
            }

            mVideoView.setVideoURI(pResult);

            if(isCancelled())
                return;

            // TODO:  add listeners for finish of video
            mVideoView.setOnCompletionListener(new OnCompletionListener(){

                public void onCompletion(MediaPlayer pMp) {
                    if(isCancelled())
                        return;
                    Rads.this.finish();
                }

            });

            if(isCancelled())
                return;

            final MediaController lMediaController = new MediaController(Rads.this);
            mVideoView.setMediaController(lMediaController);
            lMediaController.show(0);
            //mVideoView.setKeepScreenOn(true);
            mVideoView.setOnPreparedListener( new MediaPlayer.OnPreparedListener() {


                public void onPrepared(MediaPlayer pMp) {
                    if(isCancelled())
                        return;
                    Rads.this.mProgressBar.setVisibility(View.GONE);
                    Rads.this.mProgressMessage.setVisibility(View.GONE);
                }

            });

            if(isCancelled())
                return;

            mVideoView.requestFocus();
            mVideoView.start();
        } catch(Exception e){
            Log.e(this.getClass().getSimpleName(), "Error playing video!", e);

            if(!mShowedError){
                showErrorAlert();
            }
        }
    }

    private void showErrorAlert() {

        try {
            Builder lBuilder = new AlertDialog.Builder(Rads.this);
            lBuilder.setTitle(mMsgErrorTitle);
            lBuilder.setCancelable(false);
            lBuilder.setMessage(mMsgError);

            lBuilder.setPositiveButton("OK", new DialogInterface.OnClickListener(){


                public void onClick(DialogInterface pDialog, int pWhich) {
                    Rads.this.finish();
                }

            });

            AlertDialog lDialog = lBuilder.create();
            lDialog.show();
        } catch(Exception e){
            Log.e(this.getClass().getSimpleName(), "Problem showing error dialog.", e);
        }
    }

    @Override
    protected void onProgressUpdate(ProgressUpdateInfo... pValues) {
        super.onProgressUpdate(pValues);

        Rads.this.updateProgress(pValues[0].mMsg);
    }




}

@Override
protected void onStart() {
    super.onStart();
}

@Override
protected void onStop() {
    super.onStop();
}
公共类Rads扩展活动{
公共静态最终字符串方案\u YOUTUBE\u VIDEO=“ytv”;
公共静态最终字符串方案\u YOUTUBE\u PLAYLIST=“ytpl”;
静态最终字符串YOUTUBE\u视频\u信息\u URL=”http://www.youtube.com/get_video_info?&video_id=";
静态最终字符串YOUTUBE\u PLAYLIST\u ATOM\u FEED\u URL=”http://gdata.youtube.com/feeds/api/playlists/";
受保护的ProgressBar-mProgressBar;
受保护的文本视图mProgressMessage;
受保护的视频视图;
公共最终静态字符串MSG_INIT=“com.keyes.video.MSG.INIT”;
受保护的字符串mMsgInit=“初始化”;
公共最终静态字符串MSG_DETECT=“com.keyes.video.MSG.DETECT”;
受保护字符串mMsgDetect=“检测带宽”;
公共最终静态字符串MSG_PLAYLIST=“com.keyes.video.MSG.PLAYLIST”;
受保护的字符串mMsgPlaylist=“确定YouTube播放列表中的最新视频”;
公共最终静态字符串MSG_TOKEN=“com.keyes.video.MSG.TOKEN”;
受保护的字符串mMsgToken=“检索YouTube视频令牌”;
公共最终静态字符串MSG_LO_BAND=“com.keyes.video.MSG.loband”;
受保护的字符串mMsgLowBand=“缓冲低带宽视频”;
公共最终静态字符串MSG_HI_BAND=“com.keyes.video.MSG.hiband”;
受保护的字符串mMsgHiBand=“缓冲高带宽视频”;
公共最终静态字符串MSG\u ERROR\u TITLE=“com.keyes.video.MSG.ERROR.TITLE”;
受保护的字符串mmsgerrottitle=“通信错误”;
公共最终静态字符串MSG\u ERROR\u MSG=“com.keyes.video.MSG.ERROR.MSG”;
受保护字符串mMsgError=“检索视频时出错。这可能是由于网络问题或YouTube协议造成的。请稍后重试。”;
/**完成与YouTube的所有交互的后台任务*/
受保护的QueryOutubetask MQueryOutubetask;
受保护字符串mVideoId=null;
@凌驾
创建时受保护的void(Bundle pSavedInstanceState){
super.onCreate(pSavedInstanceState);
this.requestWindowFeature(Window.FEATURE\u NO\u TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_全屏,WindowManager.LayoutParams.FLAG_全屏);
//创建视图的布局
setupView();
//确定视图加载视频时要显示的消息
提取消息();
//设置标志以保持屏幕打开,以便在不关闭屏幕的情况下播放视频
getWindow().setFlags(android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
mProgressBar.bringToFront();
mProgressBar.setVisibility(View.VISIBLE);
mProgressMessage.setText(mMsgInit);
//从启动此视频的意图中提取播放列表或视频id
Uri lVideoIdUri=this.getIntent().getData();
if(lVideoIdUri==null){
Log.i(this.getClass().getSimpleName(),“在intent.Closing视频活动中未指定视频ID”);
完成();
}
字符串lVideoSchemeStr=lVideoIdUri.getScheme();
字符串lvideidstr=lVideoIdUri.getEncodedSchemeSpecificPart();
if(lvideidstr==null){
Log.i(this.getClass().getSimpleName(),“在intent.Closing视频活动中未指定视频ID”);
完成();
}
if(lvideidstr.startsWith(“/”){
如果(lvideidstr.length()>2){
lvideidstr=lvideidstr.子字符串(2);
}否则{
Log.i(this.getClass().getSimpleName(),“在intent.Closing视频活动中未指定视频ID”);
完成();
}
}
///////////////////
//根据uri方案提取视频id或播放列表id
YouTubeId lYouTubeId=null;
if(lVideoSchemeStr!=null&&lVideoSchemeStr.equalsIgnoreCase(SCHEME\u YOUTUBE\u播放列表)){
lYouTubeId=新的播放ID(lVideoIdStr);
}
else if(lVideoSchemeStr!=null&&lVideoSchemeStr.equalsIgnoreCase(SCHEME\u YOUTUBE\u VIDEO)){
lYouTubeId=新的视频ID(lVideoIdStr);
}
if(lYouTubeId==null){
Log.i(this.getClass().getSimpleName(),“无法从意图中提取视频ID。正在关闭视频活动”);
完成();
}
mQueryYouTubeTask=(QueryYouTubeTask)新建QueryYouTubeTask().execute(lYouTubeId);
}
/**
*确定视频加载和初始化期间要显示的消息。
*/
私有消息(){
Intent linvokingtent=getIntent();
字符串lMsgInit=lInvokingIntent.getStringExtra(MSG_INIT);
if(lMsgInit!=null){
mMsgInit=lMsgInit;
}
字符串lMsgDetect=lInvokingIntent.getStringExtra(MSG_DETECT);
if(lMsgDetect!=null){
mMsgDetect=lMsgDetect;
}
String lMsgPlaylist=lInvokingIntent.getStringExtra(MSG_PLAYLIST);
if(lMsgPlaylist!=null){
mmsglaylist=lMsgPlaylist;
}
字符串lMsgToken=lInvokingIntent.getStringExtra(MSG_TOKEN);
if(lMsgToken!=null){
mMsgToken=lMsgToken;
}
字符串lMsgLoBand=lInvokingIntent.getStringExtra(MSG_lou_BAND);
if(lMsgLoBand!=null){
mMsgLowBand=lMsgLoBand;
}
字符串lMsgHiBand=lInvokingIntent.getStringExtra(MSG_HI_BAND);
if(lMsgHiBand!=null){
mMsgHiBand=lMsgHiBand;
}
字符串lMsgErrTitle=lInvokingIntent.getStringExtra(MSG\u ERROR\u TITLE);
if(lMsgErrTitle!=null){
mmsgerrtitle=lMsgErrTitle;
}
字符串lMsgErrMsg=lInvokingIntent.getStringExtra(MSG\u ERROR\u MSG);
if(lMsgErrMsg!=null){
mMsgError=lMsgErrMsg;
}
}
/**
*创建将在其中渲染视频的视图。
*/
私有void setupView(){
LinearLayout=新的LinearLayout(本);
setId(1);
lLinLayout.setOrientation(LinearLayout.VERTICA