我是一个新的android工作室,所以很抱歉,如果这是一个非常愚蠢的问题,但我不能找到答案在任何地方网上。非常感谢任何帮助!
我已经建立了一个小的应用程序,有两个屏幕:一个主屏幕,一个屏幕二。主屏幕只是导航到屏幕二。从另一个屏幕,你可以播放/暂停/停止一首特定的歌曲,或者回到主屏幕。
我遇到的问题是当我导航到屏幕二时,当我按下“后退”按钮返回主屏幕时,我无法弄清楚如何将“停止”方法添加到这个按钮上。我在网上学习了一些教程来设置mediacontroller,它工作得很好,但这里我卡住了。
所以,我所需要的只是将onStop()方法添加到back按钮,这样当用户导航离开mediaplayer屏幕时,播放就会停止。
这是我的代码:
主activity(Play,pause,stop,stopPlayer,onStop方法)
public void play(View v){
if(player == null){
player = MediaPlayer.create(this, R.raw.testmeditatie);
player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
stopPlayer();
}
});
}
player.start();
}
public void pause(View v){
if(player!= null){
player.pause();
}
}
public void stop(View v){
stopPlayer();
}
private void stopPlayer(){
if(player!=null){
player.release();
player = null;
Toast.makeText(this, "MediaPlayer released", Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onStop() {
super.onStop();
stopPlayer();
}
第一段代码:
public class FirstFragment extends Fragment {
@Override
public View onCreateView(
LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState
) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_first, container, false);
}
public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
super.onStop();
view.findViewById(R.id.button_first).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
NavHostFragment.findNavController(FirstFragment.this)
.navigate(R.id.action_FirstFragment_to_SecondFragment);
}
});
}
}
第二个片段:
public class SecondFragment extends Fragment {
@Override
public View onCreateView(
LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState
) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_second, container, false);
}
public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
view.findViewById(R.id.button_second).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
NavHostFragment.findNavController(SecondFragment.this)
.navigate(R.id.action_SecondFragment_to_FirstFragment);
}
});
}
第二个fragment.xml中的按钮
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Play"
android:onClick="play"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pause"
android:onClick="pause"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Stop"
android:onClick="stop"
/>
<Button
android:id="@+id/button_second"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/previous"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
只需重写onbackpressed()
并调用stopPlayer()来释放媒体资源。
@Override
public void onBackPressed() {
stopPlayer();
super.onBackPressed();
}