我是安卓工作室的新手,如果这是一个很难回答的问题,我很抱歉,但我在网上找不到答案。非常感谢您的帮助!
我制作了一个有两个屏幕的小应用程序:一个主屏幕,一个第二屏幕。主屏幕仅用于导航至第二屏幕。从另一个屏幕,您可以播放/暂停/停止特定歌曲,或返回主屏幕。
我遇到的问题是当我导航到屏幕二时;背面";按钮返回主屏幕,我不知道如何添加";停止";方法。我已经在网上学习了一些教程来设置媒体控制器,它运行得很好,但现在我陷入了困境。
因此,我只需要将onStop((方法添加到后退按钮中,这样当用户离开媒体播放器屏幕时,播放就会停止。
这是我的代码:
主要活动(播放、暂停、停止、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);
}
});
}
第二个片段.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();
}
更新:
由于您使用的是导航组件,所以onBackPressed()
不会从第二个片段中调用,除非您显式调用它。
要做到这一点,您可以创建一个由第二个片段实现的侦听器接口:
public interface IOnBackPressed {
void onBackPressed();
}
然后在第二个片段中实现:
public class SecondFragment extends Fragment implements IOnBackPressed {
@Override
public void onBackPressed() {
stopPlayer();
}
// rest of your code
}
然后,每当按下后退按钮时,您需要显式调用第二个片段的onBackPressed()
;但这在CCD_ 5中的CCD_ 4中被触发如下:
public class MainActivity extends AppCompatActivity {
@Override
public void onBackPressed() {
NavHostFragment navHostFragment = (NavHostFragment) getSupportFragmentManager().getPrimaryNavigationFragment();
if (navHostFragment != null) {
// Getting the current fragment in the navGraph
Fragment currentFragment = navHostFragment.getChildFragmentManager()
.getFragments().get(0);
/* Check if the Current fragment implements IOnBackPressed so it
needs to consume the back before the MainActivity */
if (currentFragment instanceof IOnBackPressed)
((IOnBackPressed) currentFragment).onBackPressed();
}
super.onBackPressed();
}
// rest of your code
}
所以,我只需要将onStop((方法添加到后退按钮中,这样当用户离开媒体播放器屏幕时,播放就会停止。
好吧,这是了解Android生命周期的好时机。简而言之,当当前屏幕变得不可见时(就像你把应用程序放在后台一样(,就会调用一个Lifecycle方法onStop()
,因此我们可以在那里做一些事情。
-
将所有与
MediaPlayer
相关的代码移动到SecondFragment
中。 -
在
onStop()
:中停止您的MediaPlayer
override fun onStop() { super.onStop() stopPlayer() }
演示:https://youtu.be/XfadYTxKK3s
生命周期教程:https://youtu.be/UJN3AL4tiqw