gpt4 book ai didi

android - fragment 中的多个音频

转载 作者:行者123 更新时间:2023-11-30 04:54:50 29 4
gpt4 key购买 nike

完全是菜鸟,看不懂这个问题。一个应用程序有一个 Activity ,两个 fragment ,每个 fragment 包含 15 行,每行有两个 TextView 和一个按钮(用于播放音频 fragment )。启动成功,我注意到,如果我不播放音频,它工作正常,我可以导航到任何地方,任何 fragment ,或者轻松地来回导航到 Activity ,但如果我播放音频,然后尝试移动到另一个 fragment 或 Activity ,应用程序崩溃。开始时启动的 Activity 仅带有两个 Button 以导航到 fragment 。这个应用程序有什么问题?我只添加了第一个 fragment 和一些行..请建议..

  package com.misty.multiaudioexperiments;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.content.Intent;
import android.media.MediaPlayer;
import android.widget.Button;



public class FragmentOne extends Fragment {
MediaPlayer mp = new MediaPlayer();

@Nullable
@Override
public View onCreateView(@NonNull final LayoutInflater inflater, @Nullable final ViewGroup container, @Nullable Bundle savedInstanceState) {
//return super.onCreateView(inflater, container, savedInstanceState);

View view = inflater.inflate(R.layout.fragment_fragment_one, container, false);




final Button buttonInFragmentOneToPlayAudioMenInEnglishVar = (Button)view.findViewById(R.id.buttonInFragmentOneToPlayAudioMenInEnglishId);
final Button buttonInFragmentOneToPlayAudioPencilInEnglishVar = (Button)view.findViewById(R.id.buttonInFragmentOneToPlayAudioPencilInEnglishId);
final Button buttonInFragmentOneToPlayAudioPilotInEnglishVar = (Button)view.findViewById(R.id.buttonInFragmentOneToPlayAudioPilotInEnglishId);
final Button buttonInFragmentOneToPlayAudioRunInEnglishVar = (Button)view.findViewById(R.id.buttonInFragmentOneToPlayAudioRunInEnglishId);
final Button buttonInFragmentOneToPlayAudioShoeInEnglishVar = (Button)view.findViewById(R.id.buttonInFragmentOneToPlayAudioShoeInEnglishId);



final Button buttonInFragmentOneForTransitionToContentsVar = (Button)view.findViewById(R.id.buttonInFragmentOneForTransitionToContentsId);
final Button buttonInFragmentOneForTransitionToNextFragmentVar = (Button)view.findViewById(R.id.buttonInFragmentOneForTransitionToNextFragmentId);





buttonInFragmentOneToPlayAudioMenInEnglishVar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {


if(mp != null) {
if(mp.isPlaying()){
mp.stop();
mp.release();
}
}

mp = MediaPlayer.create(getActivity().getApplicationContext(), R.raw.men);
mp.start();
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mediaPlayer) {
mp.release();
mp = null;




}
});



}
});


















buttonInFragmentOneToPlayAudioPencilInEnglishVar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {


if(mp != null) {
if(mp.isPlaying()){
mp.stop();
mp.release();
}
}

mp = MediaPlayer.create(getActivity().getApplicationContext(), R.raw.pencil);
mp.start();
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mediaPlayer) {
mp.release();
mp = null;




}
});



}
});
















buttonInFragmentOneToPlayAudioPilotInEnglishVar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {


if(mp != null) {
if(mp.isPlaying()){
mp.stop();
mp.release();
}
}

mp = MediaPlayer.create(getActivity().getApplicationContext(), R.raw.pilot);
mp.start();
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mediaPlayer) {
mp.release();
mp = null;




}
});



}
});















buttonInFragmentOneToPlayAudioRunInEnglishVar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {


if(mp != null) {
if(mp.isPlaying()){
mp.stop();
mp.release();
}
}

mp = MediaPlayer.create(getActivity().getApplicationContext(), R.raw.run);
mp.start();
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mediaPlayer) {
mp.release();
mp = null;




}
});



}
});



















buttonInFragmentOneToPlayAudioShoeInEnglishVar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {


if(mp != null) {
if(mp.isPlaying()){
mp.stop();
mp.release();
}
}

mp = MediaPlayer.create(getActivity().getApplicationContext(), R.raw.shoe);
mp.start();
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mediaPlayer) {
mp.release();
mp = null;




}
});



}
});

















buttonInFragmentOneForTransitionToContentsVar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

Intent intent1 = new Intent(getActivity(), MainActivity.class);
startActivity(intent1);
mp.reset();
mp.release();
mp = null;

}
});




//THIS BUTTON TAKES TO THE NEXT OR FRAGMENT TWO PAGE STOPPING MEDIAPLAYER IF PLAYING

buttonInFragmentOneForTransitionToNextFragmentVar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.fragmentContainer, new FragmentTwo());
ft.commit();
mp.reset();
mp.release();
mp = null;



}
});



return view;
}
}

最佳答案

您收到异常是因为当您尝试退出 fragment 时 mp 实例为空。

  1. 在创建 fragment 的新实例时实例化 mp 引用
public class FragmentOne extends Fragment {
MediaPlayer mp = new MediaPlayer();
  1. 每次播放音频时 mp 引用都会失效

mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mediaPlayer) {
mp.release();
mp = null;
}
});

案例 1:

如果您离开 fragment 而不播放任何音频,则实例不会被更改,因此您不会得到任何异常。

案例 2:

如果您在播放一些音频后离开 fragment ,mp 实例将设置为 null,因此当您尝试在 null 实例上调用 mp.reset() 时,会导致异常。

解决方案

用 if block 包围退出检查:-

if(mp != null){
mp.reset();
mp.release();
mp = null;
}

作为最佳实践,在两个 fragment 的 onStop() 方法中执行此检查,并将其从 onClick()

中删除
    @Override
public void onStop() {
if(mp != null){
mp.reset();
mp.release();
mp = null;
}
super.onStop();
}

希望对你有帮助

关于android - fragment 中的多个音频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59441984/

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