我正在构建一个涉及视频播放器的应用程序,我有一个列表视图,它显示视频列表,然后单击其中任何一个应该播放该视频。我使用的链接是 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);
}
});
}
}
视频列表加载没有问题,甚至视频播放,但是当我点击后退按钮时,它回到列表并说对不起,这个视频无法播放!
有什么想法为什么会出错?
提前谢谢你们!