2017-05-07 112 views
0

我正在使用Android Mediaplayer播放片段中的某些音頻文件。但是,當我停止某個音頻文件並再次播放時,最後播放的某些聲音仍然存在 - 我停止播放的部分。這是爲什麼發生?Android Mediaplayer:音頻文件的某些(聲音)停止時

public class PageFragment_Bon extends Fragment implements View.OnClickListener{ 

    public static final String ARG_PAGE = "ARG_PAGE"; 

    private int mPage; 
    private Button start, stop, replay; 
    private MediaPlayer mediaPlayer; 
    int [] filer = new int[18]; 


    public static PageFragment_Bon newInstance(int page) { 
     Bundle args = new Bundle(); 
     args.putInt(ARG_PAGE, page); 
     PageFragment_Bon fragment = new PageFragment_Bon(); 
     fragment.setArguments(args); 
     return fragment; 
    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     mPage = getArguments().getInt(ARG_PAGE); 
      } 

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

     start = (Button) view.findViewById(R.id.start); 
     start.setOnClickListener(this); 

     stop = (Button) view.findViewById(R.id.stop); 
     stop.setOnClickListener(this); 

     replay = (Button) view.findViewById(R.id.replay); 
     replay.setOnClickListener(this); 

     filer[2] = R.raw.takbira; 
     filer[4] = R.raw.alfatiha; 
     filer[14] = R.raw.tashahhud; 
     filer[15] = R.raw.salat; 
     filer[16] = R.raw.assalam; 

     if(filer[mPage] != 0){ 
      start.setVisibility(View.VISIBLE); 
      stop.setVisibility(View.VISIBLE); 
      replay.setVisibility(View.VISIBLE); 
     } 

     return view; 
    } 

    @Override 
    public void onPause() { 
     super.onPause(); 

     if(mediaPlayer != null) 
     { 
      mediaPlayer.stop(); 
     } 

    } 

    @Override 
    public void onClick(View v) { 

     if(mediaPlayer == null) 
      mediaPlayer = MediaPlayer.create(getActivity().getBaseContext(), filer[mPage]);//add this line 

     if(v == start){ 
       try { 
        mediaPlayer.start(); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
     } 

     else if(v == stop){ 
      mediaPlayer.pause(); 
     } 

     else if(v == replay){ 
      mediaPlayer.seekTo(0); 
      mediaPlayer.start(); 
     } 
    } 


    @Override 
    public void setUserVisibleHint(boolean isVisibleToUser) { 
     super.setUserVisibleHint(isVisibleToUser); 

     if(!isVisibleToUser){ 

      if(mediaPlayer!=null) { 
       if (mediaPlayer.isPlaying()) { 
        try { 
         mediaPlayer.stop(); 
         mediaPlayer.prepare(); 
        } catch (Exception e) { 
         e.printStackTrace(); 
        } 
        } 
       } 
      } 

     } 
    } 

回答

0

你應該叫mediaPlayer.stop();

你這樣的代碼後使用mediaPlayer.reset()mediaPlayer.release()

mediaPlayer.stop(); 
mediaPlayer.reset(); 
mediaPlayer.release(); 
+0

在if(mediaPlayer.isPlaying())?原因不起作用。現在我不能再播放相同的聲音 – Hodhod

+0

然後你應該使用服務播放聲音。 – Saim

+0

如果我想在後臺播放聲音,不應該使用服務嗎?我不需要那個 – Hodhod