你管的视频不是在玩 [英] You tube videos are not playing

查看:202
本文介绍了你管的视频不是在玩的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用OpenYoutubeActivity.jar来播放YouTube视频像这样

 意图videoIntent =新意图(NULL,Uri.parse(YTV:// NG3WygJmiVs),对此,OpenYouTubePlayerActivity.class);
    startActivity(videoIntent);

OpenYouTubePlayerActivity.java在这里

 公共类OpenYouTubePlayerActivity延伸活动{公共静态最后弦乐SCHEME_YOUTUBE_VIDEO =YTV;
公共静态最后弦乐SCHEME_YOUTUBE_PLAYLIST =YTPL;静态最后弦乐YOUTUBE_VIDEO_INFORMATION_URL =htt​​p://www.youtube.com/get_video_info?&video_id=;
静态最后弦乐YOUTUBE_PLAYLIST_ATOM_FEED_URL =htt​​p://gdata.youtube.com/feeds/api/playlists/;保护进度mProgressBar;
保护的TextView mProgressMessage;
保护VideoView mVideoView;公共最后静态字符串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_ERROR_TITLE =com.keyes.video.msg.error.title;
保护字符串mMsgErrorTitle =通讯错误;公共最后静态字符串MSG_ERROR_MSG =com.keyes.video.msg.error.msg;
保护字符串mMsgError =视频的检索过程中出错这可能是由于网络问题或YouTube协议,请稍后再试。在所有与YouTube的交互完成/ **后台任务* /
保护QueryYouTubeTask mQueryYouTubeTask;保护字符串mVideoId = NULL;@覆盖
保护无效的onCreate(捆绑pSavedInstanceState){
    super.onCreate(pSavedInstanceState);    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    。this.getWindow()setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);    //创建视图的布局
    setupView函数();    //确定消息显示为视图加载视频
    extractMessages();    //设置标志,以保持在屏幕上,使得视频可以在没有屏幕播放被关闭
    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    乌里lVideoIdUri = this.getIntent()的getData()。    如果(lVideoIdUri == NULL){
        Log.i(this.getClass()getSimpleName(),无视频ID在意向中指定闭幕影片活动。);
        完();
    }
    串lVideoSchemeStr = lVideoIdUri.getScheme();
    字符串lVideoIdStr = lVideoIdUri.getEn codedSchemeSpecificPart();
    如果(lVideoIdStr == NULL){
        Log.i(this.getClass()getSimpleName(),无视频ID在意向中指定闭幕影片活动。);
        完();
    }
    如果(lVideoIdStr.startsWith(//)){
        如果(lVideoIdStr.length()→2){
            lVideoIdStr = lVideoIdStr.substring(2);
        }其他{
            Log.i(this.getClass()getSimpleName(),无视频ID在意向中指定闭幕影片活动。);
            完();
        }
    }    ///////////////////
    //提取无论是视频ID或播放列表ID,根据不同的URI方案
    YouTubeId lYouTubeId = NULL;
    如果(lVideoSchemeStr = NULL&放大器;!&安培; lVideoSchemeStr.equalsIgnoreCase(SCHEME_YOUTUBE_PLAYLIST)){
        lYouTubeId =新PlaylistId(lVideoIdStr);
    }    否则,如果(lVideoSchemeStr = NULL&放大器;!&安培; lVideoSchemeStr.equalsIgnoreCase(SCHEME_YOUTUBE_VIDEO)){
        lYouTubeId =新VIDEOID(lVideoIdStr);
    }    如果(lYouTubeId == NULL){
        Log.i(this.getClass()getSimpleName(),无法从意图提取视频ID闭幕影片活动。);
        完();
    }    mQueryYouTubeTask =(QueryYouTubeTask)新QueryYouTubeTask()执行(lYouTubeId)。
}/ **
 *确定该消息时视频负载和初始化显示。
 * /
私人无效extractMessages(){
    意向lInvokingIntent = getIntent();
    字符串lMsgInit = lInvokingIntent.getStringExtra(MSG_INIT);
    如果(lMsgInit!= NULL){
        mMsgInit = lMsgInit;
    }
    字符串lMsgDetect = lInvokingIntent.getStringExtra(MSG_DETECT);
    如果(lMsgDetect!= NULL){
        mMsgDetect = lMsgDetect;
    }
    字符串lMsgPlaylist = lInvokingIntent.getStringExtra(MSG_PLAYLIST);
    如果(lMsgPlaylist!= NULL){
        mMsgPlaylist = lMsgPlaylist;
    }
    字符串lMsgToken = lInvokingIntent.getStringExtra(MSG_TOKEN);
    如果(lMsgToken!= NULL){
        mMsgToken = lMsgToken;
    }
    字符串lMsgLoBand = lInvokingIntent.getStringExtra(MSG_LO_BAND);
    如果(lMsgLoBand!= NULL){
        mMsgLowBand = lMsgLoBand;
    }
    字符串lMsgHiBand = lInvokingIntent.getStringExtra(MSG_HI_BAND);
    如果(lMsgHiBand!= NULL){
        mMsgHiBand = lMsgHiBand;
    }
    字符串lMsgErrTitle = lInvokingIntent.getStringExtra(MSG_ERROR_TITLE);
    如果(lMsgErrTitle!= NULL){
        mMsgErrorTitle = lMsgErrTitle;
    }
    字符串lMsgErrMsg = lInvokingIntent.getStringExtra(MSG_ERROR_MSG);
    如果(lMsgErrMsg!= NULL){
        mMsgError = lMsgErrMsg;
    }
}/ **
 *创建其中视频将被渲染视图。
 * /
私人无效setupView函数(){
    的LinearLayout lLinLayout =新的LinearLayout(本);
    lLinLayout.setId(1);
    lLinLayout.setOrientation(LinearLayout.VERTICAL);
    lLinLayout.setGravity(Gravity.CENTER);
    lLinLayout.setBackgroundColor(Color.BLACK);    的LayoutParams lLinLayoutParms =新的LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,ViewGroup.LayoutParams.FILL_PARENT);
    lLinLayout.setLayoutParams(lLinLayoutParms);    this.setContentView(lLinLayout);
    RelativeLayout的lRelLayout =新的RelativeLayout(本);
    lRelLayout.setId(2);
    lRelLayout.setGravity(Gravity.CENTER);
    lRelLayout.setBackgroundColor(Color.BLACK);
    android.widget.RelativeLayout.LayoutParams lRelLayoutParms =新android.widget.RelativeLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,ViewGroup.LayoutParams.FILL_PARENT);
    lRelLayout.setLayoutParams(lRelLayoutParms);
    lLinLayout.addView(lRelLayout);    mVideoView =新VideoView(本);
    mVideoView.setId(3);
    android.widget.RelativeLayout.LayoutParams lVidViewLayoutParams =新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 =新的ProgressBar(本);
    mProgressBar.setIndeterminate(真);
    mProgressBar.setVisibility(View.VISIBLE);
    mProgressBar.setEnabled(真);
    mProgressBar.setId(4);
    android.widget.RelativeLayout.LayoutParams lProgressBarLayoutParms =新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 =新的TextView(本);
    mProgressMessage.setId(5);
    android.widget.RelativeLayout.LayoutParams lProgressMsgLayoutParms =新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);
}@覆盖
保护无效的onDestroy(){
    super.onDestroy();    YouTubeUtility.markVideoAsViewed(这一点,mVideoId);    如果(mQueryYouTubeTask!= NULL){
        mQueryYouTubeTask.cancel(真);
    }    如果(mVideoView!= NULL){
        mVideoView.stopPlayback();
    }    //清除,保持屏幕上的标志
    。getWindow()clearFlags(android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);    this.mQueryYouTubeTask = NULL;
    this.mVideoView = NULL;}公共无效的UpdateProgress(字符串pProgressMsg){
    尝试{
        mProgressMessage.setText(pProgressMsg);
    }赶上(例外五){
        Log.e(this.getClass()getSimpleName(),错误更新视频状态!,E。);
    }
}私有类ProgressUpdateInfo {    公共字符串MMSG;    公共ProgressUpdateInfo(字符串PMSG){
        MMSG = PMSG;
    }
}/ **
 *任务通过调用出来到YouTube的GData API弄清楚细节。我们只用公共的方法
 *不要求身份验证。
 *
 * /
私有类QueryYouTubeTask扩展的AsyncTask< YouTubeId,ProgressUpdateInfo,乌里> {    私人布尔mShowedError = FALSE;    @覆盖
    保护乌里doInBackground(YouTubeId ... pParams){
        字符串lUriStr = NULL;
        //修改由Naushad卡苏 - 更改为18,而不是17
        字符串lYouTubeFmtQuality =18; // 3GPP中等质量,这应该是足够快速地查看了EDGE连接
        字符串lYouTubeVideoId = NULL;        如果(isCancelled())
            返回null;        尝试{
            //如果目的是为了显示一个播放列表,从播放列表获取最新视频ID,否则,视频
            // ID被显式声明。
            如果(pParams [0]的instanceof PlaylistId){
                publishProgress(新ProgressUpdateInfo(mMsgPlaylist));
                lYouTubeVideoId = YouTubeUtility.queryLatestPlaylistVideo((PlaylistId)pParams [0]);
            }            否则,如果(pParams [0]的instanceof VIDEOID){
                lYouTubeVideoId = pParams [0] .getId();
            }            mVideoId = lYouTubeVideoId;            publishProgress(新ProgressUpdateInfo(mMsgToken));            如果(isCancelled())
                返回null;            ////////////////////////////////////
            //计算视频的实际URL,带适当的YouTube令牌codeD
            lUriStr = YouTubeUtility.calculateYouTubeUrl(lYouTubeFmtQuality,真实,lYouTubeVideoId);            如果(isCancelled())
                返回null;            如果(lYouTubeFmtQuality.equals(17)){
                publishProgress(新ProgressUpdateInfo(mMsgLowBand));
            }其他{
                publishProgress(新ProgressUpdateInfo(mMsgHiBand));
            }        }赶上(例外五){
            Log.e(this.getClass()getSimpleName(),而从YouTube中检索信息时发生错误。E);
        }        如果(lUriStr!= NULL){
            返回Uri.parse(lUriStr);
        }其他{
            返回null;
        }
    }    @覆盖
    保护无效onPostExecute(URI presult){
        super.onPostExecute(presult);        尝试{
            如果(isCancelled())
                返回;            如果(presult == NULL){
                抛出新的RuntimeException(无效的空网址。);
            }            mVideoView.setVideoURI(presult);            如果(isCancelled())
                返回;            // TODO:添加监听器的视频结束
            mVideoView.setOnCompletionListener(新OnCompletionListener(){                @覆盖
                公共无效onCompletion(MediaPlayer的PMP){
                    如果(isCancelled())
                        返回;
                    OpenYouTubePlayerActivity.this.finish();
                }            });            如果(isCancelled())
                返回;            最终的MediaController lMediaController =新的MediaController(OpenYouTubePlayerActivity.this);
            mVideoView.setMediaController(lMediaController);
            lMediaController.show(0);
            //mVideoView.setKeepScreenOn(true);
            mVideoView.setOn preparedListener(新MediaPlayer.On preparedListener(){                @覆盖
                在prepared公共无效(MediaPlayer的PMP){
                    如果(isCancelled())
                        返回;
                    OpenYouTubePlayerActivity.this.mProgressBar.setVisibility(View.GONE);
                    OpenYouTubePlayerActivity.this.mProgressMessage.setVisibility(View.GONE);
                }            });            如果(isCancelled())
                返回;            mVideoView.requestFocus();
            mVideoView.start();
        }赶上(例外五){
            Log.e(this.getClass()getSimpleName(),错误播放视频!,E。);            如果(!mShowedError){
                showErrorAlert();
            }
        }
    }    私人无效showErrorAlert(){        尝试{
            建设者lBuilder =新AlertDialog.Builder(OpenYouTubePlayerActivity.this);
            lBuilder.setTitle(mMsgErrorTitle);
            lBuilder.setCancelable(假);
            lBuilder.setMessage(mMsgError);            lBuilder.setPositiveButton(OK,新DialogInterface.OnClickListener(){                @覆盖
                公共无效的onClick(DialogInterface pDialog,诠释pWhich){
                    OpenYouTubePlayerActivity.this.finish();
                }            });            AlertDialog lDialog = lBuilder.create();
            lDialog.show();
        }赶上(例外五){
            Log.e(this.getClass()getSimpleName(),问题显示错误对话框,电子);
        }
    }    @覆盖
    保护无效onProgressUpdate(ProgressUpdateInfo ... pValues​​){
        super.onProgressUpdate(pValues​​);        OpenYouTubePlayerActivity.this.updateProgress(pValues​​ [0] .mMsg);
    }
}@覆盖
保护无效调用onStart(){
    super.onStart();
}@覆盖
保护无效的onStop(){
    super.onStop();
}
}

与同code一些视频正在播放,有些则不是。
它给出了这样的错误。

  05-29 18:16:42.429:ERROR / QueryYouTubeTask(287):在从YouTube中检索信息时发生错误。
05-29 18:16:42.429:ERROR / QueryYouTubeTask(287):显示java.lang.NullPointerException
05-29 18:16:42.429:ERROR / QueryYouTubeTask(287):在java.net.URLDe coder.de code(URLDe coder.java:130)
05-29 18:16:42.429:ERROR / QueryYouTubeTask(287):在java.net.URLDe coder.de code(URLDe coder.java:68)
05-29 18:16:42.429:ERROR / QueryYouTubeTask(287):在com.controlledsenility.android.youtube.YouTubeUtility.calculateYouTubeUrl(YouTubeUtility.java:152)
05-29 18:16:42.429:ERROR / QueryYouTubeTask(287):在com.ydemo.OpenYouTubePlayerActivity$QueryYouTubeTask.doInBackground(OpenYouTubePlayerActivity.java:392)
05-29 18:16:42.429:ERROR / QueryYouTubeTask(287):在com.ydemo.OpenYouTubePlayerActivity$QueryYouTubeTask.doInBackground(OpenYouTubePlayerActivity.java:1)
05-29 18:16:42.429:ERROR / QueryYouTubeTask(287):在android.os.AsyncTask $ 2.call(AsyncTask.java:185)
05-29 18:16:42.429:ERROR / QueryYouTubeTask(287):在java.util.concurrent.FutureTask中$ Sync.innerRun(FutureTask.java:305)
05-29 18:16:42.429:ERROR / QueryYouTubeTask(287):在java.util.concurrent.FutureTask.run(FutureTask.java:137)
05-29 18:16:42.429:ERROR / QueryYouTubeTask(287):在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
05-29 18:16:42.429:ERROR / QueryYouTubeTask(287):在java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:561)
05-29 18:16:42.429:ERROR / QueryYouTubeTask(287):在java.lang.Thread.run(Thread.java:1096)
05-29 18:16:42.449:ERROR / QueryYouTubeTask(287):错误播放视频!
05-29 18:16:42.449:ERROR / QueryYouTubeTask(287):了java.lang.RuntimeException:无效的空的URL。
05-29 18:16:42.449:ERROR / QueryYouTubeTask(287):在com.ydemo.OpenYouTubePlayerActivity$QueryYouTubeTask.onPostExecute(OpenYouTubePlayerActivity.java:425)
05-29 18:16:42.449:ERROR / QueryYouTubeTask(287):在com.ydemo.OpenYouTubePlayerActivity $ QueryYouTubeTask.onPostExecute(OpenYouTubePlayerActivity.java:1)
05-29 18:16:42.449:ERROR / QueryYouTubeTask(287):在android.os.AsyncTask.finish(AsyncTask.java:417)
05-29 18:16:42.449:ERROR / QueryYouTubeTask(287):在android.os.AsyncTask.access $ 300(AsyncTask.java:127)
05-29 18:16:42.449:ERROR / QueryYouTubeTask(287):在android.os.AsyncTask $ InternalHandler.handleMessage(AsyncTask.java:429)
05-29 18:16:42.449:ERROR / QueryYouTubeTask(287):在android.os.Handler.dispatchMessage(Handler.java:99)
05-29 18:16:42.449:ERROR / QueryYouTubeTask(287):在android.os.Looper.loop(Looper.java:123)
05-29 18:16:42.449:ERROR / QueryYouTubeTask(287):在android.app.ActivityThread.main(ActivityThread.java:4627)
05-29 18:16:42.449:ERROR / QueryYouTubeTask(287):在java.lang.reflect.Method.invokeNative(本机方法)
05-29 18:16:42.449:ERROR / QueryYouTubeTask(287):在java.lang.reflect.Method.invoke(Method.java:521)
05-29 18:16:42.449:ERROR / QueryYouTubeTask(287):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:868)
05-29 18:16:42.449:ERROR / QueryYouTubeTask(287):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-29 18:16:42.449:ERROR / QueryYouTubeTask(287):在dalvik.system.NativeStart.main(本机方法)


解决方案

使用

 意图videoIntent =新意图(NULL,Uri.parse(YTV:NG3WygJmiVs),对此,OpenYouTubePlayerActivity.class);
startActivity(videoIntent);

I am using OpenYoutubeActivity.jar to play youtube videos like this

Intent videoIntent = new Intent(null, Uri.parse("ytv://NG3WygJmiVs"), this,OpenYouTubePlayerActivity.class);
    startActivity(videoIntent);

OpenYouTubePlayerActivity.java is here

public class OpenYouTubePlayerActivity 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;
        // modified by Naushad Kasu - changed to 18 instead of 17
        String lYouTubeFmtQuality = "18";   // 3gpp medium quality, which should be fast enough to view over EDGE connection
        String lYouTubeVideoId = null;

        if(isCancelled())
            return null;

        try {


            // 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(){

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

            });

            if(isCancelled())
                return;

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

                @Override
                public void onPrepared(MediaPlayer pMp) {
                    if(isCancelled())
                        return;
                    OpenYouTubePlayerActivity.this.mProgressBar.setVisibility(View.GONE);
                    OpenYouTubePlayerActivity.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(OpenYouTubePlayerActivity.this);
            lBuilder.setTitle(mMsgErrorTitle);
            lBuilder.setCancelable(false);
            lBuilder.setMessage(mMsgError);

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

                @Override
                public void onClick(DialogInterface pDialog, int pWhich) {
                    OpenYouTubePlayerActivity.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);

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

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

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

with the same code some videos are playing and some are not. its gives error like this

05-29 18:16:42.429: ERROR/QueryYouTubeTask(287): Error occurred while retrieving information from YouTube.
05-29 18:16:42.429: ERROR/QueryYouTubeTask(287): java.lang.NullPointerException
05-29 18:16:42.429: ERROR/QueryYouTubeTask(287):     at java.net.URLDecoder.decode(URLDecoder.java:130)
05-29 18:16:42.429: ERROR/QueryYouTubeTask(287):     at java.net.URLDecoder.decode(URLDecoder.java:68)
05-29 18:16:42.429: ERROR/QueryYouTubeTask(287):     at com.controlledsenility.android.youtube.YouTubeUtility.calculateYouTubeUrl(YouTubeUtility.java:152)
05-29 18:16:42.429: ERROR/QueryYouTubeTask(287):     at com.ydemo.OpenYouTubePlayerActivity$QueryYouTubeTask.doInBackground(OpenYouTubePlayerActivity.java:392)
05-29 18:16:42.429: ERROR/QueryYouTubeTask(287):     at com.ydemo.OpenYouTubePlayerActivity$QueryYouTubeTask.doInBackground(OpenYouTubePlayerActivity.java:1)
05-29 18:16:42.429: ERROR/QueryYouTubeTask(287):     at android.os.AsyncTask$2.call(AsyncTask.java:185)
05-29 18:16:42.429: ERROR/QueryYouTubeTask(287):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
05-29 18:16:42.429: ERROR/QueryYouTubeTask(287):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
05-29 18:16:42.429: ERROR/QueryYouTubeTask(287):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
05-29 18:16:42.429: ERROR/QueryYouTubeTask(287):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
05-29 18:16:42.429: ERROR/QueryYouTubeTask(287):     at java.lang.Thread.run(Thread.java:1096)
05-29 18:16:42.449: ERROR/QueryYouTubeTask(287): Error playing video!
05-29 18:16:42.449: ERROR/QueryYouTubeTask(287): java.lang.RuntimeException: Invalid NULL Url.
05-29 18:16:42.449: ERROR/QueryYouTubeTask(287):     at com.ydemo.OpenYouTubePlayerActivity$QueryYouTubeTask.onPostExecute(OpenYouTubePlayerActivity.java:425)
05-29 18:16:42.449: ERROR/QueryYouTubeTask(287):     at com.ydemo.OpenYouTubePlayerActivity$QueryYouTubeTask.onPostExecute(OpenYouTubePlayerActivity.java:1)
05-29 18:16:42.449: ERROR/QueryYouTubeTask(287):     at android.os.AsyncTask.finish(AsyncTask.java:417)
05-29 18:16:42.449: ERROR/QueryYouTubeTask(287):     at android.os.AsyncTask.access$300(AsyncTask.java:127)
05-29 18:16:42.449: ERROR/QueryYouTubeTask(287):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
05-29 18:16:42.449: ERROR/QueryYouTubeTask(287):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-29 18:16:42.449: ERROR/QueryYouTubeTask(287):     at android.os.Looper.loop(Looper.java:123)
05-29 18:16:42.449: ERROR/QueryYouTubeTask(287):     at android.app.ActivityThread.main(ActivityThread.java:4627)
05-29 18:16:42.449: ERROR/QueryYouTubeTask(287):     at java.lang.reflect.Method.invokeNative(Native Method)
05-29 18:16:42.449: ERROR/QueryYouTubeTask(287):     at java.lang.reflect.Method.invoke(Method.java:521)
05-29 18:16:42.449: ERROR/QueryYouTubeTask(287):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-29 18:16:42.449: ERROR/QueryYouTubeTask(287):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-29 18:16:42.449: ERROR/QueryYouTubeTask(287):     at dalvik.system.NativeStart.main(Native Method)

解决方案

use this

Intent videoIntent = new Intent(null, Uri.parse("ytv:NG3WygJmiVs"),   this,OpenYouTubePlayerActivity.class);
startActivity(videoIntent);

这篇关于你管的视频不是在玩的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆