ListView中的setOnPreparedListener

时间:2016-03-19 16:55:08

标签: android

我有一个正在实现MediaPlayer.OnPreparedListener的ListFragment,如下所示,

public class MyList extends ListFragment implements
        LoaderCallbacks<Cursor>, MediaPlayer.OnPreparedListener {

    @Override
    public void onPrepared(MediaPlayer mp) {
        mp.start();

    }
}

这是我的cursorAdatpter

private static class MyCursorAdapter extends CursorAdapter {

           playButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    Uri audioFile = Uri.parse("file:///sdcard/Download/audio.mp3");

                    mPlayer = new MediaPlayer();

                    try {
                        mPlayer.setDataSource(audioFile.toString());
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    mPlayer.setOnPreparedListener(??);

                    mPlayer.prepareAsync();
                }
            });
}

问题出在这个

 mPlayer.setOnPreparedListener(??);

我尝试了下面的内容,但没有一个工作

this
context
getApplicationContext 
ListFragment.this

我应该使用什么?

1 个答案:

答案 0 :(得分:0)

我做了这个并且它正在工作,不知道这是否是正确的方法,但基本上我正在传递cursorAdapter的接口

private static class MyCursorAdapter extends CursorAdapter {

 public MyCursorAdapter(Context context, MediaPlayer mPlayer,MediaPlayer.OnPreparedListener playerListener) {
            super(context, cursor, 0);
            this.playerListener = playerListener;

        }

           playButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    Uri audioFile = Uri.parse("file:///sdcard/Download/audio.mp3");

                    mPlayer = new MediaPlayer();

                    try {
                        mPlayer.setDataSource(audioFile.toString());
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    mPlayer.setOnPreparedListener(playerListener);

                    mPlayer.prepareAsync();
                }
            });
}