本文介绍了如何在片段不可见阻止媒体播放器的声音?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在我的活动滑块片段。我试图播放声音时的片段可见,当用户滑动到另一片段阻止媒体播放器。这里是我的code。我试过实施onHiddenChanged碎片的方法,但声音继续播放。
公共类WorkoutBuddyViewFragment扩展片段{
MediaPlayer的熔点;
公共WorkoutBuddyViewFragment(){
}
玩的ImageButton;
TextView中显示; // BPPVComment#7:就目前而言,这是一个占位符屏幕。
//覆盖提供传感器辅助练习会话
@覆盖
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
捆绑savedInstanceState){
查看浏览量= NULL; 浏览量= inflater.inflate(R.layout.workout_buddy_dummy,集装箱,FALSE);
玩=(的ImageButton)pageView.findViewById(R.id.playButton);
显示=(TextView中)pageView.findViewById(R.id.textForWOB);
熔点= MediaPlayer.create(getActivity(),R.raw.welcome);
尝试
{
mp.start();
}
赶上(NullPointerException异常E)
{
display.setText(空指针异常);
}
play.setOnClickListener(新View.OnClickListener(){ @覆盖
公共无效的onClick(视图v){
// TODO自动生成方法存根
意图startExercise =新意图(android.intent.action.exercise);
startActivity(startExercise); }
}); 返回浏览量;
} @覆盖
公共无效onHiddenChanged(布尔隐藏){
// TODO自动生成方法存根
super.onHiddenChanged(隐藏);
尝试
{
mp.stop();
}
赶上(NullPointerException异常E)
{
e.printStackTrace();
}
}}
解决方案
我想试试这个, setUserVisibleHint 方式。
@覆盖
公共无效setUserVisibleHint(布尔isVisibleToUser){
super.setUserVisibleHint(isVisibleToUser); //确保我们目前可见
如果(this.isVisible()){
//如果我们正在成为无形的,那么......
如果(!isVisibleToUser){
mp.stop();
}
其他{
// 你喜欢什么
}
}
}
I have a slider fragments in my activity. I am trying to play a sound when the fragment is visible and stop the media player when the user slides to another fragment. Here's my code. I've tried implementing the method onHiddenChanged for the fragment but the sound keeps playing.
public class WorkoutBuddyViewFragment extends Fragment {
MediaPlayer mp;
public WorkoutBuddyViewFragment() {
}
ImageButton play;
TextView display;
// BPPVComment#7: For now, this is a placeholder screen.
// override to provide sensor assisted exercise session
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View pageView = null;
pageView = inflater.inflate(R.layout.workout_buddy_dummy, container, false);
play = (ImageButton) pageView.findViewById(R.id.playButton);
display = (TextView) pageView.findViewById(R.id.textForWOB);
mp = MediaPlayer.create(getActivity(), R.raw.welcome);
try
{
mp.start();
}
catch (NullPointerException e)
{
display.setText("Null Pointer Exception");
}
play.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent startExercise = new Intent("android.intent.action.exercise");
startActivity(startExercise);
}
});
return pageView;
}
@Override
public void onHiddenChanged(boolean hidden) {
// TODO Auto-generated method stub
super.onHiddenChanged(hidden);
try
{
mp.stop();
}
catch(NullPointerException e)
{
e.printStackTrace();
}
}
}
解决方案
I would try this, setUserVisibleHint method.
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
// Make sure that we are currently visible
if (this.isVisible()) {
// If we are becoming invisible, then...
if (!isVisibleToUser) {
mp.stop();
}
else {
// do what you like
}
}
}
这篇关于如何在片段不可见阻止媒体播放器的声音?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!