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

时间:2015-08-01 00:57:27

标签: java android android-fragments android-studio android-mediaplayer

我创建了一个简单的类来处理与声音相关的所有内容。有添加,播放,停止,释放和释放所有。它是如何工作的,你必须添加一首歌,然后通过你添加的歌曲的名称来调用播放。任何时候你需要停止,只需调用停止功能并将歌曲的名称作为参数传递,它应该停止。我的问题是它即使通过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);
        }
    }

}

我尝试做的是模拟静音按钮。点击后,所有声音都应静音。

到目前为止,这几乎都是我编码的。

干杯。

3 个答案:

答案 0 :(得分:1)

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

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

在Stop()中:

MediaPlayer songToStop = songs.get(name)

注意

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

答案 1 :(得分:0)

尝试使用songToStop.release()代替

答案 2 :(得分:0)

让它停下来。我的班级必须能够同时处理一首歌曲和许多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的回答。将它保存在单个实例上。

感谢您的帮助。