Project address, for star
Https://github.com/979451341/AppAlive
I am learning the audio and video of the black technology, found in the use of audiotrack play music, using the phone's one-click Cleanup Discovery Program is still in, I mean the activity is still alive.
I thought about it. Cool Dog Music is also the case, when I did not play music, this cool dog music through my one-click Cleanup will die, if I play music and then a key cleanup will not die, and it also has a foreground notification to survive, but if you pause the music and then a key cleanup or will die.
I'll talk about my program.
I placed a PCM file in the raw file and then played this PCM
DataInputStream dis = new DataInputStream( new BufferedInputStream(getResources().openRawResource(R.raw.a))); // 实例AudioTrack AudioTrack track = new AudioTrack(AudioManager.STREAM_MUSIC, mFrequence, mPlayChannelConfig, mAudioEncoding, bufferSize, AudioTrack.MODE_STREAM); track.setStereoVolume(0,0); // 开始播放 track.play();
Which I also pass Track.setstereovolume (0,0); Turn the PCM file to 0, which is mute
There is the playback of this PCM is constantly circulating, and is in the background, so I used the asynctask to regulate the process, through the misplaying to control the playback of the stop
Class Playtask extends Asynctask<void,void,void> {@Override protected void doinbackground (void ... arg0) { Misplaying = true; for (; misplaying;) {int buffersize = Audiotrack.getminbuffersize (mfrequence, mplaychannelconf IG, maudioencoding); short[] buffer = new Short[buffersize]; try {//defines the input stream, writes audio to the Audiotrack class, implements playback DataInputStream dis = new DataInputStream ( New Bufferedinputstream (Getresources (). Openrawresource (R.RAW.A))); Example Audiotrack audiotrack track = new Audiotrack (Audiomanager.stream_music, Mfreque NCE, Mplaychannelconfig, maudioencoding, buffersize, Audiotrack.mode_stream) ; Track.setstereovolume (0,0); Start playing track.play (); Since Audiotrack plays a stream, we need to read while playing while (misplaying && dis.available () > 0) {int i = 0; while (dis.available () > 0 && i < buffer.length) {Buffer[i] = Dis.readshort (); i++; }//Then writes the data to the Audiotrack track.write (buffer, 0, buffer.length); }//Play End track.stop (); Dis.close (); } catch (Exception e) {//Todo:handle Exception LOG.E ("slack", "Error:" + e.getmessage ()); }} return null; } protected void OnPostExecute (void result) {} protected void OnPreExecute () {}}
For audiotrack occupy the resources can make the app in android7.0 mobile phone one button clean survived, this principle does not understand, ask the big God message taught in the next
And I experimented with the android7.0 of the Huawei, the program through a key cleanup can not die, but in the android7.2 of the Meizu's one-button cleanup or dead, sure enough. Custom memory management is a powerful
Android7.0 Mobile Phone program keepalive (with source download)