简体   繁体   English

Android MediaPlayer 在屏幕关闭后继续播放

[英]Android MediaPlayer continues playing after screen is turned off

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.我正在尝试遵循核心应用程序质量指南 FN-A1,该指南表示在屏幕关闭时不应播放音频,所以我检查了它,并且在我关闭屏幕后它确实继续播放,但我不是肯定为什么。 I don't think I'm using a service, and it seems that MediaPlayer should stop playing by default.我不认为我在使用服务,而且 MediaPlayer 似乎应该默认停止播放。 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;
        }
    });
    }
    }

You should stop the MediaPlayer in onPause:您应该在 onPause 中停止 MediaPlayer:

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

Resume it in onResume:在 onResume 中恢复它:

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

Heads Up!当心!

Notes About Media player关于媒体播放器的注意事项

  1. MediaPlayer is state oriented, and handling its errors is not as easy as it seems. MediaPlayer 是面向状态的,处理它的错误并不像看起来那么容易。 If you need this in a reliable environment you have to consider setting an OnErrorListener at least如果你在可靠的环境中需要这个,你必须考虑至少设置一个 OnErrorListener

Notes About the code itself关于代码本身的注释

  1. You are catching a NullPointerException您正在捕获 NullPointerException

// the better way is to check // 更好的方法是检查

if(playback == null){ 
    Toast.makeText(getApplicationContext(), R.string.errorFileNull, Toast.LENGTH_SHORT).show(); 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM