我正在开发聊天应用,现在我想在我的ListView
中播放视频和音频文件。我可以在活动中播放这些,但不能在适配器类中播放。以下是我的完整代码。并且知道我甚至无法隐藏播放按钮并启动进度条。提前谢谢。
这是XML文件。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:emojicon="http://schemas.android.com/apk/res-auto"
android:gravity="right"
android:orientation="vertical"
android:paddingBottom="5dp"
android:paddingRight="10dp"
android:paddingTop="5dp">
<TextView
android:id="@+id/lblMsgFrom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:textColor="@color/lblFromName"
android:textSize="12dp"
android:textStyle="italic" />
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/tools"
android:id="@+id/cardviewchat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_margin="5dp"
card_view:cardCornerRadius="2dp"
android:visibility="gone"
card_view:contentPadding="10dp">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<ImageView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:src="@drawable/file_icon"
/>
<TextView
android:id="@+id/fileextention"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView"
android:layout_marginLeft="25dp"
android:text="taste.txt"
android:padding="5dp"
android:layout_marginTop="10dp"/>
</LinearLayout>
</android.support.v7.widget.CardView>
<TextView
android:id="@+id/txtMsg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="80dp"
android:background="@drawable/bg_msg_you"
android:paddingBottom="5dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="5dp"
android:visibility="gone"
android:textColor="@color/white"
android:textSize="16dp" />
<ImageView
android:layout_width="200dp"
android:layout_height="200dp"
android:id="@+id/imgmsg"
android:visibility="gone"
android:layout_gravity="right"
android:paddingBottom="5dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="5dp"
android:scaleType="fitCenter"
/>
<FrameLayout
android:id="@+id/video_frame"
android:layout_width="fill_parent"
android:layout_height="480px"
android:background="#000"
android:visibility="gone"
>
<VideoView
android:id="@+id/VideoView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
/>
<ImageButton
android:id="@+id/play_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:src="@drawable/ic_launcher"
/>
<ProgressBar
android:id="@+id/progressbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:visibility="gone" />
</FrameLayout>
这是我的Adapter类
public class MessagesListAdapter extends BaseAdapter {
private Context context;
private List<MessageV> messagesItems;
private VideoView videoview;
private ImageButton Playbutton;
private ProgressBar progressBar;
String VideoURL = "";
public MessagesListAdapter(Context context, List<MessageV> navDrawerItems) {
this.context = context;
this.messagesItems = navDrawerItems;
}
@Override
public int getCount() {
return messagesItems.size();
}
@Override
public Object getItem(int position) {
return messagesItems.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@SuppressLint("InflateParams")
@Override
public View getView(int position, View convertView, ViewGroup parent) {
/**
* The following list not implemented reusable list items as list items
* are showing incorrect data Add the solution if you have one
* */
MessageV m = messagesItems.get(position);
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
// Identifying the message owner
if (messagesItems.get(position).isSelf()) {
// message belongs to you, so load the right aligned layout
convertView = mInflater.inflate(R.layout.list_item_message_right,
null);
} else {
// message belongs to other person, load the left aligned layout
convertView = mInflater.inflate(R.layout.list_item_message_left,
null);
}
TextView lblFrom = (TextView) convertView.findViewById(R.id.lblMsgFrom);
TextView txtMsg = (TextView) convertView.findViewById(R.id.txtMsg);
ImageView imagemsg = (ImageView) convertView.findViewById(R.id.imgmsg);
CardView cartdv = (CardView) convertView.findViewById(R.id.cardviewchat);
TextView msgtext = (TextView) convertView.findViewById(R.id.fileextention);
FrameLayout videoviewframe =(FrameLayout)convertView.findViewById(R.id.video_frame);
videoview = (VideoView)convertView.findViewById(R.id.VideoView);
Playbutton = (ImageButton)convertView.findViewById(R.id.play_button);
progressBar = (ProgressBar)convertView.findViewById(R.id.progressbar);
Playbutton.setTag(position);
lblFrom.setText(m.getFromName());
if(!m.getFile().equalsIgnoreCase("")){
String string = m.getFile();
String[] parts = string.split("files\\/");
String part1 = parts[0]; // 004
String part2 = parts[1];
Log.d("part1",part1);
Log.d("part2",part2);
String[] exten = part2.split(Pattern.quote("."));
String part3 = exten[1];
if(part3.equalsIgnoreCase("png")||part3.equalsIgnoreCase("jpg")||part3.equalsIgnoreCase("jpeg")){
imagemsg.setVisibility(View.VISIBLE);
txtMsg.setVisibility(View.GONE);
Picasso.with(context).load(m.getFile()).into(imagemsg);
}else if(part3.equalsIgnoreCase("mpeg")) {
txtMsg.setVisibility(View.GONE);
videoviewframe.setVisibility(View.VISIBLE);
}else{
txtMsg.setVisibility(View.GONE);
cartdv.setVisibility(View.VISIBLE);
msgtext.setText(part2);
}
}else{
txtMsg.setVisibility(View.VISIBLE);
imagemsg.setVisibility(View.GONE);
txtMsg.setText(m.getMessage());
}
// imagemsg.setImageBitmap(m.getFile());
Playbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int pos = Integer.parseInt(String.valueOf(v.getTag()));
Playbutton.setVisibility(View.GONE);
//pDialog.show();
progressBar.setVisibility(View.VISIBLE);
VideoURL=messagesItems.get(pos).getFile();
// VideoPlay();
try {
MediaController mediacontroller = new MediaController(
context);
mediacontroller.setAnchorView(videoview);
// Get the URL from String VideoURL
Uri video = Uri.parse(VideoURL);
videoview.setMediaController(mediacontroller);
videoview.setVideoURI(video);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
videoview.requestFocus();
videoview.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
// Close the progress bar and play the video
public void onPrepared(MediaPlayer mp) {
//pDialog.dismiss();
progressBar.setVisibility(View.GONE);
DisplayMetrics displaymetrics = new DisplayMetrics();
((Activity)context).getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int h = displaymetrics.heightPixels;
int w = displaymetrics.widthPixels;
// Start the MediaController
videoview.setLayoutParams(new FrameLayout.LayoutParams(w, h));
videoview.start();
}
});
}
});
return convertView;
}
}
答案 0 :(得分:2)
Videoview无法与listview和recyclerview等滚动视图一起使用。查看此链接,了解如何实施
希望你觉得它很有用