android - OnvideoClicklistener 问题转到 YouTube 主页

标签 android android-intent android-listview android-video-player android-youtube-api

我看过 SO,但我发现了一个类似的问题,但它对我没有帮助。

我已经构建了我的第一个 Android 应用程序我的应用程序应该播放来 self 的 YouTube channel 的视频和一个动态视频列表 我在我的 Android 模拟器中运行该应用程序,但是当我在我的应用程序中单击视频缩略图时,它会转到 YouTube 主页

我不知道它是否与模拟器目标有关。

我遵循了关于这个的在线教程。

这是视频的 Main.java:

/**
* The Activity can retrieve Videos for a specific username from YouTube</br>
* It then displays them into a list including the Thumbnail preview and the title</br>
* There is a reference to each video on YouTube as well but this isn't used in this tutorial</br>
* </br>
* <b>Note<b/> orientation change isn't covered in this tutorial, you will want to override
* onSaveInstanceState() and onRestoreInstanceState() when you come to this
* </br>
* @author paul.blundell
*/
public class maincontentview extends Activity implements VideoClickListener {
// A reference to our list that will hold the video details
private VideosListView listView;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.maincontentview);

listView = (VideosListView) findViewById(R.id.videosListView);
listView.setOnVideoClickListener(this);
}

// This is the XML onClick listener to retreive a users video feed
public void getUserYouTubeFeed(View v){
// We start a new task that does its work on its own thread
// We pass in a handler that will be called when the task has finished
// We also pass in the name of the user we are searching YouTube for
new GetYouTubeUserVideosTask(responseHandler, "projectofdawah").run();
}

// This is the handler that receives the response when the YouTube task has finished
Handler responseHandler = new Handler() {
public void handleMessage(Message msg) {
populateListWithVideos(msg);
};
};

/**
* This method retrieves the Library of videos from the task and passes them to our ListView
* @param msg
*/
private void populateListWithVideos(Message msg) {
// Retreive the videos are task found from the data bundle sent back
Library lib = (Library) msg.getData().get(GetYouTubeUserVideosTask.LIBRARY);
// Because we have created a custom ListView we don't have to worry about setting the adapter in the activity
// we can just call our custom method with the list of items we want to display
listView.setVideos(lib.getVideos());
}

@Override
protected void onStop() {
// Make sure we null our handler when the activity has stopped
// because who cares if we get a callback once the activity has stopped? not me!
responseHandler = null;
super.onStop();
}

// This is the interface method that is called when a video in the listview is clicked!
// The interface is a contract between this activity and the listview
@Override
public void onVideoClicked(com.mohammed.watzIslam.Video video) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(video.getUrl()));
startActivity(intent);
}
}

这是视频java文件

    package com.mohammed.watzIslam;

    import android.app.Activity;
    import android.content.Intent;
    import android.net.Uri;
    import android.os.Bundle;
    import android.os.Handler;
    import android.os.Message;
    import android.provider.MediaStore.Video;
    import android.view.View;







    /**
    * The Activity can retrieve Videos for a specific username from YouTube</br>
    * It then displays them into a list including the Thumbnail preview and the title</br>
    * There is a reference to each video on YouTube as well but this isn't used in this tutorial</br>
    * </br>
    * <b>Note<b/> orientation change isn't covered in this tutorial, you will want to override
    * onSaveInstanceState() and onRestoreInstanceState() when you come to this
    * </br>
    */
    public class maincontentview extends Activity implements VideoClickListener {
    // A reference to our list that will hold the video details
    private VideosListView listView;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.maincontentview);

    listView = (VideosListView) findViewById(R.id.videosListView);
    listView.setOnVideoClickListener(this);
    }

    // This is the XML onClick listener to retreive a users video feed
    public void getUserYouTubeFeed(View v){
    // We start a new task that does its work on its own thread
    // We pass in a handler that will be called when the task has finished
    // We also pass in the name of the user we are searching YouTube for
    new GetYouTubeUserVideosTask(responseHandler, "projectofdawah").run();
    }

    // This is the handler that receives the response when the YouTube task has finished
    Handler responseHandler = new Handler() {
    public void handleMessage(Message msg) {
    populateListWithVideos(msg);
    };
    };

    /**
    * This method retrieves the Library of videos from the task and passes them to our ListView
    * @param msg
    */
    private void populateListWithVideos(Message msg) {
    // Retreive the videos are task found from the data bundle sent back
    Library lib = (Library) msg.getData().get(GetYouTubeUserVideosTask.LIBRARY);
    // Because we have created a custom ListView we don't have to worry about setting the adapter in the activity
    // we can just call our custom method with the list of items we want to display
    listView.setVideos(lib.getVideos());
    }

    @Override
    protected void onStop() {
    // Make sure we null our handler when the activity has stopped
    // because who cares if we get a callback once the activity has stopped? not me!
    responseHandler = null;
    super.onStop();
    }

    // This is the interface method that is called when a video in the listview is clicked!
    // The interface is a contract between this activity and the listview
    @Override
    public void onVideoClicked(com.mohammed.watzIslam.Video video) {
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setData(Uri.parse(video.getUrl()));
    startActivity(intent);
    }
    }

这是视频 listview.java

package com.mohammed.watzIslam;

import java.util.List;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;




    public class VideosListView extends ListView implements android.widget.AdapterView.OnItemClickListener  {

private List<Video> videos;
private VideoClickListener videoClickListener;

public VideosListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}

public VideosListView(Context context, AttributeSet attrs) {
super(context, attrs);
}

public VideosListView(Context context) {
super(context);
}

public void setVideos(List<Video> videos){
this.videos = videos;
VideosAdapter adapter = new VideosAdapter(getContext(), videos);
setAdapter(adapter);
// When the videos are set we also set an item click listener to the list
// this will callback to our custom list whenever an item it pressed
// it will tell us what position in the list is pressed
setOnItemClickListener(this);
}

// Calling this method sets a listener to the list
// Whatever class is passed in will be notified when the list is pressed
// (The class that is passed in just has to 'implement VideoClickListener'
// meaning is has the methods available we want to call)
public void setOnVideoClickListener(VideoClickListener l) {
videoClickListener = l;}

@Override
public void setAdapter(ListAdapter adapter) {
super.setAdapter(adapter);
}

// When we receive a notification that a list item was pressed
// we check to see if a video listener has been set
// if it has we can then tell the listener 'hey a video has just been clicked' also passing the video
@Override
public void onItemClick(AdapterView<?> adapter, View v, int position, long id) {
if(videoClickListener != null){
    videoClickListener.onVideoClicked(videos.get(position));
}
}

}

日志

    05-09 09:10:58.080: I/Choreographer(914): Skipped 64 frames!  The application may be doing too much work on its main thread.
05-09 09:11:33.139: E/Trace(968): error opening trace file: No such file or directory (2)
05-09 09:11:34.219: D/dalvikvm(968): GC_FOR_ALLOC freed 48K, 3% free 8014K/8259K, paused 81ms, total 83ms
05-09 09:11:34.269: I/dalvikvm-heap(968): Grow heap (frag case) to 8.977MB for 1156016-byte allocation
05-09 09:11:34.459: D/dalvikvm(968): GC_CONCURRENT freed 1K, 3% free 9142K/9415K, paused 82ms+30ms, total 186ms
05-09 09:11:35.400: D/gralloc_goldfish(968): Emulator without GPU emulation detected.
05-09 09:11:38.101: D/dalvikvm(968): GC_CONCURRENT freed 19K, 2% free 9572K/9735K, paused 94ms+109ms, total 311ms
05-09 09:11:38.740: I/Choreographer(968): Skipped 141 frames!  The application may be doing too much work on its main thread.
05-09 09:11:39.490: I/Choreographer(968): Skipped 98 frames!  The application may be doing too much work on its main thread.
05-09 09:11:41.310: I/Choreographer(968): Skipped 32 frames!  The application may be doing too much work on its main thread.
05-09 09:11:42.190: I/Choreographer(968): Skipped 30 frames!  The application may be doing too much work on its main thread.
05-09 09:11:51.540: I/Choreographer(968): Skipped 38 frames!  The application may be doing too much work on its main thread.
05-09 09:11:51.850: I/Choreographer(968): Skipped 55 frames!  The application may be doing too much work on its main thread.
05-09 09:11:58.519: D/Button(968): Login
05-09 09:11:59.860: E/JSON(968): {"tag":"login","success":1,"error":0,"id":"518a7c7904eda1.69983308","user":{"name":"a","email":"a","created_at":"2013-05-09 00:25:29","updated_at":null}}
05-09 09:12:02.620: I/Choreographer(968): Skipped 35 frames!  The application may be doing too much work on its main thread.
05-09 09:12:10.931: D/dalvikvm(968): GC_CONCURRENT freed 197K, 3% free 9837K/10119K, paused 74ms+91ms, total 273ms
05-09 09:12:15.679: I/Choreographer(968): Skipped 38 frames!  The application may be doing too much work on its main thread.
05-09 09:12:17.450: I/Choreographer(968): Skipped 76 frames!  The application may be doing too much work on its main thread.
05-09 09:12:17.651: I/Choreographer(968): Skipped 39 frames!  The application may be doing too much work on its main thread.
05-09 09:12:22.071: I/System.out(968): https://www.youtube.com/watch?v=hBiyaXMVM90&feature=youtube_gdata_player
05-09 09:12:22.071: I/System.out(968): https://www.youtube.com/watch?v=hBiyaXMVM90&feature=youtube_gdata_player
05-09 09:12:22.630: D/dalvikvm(968): GC_CONCURRENT freed 1603K, 17% free 8807K/10503K, paused 87ms+83ms, total 259ms
05-09 09:12:22.630: D/dalvikvm(968): WAIT_FOR_CONCURRENT_GC blocked 31ms
05-09 09:12:23.470: I/System.out(968): URL :- hBiyaXMVM90
05-09 09:12:23.590: I/Choreographer(968): Skipped 339 frames!  The application may be doing too much work on its main thread.
05-09 09:12:24.561: D/MediaPlayer(968): Couldn't open file on client side, trying server side
05-09 09:12:24.931: I/Choreographer(968): Skipped 304 frames!  The application may be doing too much work on its main thread.
05-09 09:12:25.450: D/MediaPlayer(968): getMetadata
05-09 09:12:25.591: I/Choreographer(968): Skipped 37 frames!  The application may be doing too much work on its main thread.
05-09 09:12:31.744: E/MediaPlayer(968): error (1, -2147483648)
05-09 09:12:31.744: E/MediaPlayer(968): Error (1,-2147483648)
05-09 09:12:31.750: D/VideoView(968): Error: 1,-2147483648
05-09 09:12:32.140: I/Choreographer(968): Skipped 59 frames!  The application may be doing too much work on its main thread.
05-09 09:12:33.560: I/Choreographer(968): Skipped 59 frames!  The application may be doing too much work on its main thread.

希望得到大家的帮助

最佳答案

您需要从 url 中拆分视频 ID 并将其传递给下一个 Activity ,如下所示。

@Override
    public void onVideoClicked(Video video) {
        // TODO Auto-generated method stub
        System.out.println(video.getUrl());
        Intent videointent = new Intent(DisplayVideo.this,
                YouTubeVideo.class);
        Bundle bundle = new Bundle();
        System.out.println(video.getUrl());

        bundle.putString("videoid", video.getUrl().split("v=")[1].split("&")[0]);
        videointent.putExtras(bundle);
        startActivity(videointent);
    }

现在 YouTubeVideo.java 如下所示。

public class YouTubeVideo extends Activity {
    private static ProgressDialog progressDialog;

    public void onCreate(Bundle instance) {
        super.onCreate(instance);
        setContentView(R.layout.playvideo);
        progressDialog = ProgressDialog.show(this, "", "Loading...", true);
        progressDialog.setCancelable(true);
        Bundle bundle = getIntent().getExtras();
        final String data = bundle.getString("videoid");
        final VideoView vv = (VideoView) findViewById(R.id.VideoView);
        MediaController mc = new MediaController(this);
        mc.setEnabled(true);
        mc.show(0);
        vv.setMediaController(mc);
        vv.setVideoURI(Uri.parse(getUrlVideoRTSP(data)));
        vv.requestFocus();
        vv.showContextMenu();
        vv.setOnPreparedListener(new OnPreparedListener() {

            @Override
            public void onPrepared(MediaPlayer arg0) {
                // TODO Auto-generated method stub
                progressDialog.dismiss();
                vv.start();

            }
        });

        vv.setOnErrorListener(new OnErrorListener() {

            @Override
            public boolean onError(MediaPlayer mp, int what, int extra) {
                // TODO Auto-generated method stub
                progressDialog.dismiss();
                return false;
            }
        });
        System.out.println("URL :- " + data);
    }
    public static String getUrlVideoRTSP(String urlYoutube) {
        try {
            String gdy = "http://gdata.youtube.com/feeds/api/users/YOURUSERNAME/uploads/";
            DocumentBuilder documentBuilder = DocumentBuilderFactory
                    .newInstance().newDocumentBuilder();
            String id = extractYoutubeId(urlYoutube);
            URL url = new URL(gdy + id);
            HttpURLConnection connection = (HttpURLConnection) url
                    .openConnection();
            Document doc = documentBuilder.parse(connection.getInputStream());
            Element el = doc.getDocumentElement();
            NodeList list = el.getElementsByTagName("media:content");// /media:content
            String cursor = urlYoutube;
            for (int i = 0; i < list.getLength(); i++) {
                Node node = list.item(i);
                if (node != null) {
                    NamedNodeMap nodeMap = node.getAttributes();
                    HashMap<String, String> maps = new HashMap<String, String>();
                    for (int j = 0; j < nodeMap.getLength(); j++) {
                        Attr att = (Attr) nodeMap.item(j);
                        maps.put(att.getName(), att.getValue());
                    }
                    if (maps.containsKey("yt:format")) {
                        String f = maps.get("yt:format");
                        if (maps.containsKey("url")) {
                            cursor = maps.get("url");
                        }
                        if (f.equals("1"))
                            return cursor;
                    }
                }
            }
            return cursor;
        } catch (Exception ex) {
            Log.e("Get Url Video RTSP Exception======>>", ex.toString());
        }
        return urlYoutube;
    }
    private static String extractYoutubeId(String url) {
        return url;
    }

}

getUrlVideoRTSP() 将您的 URL 转换为 rtsp 格式,在 videoview 中播放。

现在我还为 playvideo 添加了 xml 文件

播放视频.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mainlinear"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    android:orientation="vertical" >

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >


        <VideoView
            android:id="@+id/VideoView"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />
    </LinearLayout>

</LinearLayout>

就是这样。现在您的视频在您的应用程序内的 videoview 中播放。

I don't know is it something with the emulator target.

没有。它也可以在 Emulator 中播放,但需要更多时间来播放。所以在真实设备上进行测试很重要。

谢谢。 快乐编码。

关于android - OnvideoClicklistener 问题转到 YouTube 主页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16438267/

相关文章:

java - 通过 Fragment 显示 Activity 时出错

Android:从另一个 Activity 访问一个 Activity 中的方法

android - 为什么 Bundle 数据在检索时出错?

android - 如何在使用 Intent 发回数据时跳过一项 Activity ?

android - 满足条件时从屏幕底部显示 ListView(所有项目可见)

Android Studio 2.3 gradle 错误

android - RECORD_SOUND_ACTION Intent ,直接进入录音列表

android - 从我的自定义主屏幕启动时相机和 map 崩溃

java - 将 editText 值返回到 listView

android - 自动完成文本框在建议列表中突出显示键入的字符