1 page.title=nh trong nh 2 page.keywords=preview,sdk,PIP,Picture-in-picture 3 page.tags=androidn 4 5 @jd:body 6 7 <div id="tb-wrapper"> 8 <div id="tb"> 9 10 <h2>Trong ti liu ny</h2> 11 <ol> 12 <li><a href="#declaring">Khai bo Hot ng h tr 13 nh trong nh</a></li> 14 <li><a href="#pip_button">Chuyn Hot ng sang nh trong nh</a> 15 </li> 16 <li><a href="#handling_ui">X l UI trong ch nh trong nh</a> 17 </li> 18 <li><a href="#continuing_playback">Tip tc pht li video ch 19 nh trong nh</a></li> 20 <li><a href="#best">Thc hnh Tt nht</a></li> 21 </ol> 22 23 <h2>Xem thm</h2> 24 <ol> 25 <li><a href="{@docRoot}preview/features/multi-window.html">H tr 26 a ca s</a></li> 27 </ol> 28 29 </div> 30 </div> 31 32 <p>Trong Android N, ngi dng Android TV c th xem video 33 trong mt ca s c ghim vo mt gc mn hnh khi iu hng bn trong 34 cc ng dng. Ch nh trong nh (PIP) cho php ng dng chy mt hot ng 35 video trong ca s c ghim trong khi mt hot ng khc tip tc chy 36 ngm. Ca s PIP cho php ngi dng thc hin nhiu vic khi ang s dng ng dng ca bn, 37 gip cho ngi dng nng sut hn.</p> 38 39 <p>ng dng ca bn c th quyt nh khi no th kch hot ch PIP. Sau y l mt vi v d v 40 thi im chuyn vo ch PIP:</p> 41 42 <ul> 43 <li>ng dng ca bn c th chuyn mt video vo ch PIP khi ngi dng iu hng 44 ri khi video duyt ni dung khc.</li> 45 <li>ng dng ca bn c th chuyn mt video sang ch PIP khi ngi dng xem on cui 46 ca mt tp ni dung. Mn hnh chnh s hin th thng tin qung co 47 hoc tm tt v tp tip theo trong lot video .</li> 48 <li>ng dng ca bn c th mang li cho ngi dng mt cch xp hng cc ni dung b sung khi 49 h ang xem mt video. Video s tip tc c pht trong ch PIP trong khi mn hnh 50 chnh hin th hot ng chn ni dung.</li> 51 </ul> 52 53 <p>Ca s PIP c kch thc 240x135 dp v c hin th trn lp trn cng mt trong 54 bn gc ca mn hnh c h thng chn. Ngi dng c th gi ra mt menu PIP 55 , cho php h bt tt ca s PIP ln ton mn hnh hoc ng ca s 56 PIP bng cch bm gi nt <b>Home</b> trn iu khin t xa. Nu mt video 57 khc bt u pht trn mn hnh chnh, ca s PIP s t ng 58 c ng. Ngi dng cng c th ng ca s PIP thng qua nt Recents.</p> 59 60 <img src="{@docRoot}images/android-7.0/pip-active.png" /> 61 <p class="img-caption"><strong>Hnh 1.</strong> Mt video nh trong nh 62 c hin th gc mn hnh khi ngi dng duyt ni dung 63 trn mn hnh chnh.</p> 64 65 <p>PIP s dng cc API a ca s c trong Android N 66 to ca s video nm chng c ghim. thm ch PIP vo ng dng bn cn 67 ng k cc hot ng h tr PIP, chuyn hot ng ca bn sang ch PIP khi 68 cn v m bo cho cc phn t UI s b n i v pht li video vn tip tc khi 69 hot ng trong ch PIP.</p> 70 71 <h2 id="declaring">Khai bo Hot ng h tr nh trong nh</h2> 72 73 <p>Theo mc nh, h thng khng t ng h tr PIP cho cc ng dng. 74 Nu bn mun h tr ch PIP trong ng dng ca mnh, hy ng k hot ng 75 video trong bn k khai bng cch t 76 <code>android:supportsPictureInPicture</code> v 77 <code>android:resizeableActivity</code> thnh <code>true</code>. Ngoi ra, hy ch nh 78 hot ng ca bn x l cc thay i cu hnh b tr hot ng 79 khng khi chy li khi c cc thay i v b tr trong qu trnh chuyn tip ch PIP.</p> 80 81 <pre> 82 <activity android:name="VideoActivity" 83 android:resizeableActivity="true" 84 android:supportsPictureInPicture="true" 85 android:configChanges= 86 "screenSize|smallestScreenSize|screenLayout|orientation" 87 ... 88 </pre> 89 90 <p>Khi ng k hot ng ca bn, hy nh rng trong ch PIP, hot ng 91 ca bn s c hin th trong mt ca s nh nm chng trn mn hnh TV. Cc hot ng 92 pht li video vi UI ti gin s em n tri nghim ngi dng tt nht. Cc hot ng 93 c cha nhng phn t UI nh c th khng em li tri nghim ngi dng t yu cu 94 khi c chuyn sang ch PIP bi ngi dng khng th thy cc chi tit ca phn t UI 95 trong ca s PIP.</p> 96 97 <h2 id="pip_button">Chuyn Hot ng sang nh trong nh</h2> 98 99 Khi bn cn chuyn hot ng ca mnh sang ch PIP, hy gi 100 <code>Activity.enterPictureInPicture()</code>. V d sau s chuyn 101 sang ch PIP khi ngi dng chn mt nt PIP ring trn thanh iu khin 102 media:</p> 103 104 <pre> 105 @Override 106 public void onActionClicked(Action action) { 107 if (action.getId() == R.id.lb_control_picture_in_picture) { 108 getActivity().enterPictureInPicture(); 109 return; 110 } 111 ... 112 </pre> 113 114 <p>Thm mt nt PIP vo thanh iu khin media ca bn s cho php ngi dng d dng chuyn 115 sang ch PIP khi iu khin pht li video.</p> 116 117 <img src="{@docRoot}images/android-7.0/pip-button.png" /> 118 <p class="img-caption"><strong>Hnh 1.</strong> Mt nt nh trong nh 119 trn thanh iu khin media.</p> 120 121 <p>Android N c mt lp 122 <code>PlaybackControlsRow.PictureInPictureAction</code> mi nh ngha 123 cc hnh ng ch PIP trn thanh iu khin v s dng biu tng PIP.</p> 124 125 <h2 id="handling_ui">X l UI trong ch nh trong nh</h2> 126 127 <p>Khi hot ng ca bn vo trong ch PIP th hot ng ch nn hin th pht li 128 video. Hy xa cc phn t UI trc khi hot ng ca bn vo ch PIP, 129 v khi phc cc phn t ny khi hot ng quay li ch ton mn hnh. 130 Ghi phng thc <code>Activity.onPictureInPictureChanged()</code> hoc 131 <code>Fragment.onPictureInPictureChanged()</code> v bt hoc 132 tt cc phn t UI khi cn thit, v d:</p> 133 134 <pre> 135 @Override 136 public void onPictureInPictureChanged(boolean inPictureInPicture) { 137 if (inPictureInPicture) { 138 // Hide the controls in picture-in-picture mode. 139 ... 140 } else { 141 // Restore the playback UI based on the playback status. 142 ... 143 } 144 } 145 </pre> 146 147 <h2 id="continuing_playback">Tip tc pht li video ch 148 nh trong nh</h2> 149 150 <p>Khi hot ng ca bn chuyn sang ch PIP th h thng s coi hot ng ang trong 151 trng thi tm dng v s gi phng thc <code>onPause()</code> ca hot ng. Vic pht li 152 video khng nn c tm dng v cn c tip tc pht nu hot ng 153 b tm dng do ch PIP. Hy kim tra ch PIP trong phng thc 154 <code>onPause()</code> ca hot ng v x l vic pht li cho ph hp, v 155 d:</p> 156 157 <pre> 158 @Override 159 public void onPause() { 160 // If called due to PIP, do not pause playback 161 if (inPictureInPicture()) { 162 // Continue playback 163 ... 164 } 165 // If paused but not in PIP, pause playback if necessary 166 ... 167 } 168 </pre> 169 170 <p>Khi hot ng ca bn chuyn ra khi ch PIP quay tr v ch ton mn hnh th 171 h thng s tip tc hot ng ca bn v gi phng thc <code>onResume()</code>.</p> 172 173 <h2 id="best">Thc hnh Tt nht</h2> 174 175 <p>PIP c dnh cho cc hot ng pht li video ton mn hnh. Khi chuyn 176 hot ng ca bn vo ch PIP, hy trnh hin th bt k ni dung no ngoi video. 177 Theo di khi hot ng ca bn vo ch PIP v n i cc phn t UI, nh m t 178 trong <a href="#handling_ui">X l UI trong ch nh trong nh</a>.</p> 179 180 <p>V ca s PIP c hin th di dng ca s ni gc 181 mn hnh do bn cn trnh hin th cc thng tin quan trng trong mn hnh chnh 182 bt k vng no c th b che khut bi ca s PIP.</p> 183 184 <p>Theo mc nh, khi mt hot ng ang trong ch PIP th n s khng nhn tiu im nhp vo. 185 tip nhn cc s kin nhp vo khi ch PIP, hy s dng 186 <code>MediaSession.setMediaButtonReceiver()</code>.</p> 187