自定义适配器上的MediaController未显示

时间:2017-08-16 10:44:12

标签: android custom-adapter mediacontroller

在我的自定义适配器中有几个文本视图和两个按钮。一个是Play,另一个是Favorite按钮。按“播放”按钮应该播放存储在原始文件夹中的音频文件,它还应该向媒体控制器显示“开始”,“暂停”,“转发”等按钮。这是我的自定义适配器代码。

package com.codetrio.alquran.com.codetrio.alquran.util;
import android.app.Dialog;
import android.content.Context;
import android.media.MediaPlayer;
import android.os.AsyncTask;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.MediaController;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.codetrio.alquran.R;
import com.codetrio.alquran.db.AudioFileDataSource;
import com.codetrio.alquran.db.ChapterDataSource;
import com.codetrio.alquran.model.AudioFile;
import com.codetrio.alquran.model.Chapter;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

public class ChapterListAdapter extends ArrayAdapter<Chapter> implements 
MediaPlayer.OnPreparedListener, View.OnClickListener,
    MediaController.MediaPlayerControl{
ArrayList<Chapter> arabicChapters;
ArrayList<Chapter> banglaChapters;
Context mContext;
private int lastPosition = -1;
MediaPlayer mp;
private MediaController mediaController;

private Handler handler = new Handler();

ChapterDataSource dataSource;
AudioFileDataSource audioFileDataSource;
AudioFile audioFile;
Chapter chapter, chapterBangla;
MyHttpAction myHttpAction;
LinearLayout linearLayout;

@Override
public void start() {
    mp.start();
}

@Override
public void pause() {
    mp.pause();
}

@Override
public int getDuration() {
    return mp.getDuration();
}

@Override
public int getCurrentPosition() {
    return mp.getCurrentPosition();
}

@Override
public void seekTo(int i) {
    mp.seekTo(i);
}

@Override
public boolean isPlaying() {
    return mp.isPlaying();
}

@Override
public int getBufferPercentage() {
    return 0;
}

@Override
public boolean canPause() {
    return true;
}

@Override
public boolean canSeekBackward() {
    return true;
}

@Override
public boolean canSeekForward() {
    return true;
}

@Override
public int getAudioSessionId() {
    return 0;
}

@Override
public void onPrepared(final MediaPlayer mediaPlayer) {
    try{
        mediaController.setMediaPlayer(this);
        mediaController.setAnchorView(linearLayout);
        handler.post(new Runnable() {
            public void run() {
                mediaController.setEnabled(true);
                if(mediaPlayer.isPlaying()){
                    Toast.makeText(mContext, "here...", Toast.LENGTH_LONG).show();
                    mediaController.show();
                }else{
                    Toast.makeText(mContext, "not playing...", Toast.LENGTH_LONG).show();
                }
            }
        });
    }catch (Exception e){

    }
}

private static class ViewHolder{
    TextView tvChapterNo;
    TextView tvChapterNameBangla;
    TextView tvChapterName;
    TextView tvRevealedAt;
    TextView tvTotalVerse;
    Button btnPlay;
    Button btnFavorite;
    LinearLayout linerLayout;
}

public ChapterListAdapter(ArrayList<Chapter> arabicChapters, ArrayList<Chapter> banglaChapters, Context context){
    super(context, R.layout.chapter_item, arabicChapters);
    this.arabicChapters = arabicChapters;
    this.banglaChapters = banglaChapters;
    this.mContext = context;
}

@Override
public void onClick(View view) {
    int position=(Integer) view.getTag();
    Object object= getItem(position);
    Chapter chapter=(Chapter)object;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) 
{
    // Get the data item for this position
    chapter = arabicChapters.get(position);
    chapterBangla = banglaChapters.get(position);

    mp = new MediaPlayer();
    mp = MediaPlayer.create(mContext, R.raw.fatiha);
    mp.setOnPreparedListener(this);
    mediaController = new MediaController(mContext);

    // Check if an existing view is being reused, otherwise inflate the view
    final ViewHolder viewHolder; // view lookup cache stored in tag

    View view;

    if (convertView == null) {
        viewHolder = new ViewHolder();
        LayoutInflater inflater = LayoutInflater.from(getContext());
        convertView = inflater.inflate(R.layout.chapter_item, parent, false);
        viewHolder.tvChapterNo = (TextView) convertView.findViewById(R.id.tvChapterNo);
        viewHolder.tvChapterNameBangla = (TextView) convertView.findViewById(R.id.tvChapterNameBangla);
        viewHolder.tvChapterName = (TextView) convertView.findViewById(R.id.tvChapterName);
        viewHolder.tvRevealedAt = (TextView) convertView.findViewById(R.id.tvRevealedAt);
        viewHolder.tvTotalVerse = (TextView) convertView.findViewById(R.id.tvTotalVerses);
        viewHolder.btnPlay = (Button) convertView.findViewById(R.id.btnPlay);
        viewHolder.btnFavorite = (Button) convertView.findViewById(R.id.btnFavorite);
        viewHolder.linerLayout = (LinearLayout)convertView.findViewById(R.id.forMediaController);
        linearLayout = (LinearLayout)convertView.findViewById(R.id.forMediaController);

        viewHolder.btnFavorite.setTag( chapter.getId() );

        view=convertView;

        convertView.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolder) convertView.getTag();
        view=convertView;
    }

    viewHolder.tvChapterNo.setText(Integer.toString(chapter.getId()));
    viewHolder.tvChapterNameBangla.setText(chapterBangla.getChapterName());
    viewHolder.tvChapterName.setText(chapter.getChapterName());
    viewHolder.tvRevealedAt.setText("Mecca");
    viewHolder.tvTotalVerse.setText("আয়াত সংখ্যা - "+Integer.toString(chapter.getTotalVerses()));

    viewHolder.btnPlay.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View view) {
            chapter = arabicChapters.get(position); //re initializing otherwise sometimes get previous state
            chapterBangla = banglaChapters.get(position);
            audioFileDataSource = new AudioFileDataSource(getContext());
            audioFile = new AudioFile();
            audioFileDataSource.open();
            audioFile = audioFileDataSource.getAudioFileByChapter(chapter.getId());
            audioFileDataSource.close();
            if(audioFile.getFilePathLocal() != null){

            }else{
                File file = new File("/data/data/com.codetrio.alquran/"+Integer.toString(chapter.getId())+".mp3");
                if(file.exists()){
                    mp.start();
                }else{
                }
            }
        }
    });
    viewHolder.btnFavorite.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View view) {
            chapter = arabicChapters.get(position); //re initializing otherwise sometimes get previous state
            chapterBangla = banglaChapters.get(position);
            dataSource = new ChapterDataSource(getContext());
            dataSource.open();
            dataSource.setAsFavorite(chapter.getId(), chapterBangla.getId());
            dataSource.close();
        }
    });
    return convertView;
}
}

我也在这里分享布局代码。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/tvChapterNo"
        android:gravity="center"
        android:layout_width="60dp"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:background="#DDD000"
        android:text="001"
        android:textStyle="bold"
        android:textSize="20dp"
        android:textColor="#000"
        android:focusable="false"
        android:focusableInTouchMode="false"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:gravity="center"
        android:weightSum="2">

        <TextView
            android:id="@+id/tvChapterNameBangla"
            android:gravity="center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:background="#DDD000"
            android:text="Name of the sura"
            android:textStyle="bold"
            android:textSize="20dp"
            android:textColor="#000"
            android:layout_weight="1"
            android:focusable="false"
            android:focusableInTouchMode="false"/>

        <TextView
            android:id="@+id/tvChapterName"
            android:gravity="center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:background="#DDD000"
            android:text="Name of the sura"
            android:textStyle="bold"
            android:textSize="20dp"
            android:textColor="#000"
            android:layout_weight="1"
            android:focusable="false"
            android:focusableInTouchMode="false"/>

    </LinearLayout>


</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:gravity="center">

    <TextView
        android:id="@+id/tvRevealedAt"
        android:gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:background="#DDD000"
        android:text="মক্কায় অবতীর্ন"
        android:textSize="15dp"
        android:textColor="#000"
        android:focusable="false"
        android:focusableInTouchMode="false"/>

    <TextView
        android:id="@+id/tvTotalVerses"
        android:gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:background="#DDD000"
        android:text="- ০৭ টি আয়াত"
        android:textSize="15dp"
        android:textColor="#000"
        android:focusable="false"
        android:focusableInTouchMode="false"/>

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:gravity="center">
    <Button
        android:id="@+id/btnPlay"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Play"
        android:focusable="false"
        android:focusableInTouchMode="false"/>

    <Button
        android:id="@+id/btnFavorite"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Favorite"
        android:focusable="false"
        android:focusableInTouchMode="false" />
</LinearLayout>
<LinearLayout
    android:id="@+id/forMediaController"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
</LinearLayout>

不显示媒体控制器的原因是什么?虽然我能听到音频文件在播放。

由于

1 个答案:

答案 0 :(得分:0)

您的LinearLayout @ id / forMediaController为空并定义为“wrap_content”,因此布局为0dp高度且不可见。

MediaController“setAnchorView()”方法更常用于设置视图,就像屏幕的根布局一样。

在你的情况下,你想在列表的每一行添加mediaController?在任何情况下,您最好设置一个包含其他元素的anchorView,例如项目视图的根视图。

  

设置充当控件视图锚点的视图。这个可以   例如,可以是VideoView,也可以是Activity的主视图。什么时候   VideoView调用此方法,它将使用VideoView的父级作为   锚

https://developer.android.com/reference/android/widget/MediaController.html#setAnchorView(android.view.View)