繁体   English   中英

即使编程为 MediaPlayer 也不会停止播放

[英]MediaPlayer won't stop playing even if programmed to

我做了一个简单的类来处理与声音相关的一切。 具有添加、播放、停止、释放和释放所有功能。 它的工作原理是您必须添加一首歌曲,然后通过您添加的歌曲的名称调用播放。 无论何时您需要停止,只需调用停止函数并将歌曲名称作为参数传递,它就会停止。 我的问题是它不会停止,即使它通过了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();
        }
    }
}

在活动的OnCreate我调用Add然后Play 一切都很好,直到我尝试从片段中调用Stop 运行没有任何错误或异常,它根本不会停止。

活动:

public class Main extends ActionBarActivity
{
   private Sound sound = new Sound();

   private static boolean isSoundOn = true;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        isSoundOn = true;

        sound.Add(R.raw.drajamainmenueddited, "mainMenuSong", this);

        //endregion

        //Hide upper action bar
        getSupportActionBar().hide();

            if (isSoundOn)
                sound.Play("mainMenuSong", true);
    }


    public void SetIsSoundOn(Boolean isOn)
    {
        isSoundOn = isOn;
    }

    public boolean GetIsSoundOn()
    {
       return isSoundOn;
    }

    public Sound GetSoundObj()
    {
        return sound;
    }
}

分段:

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);
        }
    }

}

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

到目前为止,这几乎是我编码的全部内容。

干杯。

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

MediaPlayer song = MediaPlayer.create(context, songId);

在停止():

MediaPlayer songToStop = songs.get(name)

注意

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

试试 songToStop.release() 代替

得停下来。 我的班级必须能够一次处理一首歌曲并同时处理许多 fx。 这就是我想出的。

声音:

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;
        }
    }
}

这是基于@The Original Android 的回答。 将其保留在单个实例上。

谢谢您的帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM