gpt4 book ai didi

java - 如何在android中方向改变时连续播放音频

转载 作者:行者123 更新时间:2023-12-01 18:36:46 24 4
gpt4 key购买 nike

我开发了像媒体播放器这样的应用程序,用于从源播放音频文件,但是当我将方向纵向更改为横向按钮时出现问题,其他小部件不起作用,而其他小部件则在我再次将方向横向更改为纵向时出现同样的问题问题按钮和搜索栏不起作用

我的代码在这里

public class Aarati_Activity extends Fragment
{
private ImageButton btnplay;
private SeekBar seekbar;
MediaPlayer mp;
private ImageButton btnstop;
private int finalTime = 0;
private int startTime = 0;
private int oneTimeOnly = 0;
Handler handler=new Handler();

public Aarati_Activity() {}

int audiocurrent;
int audiomax;
private int mp_progress;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.activity_aarti_fragment, container, false);

btnplay=(ImageButton)v.findViewById(R.id.btnplay);
btnstop=(ImageButton)v.findViewById(R.id.btnstop);
seekbar=(SeekBar)v.findViewById(R.id.seekbar);
seekbar.setClickable(true);

seekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

@Override
public void onStopTrackingTouch(SeekBar seekBar) {

seekchange();
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
}
});



mp = MediaPlayer.create(getActivity(), R.raw.arti);
audiomax=mp.getDuration();
audiocurrent=mp.getCurrentPosition();

btnstop.setOnClickListener(new OnClickListener() {


@Override
public void onClick(View v) {
if(mp.isPlaying())
{
mp.stop();
Toast.makeText(getActivity(), "Stop",Toast.LENGTH_LONG).show();
btnplay.setImageResource(R.drawable.ic_action_play);

try
{
mp.prepare();
} catch (IllegalStateException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();
}


}
else
{
Toast.makeText(getActivity(),"Aarti Currently not playing",Toast.LENGTH_SHORT).show();
}

}
});


btnplay.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {

if(mp.isPlaying())
{
mp.pause();
btnplay.setImageResource(R.drawable.ic_action_play);
Toast.makeText(getActivity(), "Pause",Toast.LENGTH_SHORT).show();
finalTime = mp.getDuration();
startTime = mp.getCurrentPosition();
if(oneTimeOnly == 0)

{
seekbar.setMax((int) finalTime);
oneTimeOnly = 1;
}
seekbar.setProgress((int)startTime);

handler.postDelayed(UpdateSongTime,100);

}
else
{ btnplay.setImageResource(R.drawable.ic_action_pause);
mp.start();
Toast.makeText(getActivity(), "Play",Toast.LENGTH_SHORT).show();
finalTime = mp.getDuration();
startTime = mp.getCurrentPosition();
if(oneTimeOnly == 0)

{
seekbar.setMax((int) finalTime);
oneTimeOnly = 1;
}
seekbar.setProgress((int)startTime);

handler.postDelayed(UpdateSongTime,100);
}
}
});

return v;

}
@SuppressLint("NewApi")
public void seekchange()
{
mp_progress=seekbar.getProgress();
mp.seekTo(mp_progress);
}

private Runnable UpdateSongTime = new Runnable()
{
public void run()
{
startTime = mp.getCurrentPosition();
seekbar.setProgress((int)startTime);
handler.postDelayed(this, 100);
}
};
}

最佳答案

您需要使用Service播放歌曲。即使应用程序在后台,它也可以让您播放歌曲 Tutorial

关于java - 如何在android中方向改变时连续播放音频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21622336/

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