-2

I'm trying to follow the Core App Quality guideline FN-A1 that says that audio shouldn't play when the screen is off, so I checked it, and it does continue playing after I turn the screen off, but I'm not sure why. I don't think I'm using a service, and it seems that MediaPlayer should stop playing by default. Can anyone help me figure what I did wrong? I've copied the relevant parts of code. Thanks

public class RecordActivity extends BaseActivity {
MediaPlayer playback;

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_record);
        listView = (ListView)findViewById(R.id.listView);
        dir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + getString(R.string.app_name));

        listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
            String item = ((TextView) view).getText().toString();
            File itemFile = new File(dir + "/" + item + ".wav");
            if (itemFile.exists()) {
                playback = MediaPlayer.create(getApplicationContext(), Uri.fromFile(itemFile));
                try {
                    playback.start();
                } catch (NullPointerException e) {
                    e.printStackTrace();
                    Toast.makeText(getApplicationContext(), R.string.errorFileNull, Toast.LENGTH_SHORT).show();
                }
            }
            else {
                Toast.makeText(getApplicationContext(), R.string.errorNotRecorded, Toast.LENGTH_SHORT).show();
            }
           return true;
        }
    });
    }
    }
  • I'm glad that my media player still plays when the screen is off. I wouldn't like to drain my battery while listening to my favourite music. – Phantômaxx Oct 28 '15 at 08:22

2 Answers2

5

You should stop the MediaPlayer in onPause:

if (playback != null && playback.isPlaying()) {
        playback.pause();
    }

Resume it in onResume:

if (playback != null && !playback .isPlaying()) {
                playback.start();
    }
SuperFrog
  • 7,631
  • 9
  • 51
  • 81
  • Thank you, I am still learning android and I did not know about onPause or onResume. I will add this to my code! – SRutherford Oct 28 '15 at 01:55
  • Glad to help. Read about Android application LifeCycle, it will help you a lot. This is recommended: http://www.vogella.com/tutorials/AndroidLifeCycle/article.html – SuperFrog Oct 28 '15 at 01:57
0

Heads Up!

Notes About Media player

  1. MediaPlayer is state oriented, and handling its errors is not as easy as it seems. If you need this in a reliable environment you have to consider setting an OnErrorListener at least

Notes About the code itself

  1. You are catching a NullPointerException

// the better way is to check

if(playback == null){ 
    Toast.makeText(getApplicationContext(), R.string.errorFileNull, Toast.LENGTH_SHORT).show(); 
}
Community
  • 1
  • 1
hannunehg
  • 318
  • 2
  • 12
  • Thank you for the advice, I have set an OnErrorListener and updated my try/catch with an if else statement. It is working great so far! – SRutherford Oct 29 '15 at 20:38
  • Sure, anytime! One more important note is _Error(100,0)_ - this happens in cases of low memory and simultaneous creation/access to Audio service. You have to **release and recreate** the player when this error happens. – hannunehg Nov 01 '15 at 08:49