如何在按下后退按钮时停止播放音频?

时间:2013-07-30 17:59:53

标签: android

大家好,我的代码是:

   final ImageView imageView = (ImageView) findViewById(R.id.Image7);
   imageView.setImageResource(mFullSizeIds[position]);
   imageView.setOnClickListener(new View.OnClickListener() {

@Override
         public void onClick(View v) {
    MediaPlayer mp = MediaPlayer.create(Audio.this,mAudio[position]);  
    mp.start();

}

  });

现在,当用户点击图像时,此代码可以正常播放音频,但是当我想退出此活动并转到其他活动时,音频轨道仍在播放,即使我按下主页键音频还在玩。我怎样才能防止这种情况发生? 当用户再次点击图像或按下后退/主页按钮时,有没有办法阻止播放曲目?

3 个答案:

答案 0 :(得分:3)

添加

   @Override
    public void onBackPressed ()
    {
      if (mp != null)
        mp.stop();
      super.onBackPressed();
    }

@Override
public void onPause ()
{
  if (mp != null)
  {
    mp.pause();
    mp.stop();
  }
  super.onPause();
}

答案 1 :(得分:1)

在活动中创建一个onStop并使mp成为一个类变量,尽管我总是为这类东西创建/更喜欢媒体播放器助手类。然后在onStop上调用mp.stop()/ pause。

private MediaPlayer mp = null;

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

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

然后你只需要在mp:

之前删除MediaPlayer来创建它
mp = MediaPlayer.create(Audio.this,mAudio[position]);

答案 2 :(得分:1)

覆盖onBackPressed方法,并停止MediaPlayer mp对象,public class TransactionsFragment extends Fragment { final String urlString = "https:/exampleurl.net/restapi/Handler.ashx"; String cnoKey = "com.example.app.saveCredentials.cno"; String aliasnameKey = "com.example.app.saveCredentials.aliasname"; SharedPreferences prefs; LayoutInflater inflaterGlobal; ViewGroup containerGlobal; List<String> recids; List<String> folders; List<String> whotos; List<String> directions; List<String> documents; List<String> thedates; View rootView; @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_transactions, container, false); inflaterGlobal = inflater; containerGlobal = container; TableLayout ll = (TableLayout) rootView.findViewById(R.id.tableLayout1); prefs = getActivity().getPreferences(Context.MODE_PRIVATE); String aliasname = prefs.getString(aliasnameKey, null); new TestAsync(aliasname, "", getActivity()).execute(); return rootView; } class TestAsync extends AsyncTask<Void, Integer, String> { private String aliasname; private String password; private String value; //success value private String recid; //success recid private String folder; private String whoto; private String direction; private String document; private String thedate; private ProgressDialog progressDialog; private Activity activity; public TestAsync(String aliasname, String password, Activity activity) { super(); this.password = password; this.aliasname = aliasname; this.activity = activity; } protected void onPreExecute() { System.out.println("PreExecute"); progressDialog = ProgressDialog.show(activity, "Loading", "Loading", true); } protected String doInBackground(Void... arg0) { try { JSONObject result = GetTranReport(aliasname, "False", "1", "03", "2015"); recids = new ArrayList<>(); folders = new ArrayList<>(); whotos = new ArrayList<>(); directions = new ArrayList<>(); documents = new ArrayList<>(); thedates = new ArrayList<>(); JSONObject jsonObject; JSONArray jsonArray = result.getJSONArray("Value"); for (int i = 0; i < jsonArray.length()-1; i++) { System.out.println("i = " + i); jsonObject = jsonArray.getJSONObject(i); System.out.println("recid = " + jsonObject.getString("recid")); recids.add(jsonObject.getString("recid")); folders.add(jsonObject.getString("folder")); whotos.add(jsonObject.getString("whoto")); directions.add(jsonObject.getString("direction")); documents.add(jsonObject.getString("document")); thedates.add(jsonObject.getString("thedate")); } } catch (Exception e) { e.printStackTrace(); } return "You are at PostExecute"; } protected void onProgressUpdate(Integer... a) { System.out.println("Progress Update" + a[0]); } protected void onPostExecute(String result) { //handleLogin(value); System.out.println("" + result); View rootView = inflaterGlobal.inflate(R.layout.fragment_transactions, containerGlobal, false); //getActivity().recreate(); TableLayout ll = (TableLayout) rootView.findViewById(R.id.tableLayout1); prefs = getActivity().getPreferences(Context.MODE_PRIVATE); String aliasname = prefs.getString(aliasnameKey, null); System.out.println("carrying out table creation"); if (whotos.size() > 0) { for (int i = 0; i < whotos.size(); i++) { System.out.println("" +i); TableRow row = new TableRow(getActivity()); TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT); row.setLayoutParams(lp); TextView tv = new TextView(getActivity()); tv.setText(whotos.get(i)); TextView tv2 = new TextView(getActivity()); tv2.setText(documents.get(i)); TextView tv3 = new TextView(getActivity()); tv3.setText(directions.get(i)); TextView tv4 = new TextView(getActivity()); tv4.setText(thedates.get(i)); row.addView(tv); //TP name row.addView(tv2); //document type row.addView(tv3); //direction row.addView(tv4); //date ll.addView(row, i+3); } ll.invalidate(); ll.refreshDrawableState(); } progressDialog.dismiss(); } }