gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 10:59:54 26 4
gpt4 key购买 nike

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

在 Activity 的 OnCreate 上,我调用 Add,然后调用 Play。在我尝试从 fragment 调用 Stop 之前,一切都很好。运行没有任何错误或异常,它不会停止。

Activity :

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

fragment :

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

在 Stop() 中:

MediaPlayer songToStop = songs.get(name)

注意:

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

关于java - 即使编程为 MediaPlayer 也不会停止播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31757183/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com