我正在构建一个涉及视频播放器的应用,我有一个列表视图,显示视频列表,点击任何应该播放该视频的视频。我使用的链接是rstp youtube链接&视频播放正常,但是当我播放视频后点击后退按钮时再来到列表我得到抱歉视频无法播放错误。
这是我的视频播放器类&列表类:
视频播放器类:
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.single);
vid=(VideoView) findViewById(R.id.svid);
iv=(ImageView) findViewById(R.id.simg);
ip=(ImageView) findViewById(R.id.playimg);
t=(TextView) findViewById(R.id.textView1);
final ProgressDialog pd=new ProgressDialog(SingleItem.this);
Intent g=getIntent();
thumb=g.getStringExtra("thumb");
link=g.getStringExtra("link");
msg=g.getStringExtra("msg");
//link="rtsp://v7.cache5.c.youtube.com/CjYLENy73wIaLQmgwjdV-8ZI5BMYJCAkFEIJbXYtZ29vZ2xlSARSBWluZGV4YKSf0bH1u4jEUAw=/0/0/0/video.3gp";
String path1=link;
MediaController mc = new MediaController(this);
mc.setAnchorView(vid);
mc.setMediaPlayer(vid);
uri=Uri.parse(path1);
vid.setMediaController(mc);
vid.setVideoURI(uri);
// vid.requestFocus();
//iv.setClickable(true);
loadImage(thumb);
t.setText(msg);
ip.setClickable(true);
ip.setImageResource(R.drawable.play);
// ip.setVisibility(ImageView.INVISIBLE);
vid.setOnPreparedListener(new OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
// TODO Auto-generated method stub
//ip.setVisibility(ImageView.VISIBLE);
pd.dismiss();
}
});
ip.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
vid.start();
pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);
pd.setMessage("Loading Video...");
pd.setIndeterminate(false);
pd.setCancelable(true);
pd.show();
if(vid.isPlaying()){
iv.setVisibility(ImageView.INVISIBLE);
ip.setVisibility(ImageView.INVISIBLE);
}else{
iv.setVisibility(ImageView.VISIBLE);
ip.setVisibility(ImageView.VISIBLE);
vid.stopPlayback();
}
}
});
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
vid.stopPlayback();
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
vid.stopPlayback();
}
void loadImage(String image_location){
URL imageURL = null;
try {
imageURL = new URL(image_location);
}
catch (MalformedURLException e) {
e.printStackTrace();
}
try {
HttpURLConnection connection= (HttpURLConnection)imageURL.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream inputStream = connection.getInputStream();
bitmap = BitmapFactory.decodeStream(inputStream);//Convert to bitmap
iv.setImageBitmap(bitmap);
}
catch (IOException e) {
e.printStackTrace();
}
}
}
我的列表类:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pf=new PrefMethods(this);
e=(EditText) findViewById(R.id.editText1);
go=(Button) findViewById(R.id.bGo);
e.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
if(e.getText().length()==0){
adapter=new LazyAdapter(VideoList.this, myList);
//Toast.makeText(getApplicationContext(), "Here finally", 500).show();
list.setAdapter(adapter);
}
}
});
ArrayList<String> items = new ArrayList<String>();
myList = new ArrayList<HashMap<String, String>>();
arr_link = new ArrayList<String>();
arr_thumb = new ArrayList<String>();
arr_msg = new ArrayList<String>();
allItems=new ArrayList<HashMap<String, String>>();
try {
URL urlnew= new URL("link");
HttpURLConnection urlConnection =
(HttpURLConnection) urlnew.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.connect();
// gets the server json data
BufferedReader bufferedReader =
new BufferedReader(new InputStreamReader(
urlConnection.getInputStream()));
String next;
while ((next = bufferedReader.readLine()) != null){
JSONArray ja = new JSONArray(next);
int k=ja.length();
vid_id=pf.loadprefs();
//Toast.makeText(getApplicationContext(), "Here", 500).show();
for (int i = 0; i < ja.length(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
JSONObject jo = (JSONObject) ja.get(i);
WaveData waveData = new WaveData(jo.getString("VUpload"), jo.getInt("recid"),jo.getString("VYoutube"),jo.getString("VMessage"),jo.getString("VThumb"));
if(jo.getInt("recid")>vid_id){
if(i==k-1){
pf.saveprefs(jo.getInt("recid"));
//vid_id=2;
vid_id=jo.getInt("recid");
//Toast.makeText(getApplicationContext(), ""+vid_id, 500).show();
}else{}
}else{}
if(jo.has("VUpload")){
map.put("msg", jo.getString("VMessage"));
map.put("youtube", jo.getString("VYoutube"));
map.put("thumb", jo.getString("VThumb"));
// Toast.makeText(getApplicationContext(), "Here too", 500).show();
myList.add(map);
//Toast.makeText(getApplicationContext(), jo.getString("VMessage"), 500).show();
items.add(jo.getString("VMessage"));
arr_msg.add(jo.getString("VMessage"));
arr_link.add(jo.getString("VYoutube"));
arr_thumb.add(jo.getString("VThumb"));
}
}
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
list=(ListView)findViewById(R.id.list);
adapter=new LazyAdapter(this, myList);
//Toast.makeText(getApplicationContext(), "Here finally", 500).show();
list.setAdapter(adapter);
//Toast.makeText(getApplicationContext(), "Set List ", 500).show();
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
String l=list.getAdapter().getItem(arg2).toString();
Toast.makeText(getApplicationContext(), l, 500).show();
String sthumb=arr_thumb.get(arg2);
String slink=arr_link.get(arg2);
String smsg=arr_msg.get(arg2);
Intent vid=new Intent(getApplicationContext(), SingleItem.class);
vid.putExtra("link", slink);
vid.putExtra("msg", smsg);
vid.putExtra("thumb", sthumb);
startActivity(vid);
}
});
go.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
allItems.clear();
String l= e.getText().toString();
for(int g=0;g<vid_id;g++){
if(myList.get(g).containsValue(l)){
allItems.add(myList.get(g));
}
}
adapter=new LazyAdapter(VideoList.this, allItems);
// Toast.makeText(getApplicationContext(), "Here finally", 500).show();
list.setAdapter(adapter);
}
});
}
}
视频列表无法加载任何问题&amp;即使视频播放,但当我点击后退按钮时,它会回到列表&amp;对不起,这个视频无法播放!
任何想法为何出错?
先谢谢你们!
答案 0 :(得分:1)
解决了这个问题,我所做的就是让视频停止播放onPause&amp; onDestroy方法。
现在它工作正常,没有任何错误