2017-08-16 4 views
0

私のカスタムアダプタには、いくつかのテキストビューと2つのボタンがあります。 1つはPlayで、もう1つはFavoriteボタンです。再生ボタンを押すと、生のフォルダに保存されたオーディオファイルが再生され、開始、一時停止、転送ボタンなどのメディアコントローラも表示されます。ここに私のカスタムアダプターコードです。カスタムアダプタのMediaControllerが表示されない

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> 

?私はオーディオファイルが再生されて聞こえるが。

おかげ

答えて

0

あなたのLinearLayoutの@ idは/ forMediaControllerが空と "wrap_content" として定義されているので、レイアウトが0dpの高さと、表示されません。

MediaController "setAnchorView()"メソッドは、より一般的には、画面のルートレイアウトのようなビューを設定するために使用されます。

あなたの場合、リストの各行にmediaControllerを追加しますか?いずれの場合でも、アイテムビューのルートビューなど、他の要素を含むanchorViewを設定する方がよいでしょう。

コントロールビューのアンカーとして機能するビューを設定します。これは、たとえばVideoViewまたはあなたのアクティビティのメインビューである です。 VideoViewがこのメソッドを呼び出すと、VideoViewの親を アンカーとして使用します。

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

+0

私は100dpとしてのLinearLayoutの高さを設定しようとしたが、それはうまくいきませんでした。また、そのレイアウトでVideoViewを設定し、MediaControllerでバインドしようとしましたが、それは同じです。私はリストビューを含むが失敗した親レイアウト(フラグメントのレイアウト)を設定しようとしました! 次のようにonPreparedメソッドを変更すると、不正なトークン例外が発生します。次の例外が発生します android.view.WindowManager $ BadTokenException:ウィンドウを追加できません - トークンnullが無効です。あなたの活動は実行されていますか? –

+1

しかし、onPrepared()が呼び出されると、linearlayout変数が設定されていることは確かですか?あなたのMediaPlayerとMediaControllerをあなたのアダプタから外したほうがいいでしょう。これは実際にgetView()の各呼び出しでこれを複製する良い方法ではありません。 – smora

+0

あなたが言ったように、MediaPlayerとMediaControllerをFragmentクラスに配置したいと考えていました。 –

関連する問題