3

我想要做的是用一个 BaseAdapter 填充一个 ListView ,其中有一个 TextureView 。问题是我在等待至少 2 分钟之前看不到任何视频播放,这与我的互联网连接速度无关。当我将 TextureView 的宽度设置为 300dp 时,我的 ListView 开始包含多个视图,并且所有视图都开始播放视频,从而造成混乱。我希望我的程序像 Vime 或 Instapram 应用程序一样工作,当它有焦点(或可见)时自动启动,当我滚动时暂停。我在网上搜索了 5 个小时,但仍然一无所获。任何指南、帮助、示例代码将不胜感激。提前致谢。

public class VideoAdapter extends BaseAdapter {
private Activity context;
private ArrayList<String> urls;
private MediaPlayer mediaPlayer;

public VideoAdapter(Activity context, ArrayList<String> urls) {
    this.context = context;
    this.urls = urls;

}

@Override
public int getCount() {
    return urls.size();
}

@Override
public Object getItem(int position) {
    return urls.get(position);
}

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

@Override
public View getView(int positionx, View convertView, ViewGroup parent) {
    final ViewHolderItems viewHolderItems;
    SurfaceHolder surfaceHolder;
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if (convertView == null) {
        convertView = inflater.inflate(R.layout.single_video_item, parent, false);
        viewHolderItems = new ViewHolderItems();
        viewHolderItems.textureView = (TextureView) convertView.findViewById(R.id.textureView);
        convertView.setTag(viewHolderItems);
    }else{
        viewHolderItems = (ViewHolderItems) convertView.getTag();
    }
    final int position = positionx;
    LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)viewHolderItems.textureView.getLayoutParams();
    params.height = 850;
    params.width = LinearLayout.LayoutParams.MATCH_PARENT;
    viewHolderItems.textureView.setLayoutParams(params);
    mediaPlayer = new MediaPlayer();
    viewHolderItems.textureView.setSurfaceTextureListener(new TextureView.SurfaceTextureListener() {
        @Override
        public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
            mediaPlayer.setSurface(new Surface(surface));
            mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            try {
                mediaPlayer.setDataSource(urls.get(position));
                Log.d("JWP", "" + urls.get(position));
                mediaPlayer.prepareAsync();
                mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                    @Override
                    public void onPrepared(MediaPlayer mp) {
                        mp.start();
                    }
                });
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

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

        }

        @Override
        public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
            /*mediaPlayer.stop();
            mediaPlayer.release();*/
            return false;
        }

        @Override
        public void onSurfaceTextureUpdated(SurfaceTexture surface) {
        }
    });

    return convertView;
}
static class ViewHolderItems{
    TextureView textureView;
}
}

我没有任何崩溃,但仍然出现错误:

E/MediaPlayer﹕ attachNewPlayer called in state 4
W/System.err﹕ java.lang.IllegalStateException
W/System.err﹕ at android.media.MediaPlayer.nativeSetDataSource(Native Method)
W/System.err﹕ at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1061)
W/System.err﹕ at android.view.TextureView.getHardwareLayer(TextureView.java:370)
W/System.err﹕ at android.view.View.updateDisplayListIfDirty(View.java:14025)
W/System.err﹕ at android.view.View.getDisplayList(View.java:14071)
W/System.err﹕ at android.view.View.draw(View.java:14838)
W/System.err﹕ at android.view.ViewGroup.drawChild(ViewGroup.java:3404)
W/System.err﹕ at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198)
W/System.err﹕ at android.view.View.updateDisplayListIfDirty(View.java:14043)
W/System.err﹕ at android.view.View.getDisplayList(View.java:14071)
W/System.err﹕ at android.view.View.draw(View.java:14838)
4

0 回答 0