Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java MediaPlayer赢得';不要停止播放,即使已编程为_Java_Android_Android Fragments_Android Studio_Android Mediaplayer - Fatal编程技术网

Java MediaPlayer赢得';不要停止播放,即使已编程为

Java MediaPlayer赢得';不要停止播放,即使已编程为,java,android,android-fragments,android-studio,android-mediaplayer,Java,Android,Android Fragments,Android Studio,Android Mediaplayer,我制作了一个简单的类,它处理与声音相关的一切。具有添加、播放、停止、释放和释放全部功能。它的工作原理是,您必须添加一首歌曲,然后通过您添加的歌曲名称调用play。任何时候需要停止,只需调用stop函数并将歌曲名称作为参数传递,它就会停止。我的问题是,即使它通过stop(),它也不会停止 声音等级: public class Sound { private Map<String, MediaPlayer> songs = new HashMap<String, Media

我制作了一个简单的类,它处理与声音相关的一切。具有添加、播放、停止、释放和释放全部功能。它的工作原理是,您必须添加一首歌曲,然后通过您添加的歌曲名称调用play。任何时候需要停止,只需调用stop函数并将歌曲名称作为参数传递,它就会停止。我的问题是,即使它通过
stop()
,它也不会停止

声音等级:

public class Sound
{
    private Map<String, MediaPlayer> songs = new HashMap<String, MediaPlayer>();

    private MediaPlayer currentlyPlayingSong;

    public Sound() {}

    public void Add(int songId, String songName, Context context)
    {
        MediaPlayer song = MediaPlayer.create(context, songId);

        songs.put(songName, song);
    }

    public void Play(String name, boolean shouldLoop)
    {
        MediaPlayer songToPlay = songs.get(name);

        if ( songToPlay != currentlyPlayingSong && songToPlay != null) 
    {
        currentlyPlayingSong = songToPlay;

        currentlyPlayingSong.start();

        currentlyPlayingSong.setLooping(shouldLoop);
    }
    }

    public void Stop(String name)
    {
        MediaPlayer songToStop = songs.get(name);
        if (songToStop != null)
        {
            songToStop.setLooping(false);

            songToStop.stop();
        }
    }

    public void Release(String name)
    {
        songs.get(name).release();
    }

    public void ReleaseAll()
    {
       LinkedList<MediaPlayer> _songs;

        _songs = (LinkedList)songs.values();

        for (int i = 0; i < _songs.size(); i++)
        {
            _songs.get(i).release();
        }
    }
}
片段:

public class MainMenuFragment extends Fragment {

    private ImageButton soundImgBtn;

    private FragmentConfig fragmentConfig;

    public MainMenuFragment()
    {
        fragmentConfig = new FragmentConfig();
    }

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

        soundImgBtn = (ImageButton)view.findViewById(R.id.soundImgBtn);
        //endregion

        //region Listeners

        soundImgBtn.setOnClickListener(
                new View.OnClickListener()
                {
                    @Override
                    public void onClick(View v)
                    {
                        SoundImgBtnClick(v);
                    }
                }
        );
        //endregion

        //Changes audio img
        if (((Main)getActivity()).GetIsSoundOn())
            soundImgBtn.setImageResource(android.R.drawable.ic_lock_silent_mode_off);

        else
            soundImgBtn.setImageResource(android.R.drawable.ic_lock_silent_mode);

        // Inflate the layout for this fragment
        return view;
    }

    private void SoundImgBtnClick(View v)
    {
        //if sound is on and clicked, turn off
        if (((Main)getActivity()).GetIsSoundOn())
        {
            ((Main)getActivity()).SetIsSoundOn(false);

            ((Main)getActivity()).GetSoundObj().Stop("mainMenuSong");

            soundImgBtn.setImageResource(android.R.drawable.ic_lock_silent_mode);
        }

        else
        {
            ((Main)getActivity()).SetIsSoundOn(true);

            ((Main)getActivity()).GetSoundObj().Play("mainMenuSong", true);

            soundImgBtn.setImageResource(android.R.drawable.ic_lock_silent_mode_off);
        }
    }

}
我想做的是模拟一个静音按钮。单击后,所有声音都应静音

到目前为止,这几乎是我编写的全部代码


干杯。

请尝试使用songToStop.release()

我怀疑您使用的是不同的MediaPlayer实例。您可以这样做,但必须在同一实例中停止歌曲。 关于Add()中的代码:

在Stop()中:

注意

  • 上面的代码告诉我,您正在为同一首歌曲使用不同的MediaPlayer实例。需要在更高的范围内声明对象
    歌曲
    ,以便您访问它并停止歌曲
  • 需要在stop()之后调用release()方法以释放资源

    • 让它停下来。我的班级必须能够处理一首歌在同一时间和许多外汇。这就是我想到的

      声音:

      public class Sound
      {
          private static MediaPlayer currentlyPlayingSong,
          currentlyPlayingFX;
      
          public Sound() {}
      
          public void PlayFX(int fxId, Context context, boolean shouldLoop)
          {
              MediaPlayer fx = MediaPlayer.create(context, fxId);
      
              if (currentlyPlayingFX != fx)
              {
                  StopFX();
      
                  currentlyPlayingFX = fx;
      
                  currentlyPlayingFX.start();
      
                  currentlyPlayingFX.setLooping(shouldLoop);
              }
          }
      
          public void PlaySong(int songId, boolean shouldLoop, Context context)
          {
              MediaPlayer song = MediaPlayer.create(context, songId);
      
              if (currentlyPlayingSong != song)
              {
                  StopSong();
      
                  currentlyPlayingSong = song;
      
                  currentlyPlayingSong.start();
      
                  currentlyPlayingSong.setLooping(shouldLoop);
              }
          }
      
          public void StopFX()
          {
              if (currentlyPlayingFX != null)
              {
                  currentlyPlayingFX.stop();
      
                  currentlyPlayingFX.release();
      
                  currentlyPlayingFX = null;
              }
          }
      
          public void StopSong()
          {
              if (currentlyPlayingSong != null)
              {
                  currentlyPlayingSong.stop();
      
                  currentlyPlayingSong.release();
      
                  currentlyPlayingSong = null;
              }
          }
      }
      
      这是基于@原安卓的回答。将其保留在单个实例上


      谢谢你的帮助

      如果我想再次调用同一首歌,这难道不会给我一个例外吗?我需要制作一个静音按钮,这就是停止的目的。它在模仿哑巴。如果我释放,那么我将不得不创建另一个对象。我不明白,你说的静音是什么意思?您要停止歌曲还是将歌曲静音。如果你想“停止”这首歌,有一个.pause()函数。我想停止这首歌和静音是一样的。我需要它在调用
      Play
      时从头开始。“暂停”将暂停,当再次播放时,我假设它将从暂停处继续播放。我希望音频完全停止,但我不想发布歌曲它必须是同一个实例?该死的。我认为通过将
      song
      添加到
      songs
      中,然后将
      songs.get(name)
      分配给一个局部变量,它将继承它的所有值。出于同样的原因,歌曲是全局性的,因为我需要在任何地方访问歌曲地图。正如您所看到的,我没有使用
      isplay()
      ,因为大多数时间在实际播放时返回false。可能是像@Original Android Suggestions这样的实例问题的原因。在片段上,我在需要的地方调用了
      PlaySong
      currentlyPlaying
      变量是为了避免使用
      isPlaying
      。我已经习惯了不正确,我只是想不惜一切代价避免它lol
      MediaPlayer song = MediaPlayer.create(context, songId);
      
      MediaPlayer songToStop = songs.get(name)
      
      public class Sound
      {
          private static MediaPlayer currentlyPlayingSong,
          currentlyPlayingFX;
      
          public Sound() {}
      
          public void PlayFX(int fxId, Context context, boolean shouldLoop)
          {
              MediaPlayer fx = MediaPlayer.create(context, fxId);
      
              if (currentlyPlayingFX != fx)
              {
                  StopFX();
      
                  currentlyPlayingFX = fx;
      
                  currentlyPlayingFX.start();
      
                  currentlyPlayingFX.setLooping(shouldLoop);
              }
          }
      
          public void PlaySong(int songId, boolean shouldLoop, Context context)
          {
              MediaPlayer song = MediaPlayer.create(context, songId);
      
              if (currentlyPlayingSong != song)
              {
                  StopSong();
      
                  currentlyPlayingSong = song;
      
                  currentlyPlayingSong.start();
      
                  currentlyPlayingSong.setLooping(shouldLoop);
              }
          }
      
          public void StopFX()
          {
              if (currentlyPlayingFX != null)
              {
                  currentlyPlayingFX.stop();
      
                  currentlyPlayingFX.release();
      
                  currentlyPlayingFX = null;
              }
          }
      
          public void StopSong()
          {
              if (currentlyPlayingSong != null)
              {
                  currentlyPlayingSong.stop();
      
                  currentlyPlayingSong.release();
      
                  currentlyPlayingSong = null;
              }
          }
      }