ksvc / KSYMediaPlayer_Android

金山云Android播放SDK(KSYUN Live Streaming player SDK),支持RTMP HTTP-FLV HLS 协议(supporting RTMP HTTP-FLV HLS protocol),直播延时2-3秒(Living delay 2 or 3 seconds),supporting jitter control,software&hardware decode
http://v.ksyun.com/
Apache License 2.0
855 stars 247 forks source link

I need help OnClcik to play from Json http m3u file #107

Open adetheprince opened 7 years ago

adetheprince commented 7 years ago

Hi there I am trying your code and the demo is good so far, but possible you can help me on how to play the video ? I have modified NetMediaActivity.class to this and its loading fine from my json file saved on the server.

here is my new code which is showing all channels name but on click is missing.

Thanks.

package com.ksyun.player.demo.activity;

import android.graphics.Color; import android.os.Bundle; import android.support.v4.widget.DrawerLayout;

import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.Gravity;

import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.ArrayAdapter;

import android.widget.ListView; import android.widget.TextView;

import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; import com.ksyun.player.demo.R; import com.ksyun.player.demo.model.NetDbAdapter;

import java.io.IOException; import java.lang.reflect.Type; import java.util.ArrayList; import java.util.List;

import okhttp3.ResponseBody; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; import retrofit2.Retrofit; import rx.Observable; import rx.functions.Action1;

public class NetMediaActivty extends AppCompatActivity {

private List<String> groupArray;
private List<List<VideoLive>> childArray;

private DrawerLayout drawerLayout;
private ListView leftDrawer;
private ArrayAdapter<String> adapter;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_net);
    drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    leftDrawer = (ListView) findViewById(R.id.drawer_left);

    leftDrawer.setBackgroundColor(Color.WHITE);

    // 设置选择模式为单条选中
    leftDrawer.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

    // 通过代码:根据重力方向打开指定抽屉
    drawerLayout.openDrawer(Gravity.LEFT);
    // 设置抽屉空余处颜色
    drawerLayout.setScrimColor(Color.BLUE);

    //对这两个List进行初始化,并插入一些数据
    groupArray = new ArrayList<String>();
    childArray = new ArrayList<List<VideoLive>>();

    getVideoJson();
}

private void getVideoJson() {
    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl("http://satdoc.dyndns.info/")
            .client(AFHttpClient.getHttpClient())
            .build();

    VideoService loginService = retrofit.create(VideoService.class);
    Call<ResponseBody> call = loginService.getJson();

    call.enqueue(new Callback<ResponseBody>() {
        @Override
        public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
            String jsonString = null;
            try {
                jsonString = response.body().string();
            } catch (IOException e) {
                e.printStackTrace();
            }
            Type listType = new TypeToken<ArrayList<VideoInfo>>(){}.getType();
            ArrayList<VideoInfo> videoInfoList = new Gson().fromJson(jsonString, listType);

            Log.d("Channels List Ready: ", "" + videoInfoList.size());

            setData(videoInfoList);
        }

        @Override
        public void onFailure(Call<ResponseBody> call, Throwable t) {
            Log.e("wxl", "onFailure=" + t.getMessage());
        }
    });
}

private void setData(ArrayList<VideoInfo> videoInfoList) {
    Observable.from(videoInfoList).subscribe(new Action1<VideoInfo>() {
        @Override
        public void call(VideoInfo videoInfo) {
            groupArray.add(videoInfo.getName());

            childArray.add(videoInfo.getSamples());
        }
    });
    initView();
}

private void initView() {
    adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, groupArray) {
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // 设置背填充内容背景色
            TextView tView = (TextView) super.getView(position,
                    convertView, parent);
            tView.setTextColor(Color.BLACK);
            return super.getView(position, convertView, parent);
        }
    };
    leftDrawer.setAdapter(adapter);
    leftDrawer.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int pos,
                                long id) {
            leftDrawer.setItemChecked(pos, true);
            VideoFragment videoFragment = VideoFragment.getInstance(childArray.get(pos));
            getSupportFragmentManager().beginTransaction()
                    .replace(R.id.content_view, videoFragment).commit();
            // 关闭所有打开的抽屉
            drawerLayout.closeDrawers();
        }
    });

}

}

image

adetheprince commented 7 years ago

public class VideoFragment extends Fragment {

private ListView listView;
private VideoAdapter adapter;
private static List<VideoLive> mChildArray;

public static VideoFragment getInstance(List<VideoLive> childArray) {
    mChildArray = childArray;
    VideoFragment v = new VideoFragment();
    return v;
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_video, container, false);
    listView = (ListView) view.findViewById(R.id.listView);
    init();
    return view;
}

public void init() {
    adapter = new VideoAdapter(getContext(), mChildArray);
    listView.setAdapter(adapter);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
           IMediaPlayer.OnPreparedListener (getContext(), mChildArray.get(position).getChlink(), mChildArray.get(position).getChname());

(( is this correct ?? ?I dont Know what to write here please help)) } }); }

}

FirmianaRain commented 7 years ago

You want to play the video when one item was clicked? Maybe you could refer to the code of demo.

adetheprince commented 7 years ago

yes Sorry I checked the demo but i am totally new to android. Is there easy code i can enter there? Thanks

fpzeng commented 7 years ago

Could you just show me the jsonstring which is responsed from loginService?

adetheprince commented 7 years ago

Ok here are all the files that i added hope it will help. Many thanks.

VideoLive.java

package com.ksyun.player.demo.activity;

import java.io.Serializable;

public class VideoLive implements Serializable {

private Integer chid;
private String chname;
private String chlogo;
private String chlink;
private String chenable;
private String chnote;

public Integer getChid() {
    return chid;
}

public void setChid(Integer chid) {
    this.chid = chid;
}

public String getChname() {
    return chname;
}

public void setChname(String chname) {
    this.chname = chname;
}

public String getChlogo() {
    return chlogo;
}

public void setChlogo(String chlogo) {
    this.chlogo = chlogo;
}

public String getChlink() {
    return chlink;
}

public void setChlink(String chlink) {
    this.chlink = chlink;
}

public String getChenable() {
    return chenable;
}

public void setChenable(String chenable) {
    this.chenable = chenable;
}

public String getChnote() {
    return chnote;
}

public void setChnote(String chnote) {
    this.chnote = chnote;
}

}

adetheprince commented 7 years ago

VideoInfo.java

package com.ksyun.player.demo.activity;

import java.io.Serializable; import java.util.List;

public class VideoInfo implements Serializable { private String name; private List samples;

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public List<VideoLive> getSamples() {
    return samples;
}

public void setSamples(List<VideoLive> samples) {
    this.samples = samples;
}

}

adetheprince commented 7 years ago

VideoAdapter

package com.ksyun.player.demo.activity;

import android.content.Context; import android.graphics.Color; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView;

import java.util.List;

public class VideoAdapter extends BaseAdapter {

private Context context;
private List<VideoLive> childArray;

public VideoAdapter(Context context, List<VideoLive> childArray) {
    this.context = context;
    this.childArray = childArray;
}

@Override
public int getCount() {
    return childArray.size();
}

@Override
public Object getItem(int position) {
    return childArray.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    TextView view = new TextView(context);
    view.setText(childArray.get(position).getChname());
    view.setTextColor(Color.BLACK);
    return view;
}

}

adetheprince commented 7 years ago

and last videoservice

package com.ksyun.player.demo.activity;

import okhttp3.ResponseBody; import retrofit2.Call; import retrofit2.http.GET;

public interface VideoService { @GET("chanTest.js") Call getJson(); }

and chanTest.js looks like this

] },

{ "name": "FRANCE", "samples": [

    {
        "chid": 134,
        "chname": "FR:TCM 1",
        "chlogo": "",
        "chlink": "http://MyIPAddress:8000/live/Yasirmbl/Test/357.ts",
        "chenable": "",
        "chnote": ""
    },
    {

]