Android:如何控制何时在不同的片段页面中启动/停止声音?

时间:2017-05-06 21:19:00

标签: android android-fragments android-mediaplayer

我的活动有一个18页的片段。在某些页面中,单击播放按钮时可以播放声音。但是,当用户点击播放按钮并切换到另一页时,前一页的声音仍在播放。如何才能在相关页面中播放,如果页面被切换则停止?

public class PageFragment_Bon extends Fragment implements View.OnClickListener{

    public static final String ARG_PAGE = "ARG_PAGE";

    private int mPage;
    private Button start, stop, replay;
    private MediaPlayer mediaPlayer;
    int [] filer = new int[18];


    public static PageFragment_Bon newInstance(int page) {
        Bundle args = new Bundle();
        args.putInt(ARG_PAGE, page);
        PageFragment_Bon fragment = new PageFragment_Bon();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mPage = getArguments().getInt(ARG_PAGE);
           }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_boenner, container, false);

        start = (Button) view.findViewById(R.id.start);
        start.setOnClickListener(this);

        stop = (Button) view.findViewById(R.id.stop);
        stop.setOnClickListener(this);

        replay = (Button) view.findViewById(R.id.replay);
        replay.setOnClickListener(this);

        filer[2] = R.raw.Hello;
        filer[4] = R.raw.Welcome;
        filer[14] = R.raw.Intro;
        filer[15] = R.raw.Farewell;
        filer[16] = R.raw.Leave;

        if(filer[mPage] != 0){
            start.setVisibility(View.VISIBLE);
            stop.setVisibility(View.VISIBLE);
            replay.setVisibility(View.VISIBLE);
        }

        return view;
    }

    @Override
    public void onResume() {
        super.onResume();

        if(mediaPlayer != null)
        {
            mediaPlayer.seekTo(0);
            mediaPlayer.start();
        }

    }

    @Override
    public void onPause() {
        super.onPause();

        if(mediaPlayer != null)
        {
            mediaPlayer.pause();
        }

    }

    @Override
    public void onClick(View v) {

        if(mediaPlayer == null)
            mediaPlayer = MediaPlayer.create(getActivity().getBaseContext(), filer[mPage]);
        if(v == start){
            mediaPlayer.start();
        }

        else if(v == stop){
            mediaPlayer.pause();
        }

        else if(v == replay){
            mediaPlayer.seekTo(0);
            mediaPlayer.start();
        }
    }


    }

2 个答案:

答案 0 :(得分:0)

你可以在有声音的片段中尝试这样的事情

@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
    super.setUserVisibleHint(isVisibleToUser);

    if(!isVisibleToUser){

        if(mediaPlayer!=null) {
            if (mediaPlayer.isPlaying()) {
                try {
                    mediaPlayer.reset();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

    }
}

通过覆盖setUserVisibleHint中的Fragment,您可以确定片段何时可​​见

修改

使用try and catch启动媒体播放器(如果您在尝试启动媒体播放器时有任何异常)

   if(v == start){
        try {
            mediaPlayer.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

答案 1 :(得分:0)

It is better to override onPause() and onResume() in fragment. And we need to save the current position for media player. So, I am using the shared preference to save the position for media player.
In onCreateView, mediaPlayer_position = getActivity().getSharedPreferences("PLAY_PAUSE", Activity.MODE_PRIVATE).getInt("CHECK_PLAY_PAUSE", 0);

@Override
public void onResume() {
    super.onResume();
   int position = getActivity().getSharedPreferences("PLAY_PAUSE", Activity.MODE_PRIVATE).getInt("CHECK_PLAY_PAUSE", 0);
    if(position > 0) {
        try {
            mediaPlayer.setDataSource(mp3_link);//mp3_link from url to MediaPlayer data source
            mediaPlayer.prepare(); 
        } catch (Exception e) {
            e.printStackTrace();
        }

        mediaPlayer.seekTo(position);
        mediaPlayer.start();

    }

}

@Override
public void onPause() {
    super.onPause();
    if(mediaPlayer.isPlaying()) {
        mediaPlayer_position = mediaPlayer.getCurrentPosition();
        getActivity().getSharedPreferences("PLAY_PAUSE", Context.MODE_PRIVATE).edit().putInt("CHECK_PLAY_PAUSE", mediaPlayer_position).apply();
        mediaPlayer.pause();
    }

}

Ref: "https://www.hrupin.com/2011/02/example-of-streaming-mp3-mediafile-with-android-mediaplayer-class"