Hiện kết quả từ 1 tới 4 của 4

Chủ đề: [Soure] Ứng dụng xem video youtube


  1. #1
    Thành Viên Mới Avatar của ngovanninh
    Tham gia
    May 2013
    Bài gửi
    7

    [Soure] Ứng dụng xem video youtube

    Ứng dụng đơn giản mong mọi người đóng góp ý kiến.

    Link sản phẩm của mình trên google play: https://play.google.com/store/apps/d...5pbmguanYiXQ..

    Đây là giao diện

    AndroidManifest.xml
    Mã:
    <?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.ninh.jv"
        android:versionCode="1"
        android:versionName="1.0" >
    
    
        <uses-sdk android:minSdkVersion="8" />
    
    
        <uses-permission android:name="android.permission.INTERNET" />
    
    
        <application
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name" >
            <activity
                android:name=".MainActivity"
                android:label="@string/app_name" android:screenOrientation="portrait">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
            <activity android:name=".VideoActivity" android:screenOrientation="portrait"></activity>
        </application>
    
    
    </manifest>
    Layout

    activity_main.xml
    Mã:
    <?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
    
        <com.ninh.ui.widget.VideosListView
            android:id="@+id/videosListView"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />
    
    </LinearLayout>
    list_item_video.xml

    Mã:
    <?xml version="1.0" encoding="utf-8"?><FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:paddingBottom="5dip"
        android:layout_height="120dip" >
    
        <com.ninh.ui.widget.UrlImageView
            android:id="@+id/userVideoThumbImageView"
            android:layout_width="match_parent"
            android:gravity="center"
            android:layout_gravity="center"/>
    
        <TextView
            android:id="@+id/userVideoTitleTextView"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:padding="5dip"
            android:textColor="@android:color/white"
            android:text="Video Title Not Found" />
    
    </FrameLayout>
    MainActivity.java
    Mã:
    package com.ninh.jv;
    
    import java.util.HashMap;
    import android.app.Activity;
    import android.app.ProgressDialog;
    import android.content.Intent;
    import android.net.Uri;
    import android.os.AsyncTask;
    import android.os.Bundle;
    import android.os.Handler;
    import android.os.Message;
    import android.util.Log;
    import android.view.View;
    import android.view.Window;
    import android.widget.AdapterView;
    import android.widget.AdapterView.OnItemClickListener;
    import android.widget.Toast;
    
    
    import com.ninh.jv.R;
    import com.ninh.domain.Library;
    import com.ninh.domain.Video;
    import com.ninh.service.task.GetYouTube;
    import com.ninh.ui.widget.VideosListView;
    
    public class MainActivity extends Activity {
        private VideosListView listView;
        ProgressDialog progressDialog;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            setContentView(R.layout.activity_main);
    
            listView = (VideosListView) findViewById(R.id.videosListView);
            getUserYouTubeFeed(listView);
            listView.setOnItemClickListener(new OnItemClickListener() {
    
                public void onItemClick(AdapterView<?> arg0, View arg1,
                        int position, long arg3) {
                    final Video vi = (Video) arg0.getItemAtPosition(position);
                    
                    Uri uri = Uri.parse(vi.getUrl());
                    String v = uri.getQueryParameter("v");
                    Intent intent = new Intent(Intent.ACTION_VIEW, Uri
                            .parse("vnd.youtube:" + v));
                    intent.putExtra("VIDEO_ID", v);
                    startActivity(intent);
                    
                }
            });
        }
    
    
        public void getUserYouTubeFeed(View v) {
            progressDialog = ProgressDialog.show(this, "", "loading...");
            new Thread(new GetYouTube(responseHandler, ""))
                    .start();
        }
    
        Handler responseHandler = new Handler() {
            public void handleMessage(Message msg) {
                populateListWithVideos(msg);
            };
        };
        private void populateListWithVideos(Message msg) {
            final Library lib = (Library) msg.getData().get(
                     GetYouTube.LIBRARY);
            runOnUiThread(new Runnable() {
                public void run() {
                    if (progressDialog != null)
                        progressDialog.dismiss();
                    listView.setVideos(lib.getVideos());
                }
            });
        }
    
        @Override
        protected void onStop() {
            responseHandler = null;
            super.onStop();
        }
    
    
    }


    GetYouTube.java
    Mã:
    package com.ninh.service.task;
    
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;
    
    import org.apache.http.HttpResponse;
    import org.apache.http.client.ClientProtocolException;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.client.methods.HttpUriRequest;
    import org.apache.http.impl.client.DefaultHttpClient;
    import org.json.JSONArray;
    import org.json.JSONException;
    import org.json.JSONObject;
    
    
    import android.os.Bundle;
    import android.os.Handler;
    import android.os.Message;
    
    import com.ninh.domain.Library;
    import com.ninh.domain.Video;
    import com.ninh.util.Log;
    import com.ninh.util.StreamUtils;
    
    public class GetYouTube implements Runnable {
        public static final String LIBRARY = "Library";
        private final Handler replyTo;
        private final String username;
    
        public GetYouTube(Handler replyTo, String username) {
            this.replyTo = replyTo;
            this.username = username;
        }
        
        public void run() {
            try {
                HttpClient client = new DefaultHttpClient();
                HttpUriRequest request = new HttpGet("http://gdata.youtube.com/feeds/api/users/JVevermind/uploads?v=2&alt=jsonc");
                HttpResponse response = client.execute(request);
                String jsonString = StreamUtils.convertToString(response.getEntity().getContent());
                JSONObject json = new JSONObject(jsonString);
                
                JSONArray jsonArray = json.getJSONObject("data").getJSONArray("items");
                
                List<Video> videos = new ArrayList<Video>();
                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                    String title = jsonObject.getString("title");
                    String url;
                    try {
                        url = jsonObject.getJSONObject("player").getString("mobile");
                    } catch (JSONException ignore) {
                        url = jsonObject.getJSONObject("player").getString("default");
                    }
                    String thumbUrl = jsonObject.getJSONObject("thumbnail").getString("sqDefault");
                    
                    videos.add(new Video(title, url, thumbUrl));
                }
                Library lib = new Library(username, videos);
                Bundle data = new Bundle();
                data.putSerializable(LIBRARY, lib);
                
                Message msg = Message.obtain();
                msg.setData(data);
                replyTo.sendMessage(msg);
                
            } catch (ClientProtocolException e) {
                Log.e("Feck", e);
            } catch (IOException e) {
                Log.e("Feck", e);
            } catch (JSONException e) {
                Log.e("Feck", e);
            }
        }
    
    }


    Video.java
    Mã:
    package com.ninh.domain;
    
    import java.io.Serializable;
    
    
    public class Video implements Serializable {
        private String title;
        private String url;
        private String thumbUrl;
        
        public Video(String title, String url, String thumbUrl) {
            super();
            this.title = title;
            this.url = url;
            this.thumbUrl = thumbUrl;
        }
    
    
        public String getTitle(){
            return title;
        }
    
    
        public String getUrl() {
            return url;
        }
    
    
        public String getThumbUrl() {
            return thumbUrl;
        }
    
    }


    Library.java
    Mã:
    package com.ninh.domain;
    
    import java.io.Serializable;
    import java.util.List;
    
    
    public class Library implements Serializable{
        private String user;
        private List<Video> videos;
        
        public Library(String user, List<Video> videos) {
            this.user = user;
            this.videos = videos;
        }
    
    
        public String getUser() {
            return user;
        }
    
    
        public List<Video> getVideos() {
            return videos;
        }
    
    }


    VideoListView.java
    Mã:
    package com.ninh.ui.widget;
    
    import java.util.List;
    import android.content.Context;
    import android.util.AttributeSet;
    import android.widget.ListAdapter;
    import android.widget.ListView;
    
    
    import com.ninh.domain.Video;
    import com.ninh.ui.adapter.VideosAdapter;
    
    public class VideosListView extends ListView {
    
        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){
            VideosAdapter adapter = new VideosAdapter(getContext(), videos);
            setAdapter(adapter);
        }
        
        @Override
        public void setAdapter(ListAdapter adapter) {
            super.setAdapter(adapter);
        }
    
    }


    UrlImageView.java
    Mã:
    package com.ninh.ui.widget;
    
    import static android.app.Activity.RESULT_CANCELED;
    import static android.app.Activity.RESULT_OK;
    import java.io.IOException;
    import java.net.MalformedURLException;
    import android.content.Context;
    import android.graphics.drawable.Drawable;
    import android.os.Handler;
    import android.os.Handler.Callback;
    import android.os.Message;
    import android.util.AttributeSet;
    import android.view.View;
    import android.widget.ImageView;
    import android.widget.ImageView.ScaleType;
    import android.widget.LinearLayout;
    import android.widget.ProgressBar;
    public class UrlImageView extends LinearLayout {
    
        private Context mContext;
        private Drawable mDrawable;
        private ProgressBar mSpinner;
        private ImageView mImage;
    
        public UrlImageView(Context context, AttributeSet attrs) {
            super(context, attrs);
            init(context);
        }
    
        public UrlImageView(Context context) {
            super(context);
            init(context);
        }
    
        private void init(final Context context) {
            mContext = context;
            mImage = new ImageView(mContext);
            mImage.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
                    LayoutParams.MATCH_PARENT));
            mImage.setVisibility(View.GONE);
            mImage.setScaleType(ScaleType.CENTER_CROP);
            mSpinner = new ProgressBar(mContext);
            mSpinner.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT));
            mSpinner.setIndeterminate(true);
    
            addView(mSpinner);
            addView(mImage);
        }
    
        public void setImageDrawable(final String imageUrl) {
            mDrawable = null;
            mSpinner.setVisibility(View.VISIBLE);
            mImage.setVisibility(View.GONE);
            new Thread() {
                public void run() {
                    try {
                        mDrawable = getDrawableFromUrl(imageUrl);
                        imageLoadedHandler.sendEmptyMessage(RESULT_OK);
                    } catch (MalformedURLException e) {
                        imageLoadedHandler.sendEmptyMessage(RESULT_CANCELED);
                    } catch (IOException e) {
                        imageLoadedHandler.sendEmptyMessage(RESULT_CANCELED);
                    }
                };
            }.start();
        }
    
    
        private final Handler imageLoadedHandler = new Handler(new Callback() {
            public boolean handleMessage(Message msg) {
                switch (msg.what) {
                case RESULT_OK:
                    mImage.setImageDrawable(mDrawable);
                    mImage.setVisibility(View.VISIBLE);
                    mSpinner.setVisibility(View.GONE);
                    break;
                case RESULT_CANCELED:
                default:
                    break;
                }
                return true;
            }
        });
    
        private static Drawable getDrawableFromUrl(final String url)
                throws IOException, MalformedURLException {
            return Drawable.createFromStream(
                    ((java.io.InputStream) new java.net.URL(url).getContent()),
                    "name");
        }
    }


    VideoAdaper.java
    Mã:
    package com.ninh.ui.adapter;
    import java.util.List;
    
    import android.content.Context;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.TextView;
    
    import com.ninh.jv.R;
    import com.ninh.domain.Video;
    import com.ninh.ui.widget.UrlImageView;
    
    public class VideosAdapter extends BaseAdapter {
        List<Video> videos;
        private LayoutInflater mInflater;
    
        public VideosAdapter(Context context, List<Video> videos) {
            this.videos = videos;
            this.mInflater = LayoutInflater.from(context);
        }
    
        public int getCount() {
            return videos.size();
        }
    
        public Object getItem(int position) {
            return videos.get(position);
        }
    
        public long getItemId(int position) {
            return position;
        }
    
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = mInflater
                        .inflate(R.layout.list_item_video, null);
            }
            UrlImageView thumb = (UrlImageView) convertView
                    .findViewById(R.id.userVideoThumbImageView);
    
    
            TextView title = (TextView) convertView
                    .findViewById(R.id.userVideoTitleTextView);
            Video video = videos.get(position);
            thumb.setImageDrawable(video.getThumbUrl());
            title.setText(video.getTitle());
    
            return convertView;
        }
    
    }


    Log.java
    Mã:
    package com.ninh.util;
    
    import android.content.res.Resources;
    import android.content.res.Resources.NotFoundException;
    import android.os.Message;
    
    
    public class Log {
    
    
        private static final boolean live = false;  
        private static final String TAG = "ngovanninh";
        public static void d(String msg){
            d(msg, null);
        }
        
        public static void d(String msg, Throwable e){
            if(!live)
                android.util.Log.d(TAG, Thread.currentThread().getName() +"| "+ msg, e);
        }
        
        public static void i(String msg){
            i(msg, null);
        }
        
        public static void i(String msg, Throwable e){
            if(!live)
                android.util.Log.i(TAG, Thread.currentThread().getName() +"| "+ msg, e);
        }
        
        public static void e(String msg){
            e(msg, null);
        }
        
        public static void e(String msg, Throwable e){
            if(!live)
                android.util.Log.e(TAG, Thread.currentThread().getName() +"| "+ msg, e);
        }
    
        public static String identifyMessage(Resources res, Message msg) {
            try{ 
                return res.getResourceEntryName(msg.what); 
            } 
            catch(NotFoundException ignore){ 
                return "not found";
            }
        }    
    
    }


    StreamUtils.java
    Mã:
    package com.ninh.util;
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.Reader;
    import java.io.StringWriter;
    import java.io.Writer;
    
    public class StreamUtils {
        public static String convertToString(InputStream inputStream) throws IOException {
            if (inputStream != null) {
                Writer writer = new StringWriter();
                char[] buffer = new char[1024];
                try {
                    Reader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 1024);
                    int n;
                    while ((n = reader.read(buffer)) != -1) {
                        writer.write(buffer, 0, n);
                    }
                } finally {
                    inputStream.close();
                }
                return writer.toString();
            } else {
                return "";
            }
        }
    
    }


    Đó là toàn bộ code và hướng dẫn của mình. Rất mong nhận được sự đóng góp của mọi người.
    Google play :
    https://play.google.com/store/apps/d...5pbmguanYiXQ..

    Source: http://www.mediafire.com/download/pf...ov/ninh.jv.rar

    Chủ đề tương tự:

    Lần sửa cuối bởi ngovanninh; 13-06-13 lúc 10:23 PM

  2. #2
    Thành Viên Chính Thức Avatar của ittus
    Tham gia
    Jan 2013
    Bài gửi
    66
    Thiếu class GetYouTubeUserVideosTask à bạn



  3. #3
    Thành Viên Mới Avatar của Nguyendola
    Tham gia
    Nov 2013
    Bài gửi
    2
    Bạn ơi có thể gửi lại được không link die rồi!



  4. #4
    Thành Viên Sao Cấp 1 Avatar của vietdroid
    Tham gia
    Sep 2010
    Bài gửi
    1.913
    Quote Nguyên văn bởi Nguyendola Xem bài viết
    Bạn ơi có thể gửi lại được không link die rồi!
    Code đã show hết bên trong rồi đó bạn.



Đánh dấu

Quyền viết bài

  • Bạn không thể gửi chủ đề mới
  • Bạn không thể gửi trả lời
  • Bạn không thể gửi file đính kèm
  • Bạn không thể sửa bài viết của mình
  •