2017-04-17 51 views
0

我有recyler列表視圖TextureView和我設置的URI(這是從原料夾視頻)到紋理視圖和適配器此我後實現TextureView.SurfaceTextureListener inteface我讓下面的方法與textureview recyler列表視圖是緩慢滾動時

  1. onSurfaceTextureAvailable
  2. onSurfaceTextureSizeChanged
  3. onSurfaceTextureDestroyed

onSurfaceTextureAvailable我正在設置我的MediaPlayer並啓動視頻。但是,當滾動列表時,問題就出現了。(並且加載速度很慢)。

以下是我的代碼。

在此先感謝。

MyAdapter.java

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> implements TextureView.SurfaceTextureListener { 

private List<Model> list = new ArrayList<>(); 
private Context context; 
private MediaPlayer mediaPlayer =null; 
Uri uri; 


public MyAdapter(Context context, List<Model> l) { 
    this.list = l; 
    this.context = context; 
    uri = Uri.parse("android.resource://" + context.getPackageName() + "/" + R.raw.malhari1); 
} 

@Override 
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
    View itemView = LayoutInflater.from(parent.getContext()) 
      .inflate(R.layout.row_layout, parent, false); 

    return new MyViewHolder(itemView); 
} 


@Override 
public long getItemId(int position) { 
    return super.getItemId(position); 
} 


@Override 
public void onBindViewHolder(MyViewHolder holder, int position) { 
    holder.setIsRecyclable(false); 

    holder.name.setText(list.get(position).getName()); 

    holder.textureView.setSurfaceTextureListener(this); 

} 



@Override 
public int getItemCount() { 
    return list.size(); 
} 

@Override 
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) { 

    System.out.println("onSurfaceTextureAvailable"); 

    setMediaPlayer(surface, uri); 
} 

private void setMediaPlayer(SurfaceTexture surface, Uri uri) { 

    MediaPlayer mediaPlayer = new MediaPlayer(); 
    mediaPlayer.setSurface(new Surface(surface)); 
    try { 
     mediaPlayer.setDataSource(context,uri); 
     mediaPlayer.prepare(); 
     mediaPlayer.start(); 
     // mediaPlayerTemp = mediaPlayer; 
     mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { 
      @Override 
      public void onPrepared(MediaPlayer mp) { 
       if (mp != null) { 
        mp.start(); 
       } 
      } 
     }); 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

@Override 
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) { 

} 

@Override 
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) { 
    System.out.println("onSurfaceTextureDestroyed"); 
    return false; 
} 

@Override 
public void onSurfaceTextureUpdated(SurfaceTexture surface) { 
} 


public class MyViewHolder extends RecyclerView.ViewHolder{ 
    public TextView name; 
    TextureView textureView; 

    public MyViewHolder(View itemView) { 
     super(itemView); 
     name = (TextView)itemView.findViewById(R.id.tvName); 
     textureView = (TextureView) 
itemView.findViewById(R.id.textureView); 
    } 
} 

    } 

MainActivity.java

public class MainActivity extends AppCompatActivity { 

private List<Model> list = new ArrayList<>(); 
RecyclerView recyclerView; 
MyAdapter adapter; 
RecyclerView.LayoutManager mLayoutManager; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    recyclerView = (RecyclerView) findViewById(R.id.recycler_view); 

    adapter = new MyAdapter(MainActivity.this,list); 
    mLayoutManager = new LinearLayoutManager(getApplicationContext()); 
    recyclerView.setLayoutManager(mLayoutManager); 
    recyclerView.setAdapter(adapter); 


    setList(); 

} 

private void setList() { 

    for (int i = 0; i < 15; i++) { 
     Model model = new Model(); 
     model.setName("Name : "+i); 
     list.add(model); 
    } 

    adapter.notifyDataSetChanged(); 

} 
    } 

回答

0

我認爲你正試圖在滾動回收站視圖中播放視頻,請對下面的庫看看: 1)Toro

2)VideoPlayerManager

3)ExoPlayer

+0

是啊vivek我想播放列表中的視頻.. – Harshad07

+0

然後,看看上面的庫以及ExoPlayer在Android .. –

+0

ExoPlayer是Android的MediaPlayer的替代品。 –

0

做到這一點,首先從下載網址的視頻,然後用自定義播放器播放。這裏是我做的:

1)獲取所有網址的需要播放

2)開始從本地存儲的URL下載(隊列)的視頻,並保持在偏好的標誌(一個視頻已經下載或者沒有)

3)指定的網址適配器在初始化視頻播放器控制器,處理視頻播放

4)設置addOnScrollListener檢查哪個位置/視頻是當前可見的,並檢查是否視頻已經下載或對象如果是的話,那就玩吧。