如何正确停止媒体播放器
How to stop the media player properly
当用户点击后退按钮时,如何停止片段 activity 和 pageradapter
中的媒体播放器?
每次我点击后退按钮,音频仍在播放。
package com.androidbasedcollectionofstories;
import android.graphics.drawable.AnimationDrawable;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
public class BRUSH1 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
{
// TODO Auto-generated method stub
View view =inflater.inflate(R.layout.brushing1,container,false);
ImageView imageview = (ImageView) view.findViewById (R.id.imageView1);
if(imageview == null) throw new AssertionError();
imageview.setBackgroundResource(R.drawable.animbrushing1);
AnimationDrawable anim = (AnimationDrawable)imageview.getBackground();
anim.start();
final MediaPlayer sound=MediaPlayer.create(getActivity(), R.raw.brushingtitle);
sound.start();
return view;
}
}
如果你需要停止媒体播放器,那么你可以在onDestroyView
方法中停止。
public class BRUSH1 extends Fragment {
MediaPlayer sound;
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view =inflater.inflate(R.layout.brushing1,container,false);
ImageView imageview = (ImageView) view.findViewById (R.id.imageView1);
if(imageview == null) throw new AssertionError();
imageview.setBackgroundResource(R.drawable.animbrushing1);
AnimationDrawable anim = (AnimationDrawable)imageview.getBackground();
anim.start();
sound = MediaPlayer.create(getActivity(), R.raw.brushingtitle);
sound.start();
return view;
}
@Override
public void onDestroyView() {
if (sound != null) {
sound.stop();
sound.release();
}
super.onDestroyView();
}
}
当用户点击后退按钮时,如何停止片段 activity 和 pageradapter
中的媒体播放器?
每次我点击后退按钮,音频仍在播放。
package com.androidbasedcollectionofstories;
import android.graphics.drawable.AnimationDrawable;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
public class BRUSH1 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
{
// TODO Auto-generated method stub
View view =inflater.inflate(R.layout.brushing1,container,false);
ImageView imageview = (ImageView) view.findViewById (R.id.imageView1);
if(imageview == null) throw new AssertionError();
imageview.setBackgroundResource(R.drawable.animbrushing1);
AnimationDrawable anim = (AnimationDrawable)imageview.getBackground();
anim.start();
final MediaPlayer sound=MediaPlayer.create(getActivity(), R.raw.brushingtitle);
sound.start();
return view;
}
}
如果你需要停止媒体播放器,那么你可以在onDestroyView
方法中停止。
public class BRUSH1 extends Fragment {
MediaPlayer sound;
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view =inflater.inflate(R.layout.brushing1,container,false);
ImageView imageview = (ImageView) view.findViewById (R.id.imageView1);
if(imageview == null) throw new AssertionError();
imageview.setBackgroundResource(R.drawable.animbrushing1);
AnimationDrawable anim = (AnimationDrawable)imageview.getBackground();
anim.start();
sound = MediaPlayer.create(getActivity(), R.raw.brushingtitle);
sound.start();
return view;
}
@Override
public void onDestroyView() {
if (sound != null) {
sound.stop();
sound.release();
}
super.onDestroyView();
}
}