我如何在片段中设置MediaMedia?

时间:2017-10-03 04:44:29

标签: android android-fragments android-mediaplayer android-videoview

我有一个片段,我正在使用媒体播放器并使用我自己的自定义控制器类。我无法在片段内设置controller.setMediaPlayer。我在片段中实现了VideoControllerView.MediaPlayerControl类。

我的片段类是:

public class HomeFragment extends Fragment implements SurfaceHolder.Callback, VideoControllerView.MediaPlayerControl {

@BindView(R.id.videoSurfaceContainer)
FrameLayout videoContainer;
@BindView(R.id.videoSurface)
SurfaceView videoSurface;
@BindView(R.id.progress)
ProgressBar progressBar;
@BindView(R.id.errorTv)
RelativeLayout relativeLayout;
@BindView(R.id.errorTextView)
TextView errorTextView;


public MediaPlayer player;
public VideoControllerView controller;
private  Context context;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View v = inflater.inflate(R.layout.fragment_home, container, false);
    ButterKnife.bind(this, v);

    player = new MediaPlayer();

    controller = new VideoControllerView(getActivity());

    SurfaceHolder videoHolder = videoSurface.getHolder();
    videoHolder.addCallback(this);

    progressBar.setVisibility(View.VISIBLE);

    playVideo("http://202.166.200.170:8081/otv/aastha.stream/playlist.m3u8");

    relativeLayout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            try {
                relativeLayout.setVisibility(View.GONE);
                player.reset();
                progressBar.setVisibility(View.VISIBLE);
                playVideo("http://202.166.200.170:8081/otv/aastha.stream/playlist.m3u8");

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

    return v;
}

@Override
public void surfaceCreated(SurfaceHolder surfaceHolder) {
    player.setDisplay(surfaceHolder);
}

@Override
public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {

}

@Override
public void surfaceDestroyed(SurfaceHolder surfaceHolder) {

}


public void playVideo(String link) {
    try {
        videoSurface.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                if (controller.isShowing())
                    controller.hide();
                else
                    controller.show();

                return false;
            }

        });

        player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                controller.setMediaPlayer();
                controller.setAnchorView((FrameLayout) getActivity().findViewById(R.id.videoSurfaceContainer));
                player.start();
                progressBar.setVisibility(View.GONE);

                if (relativeLayout.isShown())
                    relativeLayout.setVisibility(View.GONE);
            }
        });

        player.setAudioStreamType(AudioManager.STREAM_MUSIC);
        player.setDataSource(getActivity(), Uri.parse(link));
        try {

            player.prepareAsync();

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

        player.setOnErrorListener(new MediaPlayer.OnErrorListener() {
            @Override
            public boolean onError(MediaPlayer mp, int what, int extra) {

                progressBar.setVisibility(View.GONE);

                errorMessage("Streaming Error");
                player.reset();
                errorTextView.setText(getResources().getString(R.string.error_msg_video));
                return false;
            }
        });

        player.setScreenOnWhilePlaying(true);
        Log.d("after click", "last try");
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public void errorMessage(String message) {
    try {
        relativeLayout.setVisibility(View.VISIBLE);
        progressBar.setVisibility(View.GONE);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

@Override
public void start() {

}

@Override
public void pause() {

}

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

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

@Override
public void seekTo(int pos) {

}

@Override
public boolean isPlaying() {
    return false;
}

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

@Override
public boolean isFullScreen() {
    return false;
}

@Override
public void toggleFullScreen() {

}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    context = activity;

}

如果你在setOnpreparedListener中看到里面的playVideo方法我在controller.setMediaPlayer()中收到错误,因为我无法在这里传递上下文。

这里可以做些什么来解决错误?

3 个答案:

答案 0 :(得分:0)

这就是我们如何使用controller.setMediaPlayer()

// Implement MediaPlayer.OnPreparedListener
@Override
public void onPrepared(MediaPlayer mp) {
controller.setMediaPlayer(this);
controller.setAnchorView((FrameLayout) 
 findViewById(R.id.videoSurfaceContainer));
player.start();
}
// End MediaPlayer.OnPreparedListener

This is example to use costume controller

答案 1 :(得分:0)

如果您愿意,可以使用HomeFragment.this.getContext()从那里获取上下文,例如:

player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer mp) {
            controller.setMediaPlayer(HomeFragment.this.getContext());
            controller.setAnchorView((FrameLayout) getActivity().findViewById(R.id.videoSurfaceContainer));
            player.start();
            progressBar.setVisibility(View.GONE);

            if (relativeLayout.isShown())
                relativeLayout.setVisibility(View.GONE);
        }
    });

答案 2 :(得分:0)

我刚刚将Button button = view.findViewById(R.id.buttonPanel2); button.setOnFocusChangeListener(new View.OnFocusChangeListener() { @Override public void onFocusChange(View view, boolean focus) { if(focus){ view.performClick(); } } }); 移到了onCreateView上,但它确实有效。