我有一个从项目名称数组填充的列表视图。它由 List(类)和 list_item(布局)设置。现在,我为列表视图行设置了单击侦听器,以在使用 ClipsActivity (class) activity_clips(layout) 设置的 VideoView 中播放视频。
问题:单击 ListView 中的行时,下一个活动(ClipsActivity)会加载警报“抱歉,无法播放此视频”。我肯定在资产文件夹中有视频文件。我相信 - 问题可能在于字符串从 ClickListener 传递/解析到 videoview 的方式。希望有人在这方面帮助我。
用于 List 类的代码
package com.abcdef.list;
//import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
//import android.widget.AdapterView;
//import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
//import android.widget.TextView;
public class List extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// storing string resources into Array
String[] clips_files = getResources().getStringArray(R.array.clips_files);
// Binding resources Array to ListAdapter
this.setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, R.id.label, clips_files));
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
change(position);
}
void change(int position){
Intent intent = new Intent(getApplicationContext(), ClipsActivity.class);
switch(position){
case 0 :
intent.setData(Uri.parse("file:///android_asset/A1.mp4"));
break;
case 1 :
intent.setData(Uri.parse("file:///android_asset/A2.mp4"));
break;
case 2 :
intent.setData(Uri.parse("file:///android_asset/A3.mp4"));
break;
case 3 :
intent.setData(Uri.parse("file:///android_asset/A4.mp4"));
break;
}
startActivity(intent);
}
}
这是用于带有 VideoView 的 ClipsActivity 类的代码
包 com.abcde.list;
import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.widget.MediaController;
import android.widget.VideoView;
import java.net.URI;
@SuppressWarnings("unused")
public class ClipsActivity extends Activity {
VideoView mVideoView;
MediaController mc;
// String uriPath;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_clips);
final VideoView mVideoView = (VideoView)findViewById(R.id.videoview);
MediaController mc = new MediaController(this);
mc.setAnchorView(mVideoView);
mVideoView.setMediaController(mc);
mVideoView.setVideoPath((getIntent().getDataString());
mVideoView.start();
mVideoView.setOnCompletionListener(
new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mVideoView.start();
}
});
}
}