gpt4 book ai didi

android - 音频在Android中无法正常工作

转载 作者:行者123 更新时间:2023-12-03 02:13:36 27 4
gpt4 key购买 nike

我尝试根据用户选择从原始文件夹播放音频文件,但是如果用户选择或更改两个或多个文件,则音频无法正常工作,但是在更改选项后,它会流式传输选定文件以及先前选定的文件。我是android新手...我在下面发布了我的代码,任何人都可以帮我解决这个问题。

switch (Global.id)
{
case R.id.radioButton1:
mMediaPlayer = MediaPlayer.create(context, R.raw.kalimba);
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.setLooping(true);
mMediaPlayer.start();
break;

case R.id.radioButton2:
mMediaPlayer = MediaPlayer.create(context, R.raw.maid);
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.setLooping(true);
mMediaPlayer.start();
break;

case R.id.radioButton3:
mMediaPlayer = MediaPlayer.create(context, R.raw.sleep_away);
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.setLooping(true);
mMediaPlayer.start();
break;
}

这是 onDestroy 方法。
private void destroy()
{
mMediaPlayer.release();
mMediaPlayer.reset();
mMediaPlayer.stop();
}

最佳答案

试试这个

if ( mMediaPlayer.isPlaying() )  // Checking if already playing any song
{
mMediaPlayer.reset();
mMediaPlayer.stop();

}

在开关盒中添加以上条件
switch (Global.id)
{
case R.id.radioButton1:
mMediaPlayer = MediaPlayer.create(context, R.raw.kalimba);
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.setLooping(true);

if ( mMediaPlayer.isPlaying() ) // Checking if already playing any song
{
mMediaPlayer.reset();
mMediaPlayer.stop();
}
mMediaPlayer.start();
break;
case R.id.radioButton2:
mMediaPlayer = MediaPlayer.create(context, R.raw.maid);
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.setLooping(true);
if ( mMediaPlayer.isPlaying() ) // Checking if already playing any song
{
mMediaPlayer.reset();
mMediaPlayer.stop();
}
mMediaPlayer.start();
break;
case R.id.radioButton3:
mMediaPlayer = MediaPlayer.create(context, R.raw.sleep_away);
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.setLooping(true);
if ( mMediaPlayer.isPlaying() ) // Checking if already playing any song
{
mMediaPlayer.reset();
mMediaPlayer.stop();
}
mMediaPlayer.start();
break;
}

关于android - 音频在Android中无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22012430/

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