VideoAndLivePlayerView.java 82.4 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814

package com.wd.player.widget;

import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.content.res.TypedArray;
import android.net.Uri;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.aliyun.player.AliPlayer;
import com.aliyun.player.AliPlayerFactory;
import com.aliyun.player.IPlayer;
import com.aliyun.player.bean.ErrorInfo;
import com.aliyun.player.bean.InfoBean;
import com.aliyun.player.bean.InfoCode;
import com.aliyun.player.nativeclass.CacheConfig;
import com.aliyun.player.nativeclass.MediaInfo;
import com.aliyun.player.nativeclass.PlayerConfig;
import com.aliyun.player.nativeclass.TrackInfo;
import com.aliyun.player.source.LiveSts;
import com.aliyun.player.source.StsInfo;
import com.aliyun.player.source.UrlSource;
import com.aliyun.player.source.VidAuth;
import com.aliyun.player.source.VidMps;
import com.aliyun.player.source.VidSts;
import com.aliyun.subtitle.LocationStyle;
import com.aliyun.subtitle.SubtitleView;
import com.aliyun.svideo.wd.player.R;
import com.cicada.player.utils.ass.AssHeader;
import com.cicada.player.utils.ass.AssSubtitleView;
import com.wd.base.log.Logger;
import com.wd.foundation.wdkit.constant.EventConstants;
import com.wd.capability.network.utils.NetworkUtil;
import com.wd.foundation.wdkit.constant.Constants;
import com.wd.foundation.wdkit.imageglide.ImageUtils;
import com.wd.foundation.wdkit.utils.NetWatchdogUtils;
import com.wd.foundation.wdkit.utils.NumberUtil;
import com.wd.foundation.wdkit.utils.ScreenUtils;
import com.wd.foundation.wdkit.utils.ToastNightUtil;
import com.wd.foundation.wdkit.view.RenderViewOutlineProvider;
import com.wd.foundation.wdkit.viewclick.BaseClickListener;
import com.wd.foundation.wdkitcore.livedata.LiveDataBus;
import com.wd.player.constant.GlobalPlayerConfig;
import com.wd.player.control.ControlView;
import com.wd.player.durationview.VideoDurationView;
import com.wd.player.function.MarqueeView;
import com.wd.player.gesture.GestureView;
import com.wd.player.gesture.NetChangeLayoutView;
import com.wd.player.gesturedialog.OrientationWatchDog;
import com.wd.player.interfaces.ViewAction;
import com.wd.player.listener.OnBackClickListener;
import com.wd.player.listener.OnOrientationChangeListener;
import com.wd.player.listener.OnPlayStateBtnClickListener;
import com.wd.player.listener.OnScreenModeClickListener;
import com.wd.player.listener.OnSeekListener;
import com.wd.player.listener.OnSeekStartListener;
import com.wd.player.listener.OnShareClickListener;
import com.wd.player.listener.OnStoppedListener;
import com.wd.player.listener.OnTipClickListener;
import com.wd.player.listener.SimpleGestureListener;
import com.wd.player.listener.SimpleNetChangeListener;
import com.wd.player.listener.VideoPlayerCompletionListener;
import com.wd.player.listener.VideoPlayerErrorListener;
import com.wd.player.listener.VideoPlayerInfoListener;
import com.wd.player.listener.VideoPlayerLoadingStatusListener;
import com.wd.player.listener.VideoPlayerOnSeekCompleteListener;
import com.wd.player.listener.VideoPlayerPreparedListener;
import com.wd.player.listener.VideoPlayerRenderingStartListener;
import com.wd.player.listener.VideoPlayerStateChangedListener;
import com.wd.player.playerutil.NetWatchdog;
import com.wd.player.playerutil.VodPlayerLoadEndHandler;
import com.wd.player.tipsview.TipsView;

import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.Map;

/**
 * 带控制View的视频/直播 播放View
 * UI播放器的主要实现类。 通过ITheme控制各个界面的主题色。 通过各种view的组合实现UI的界面。这些view包括: 用户手势操作的{@link GestureView} 控制播放,显示信息的{@link
 * ControlView} 用户提示页面{@link TipsView}
 * 以及封面等。 view 的初始化是在{@link #initVideoView}方法中实现的。 然后是对各个view添加监听方法,处理对应的操作,从而实现与播放器的共同操作
 */
public class VideoAndLivePlayerView extends RelativeLayout {


    private static final String TAG = VideoAndLivePlayerView.class.getSimpleName();
    /**
     * 跑马灯显示区域
     */
    private static final MarqueeView.MarqueeRegion MARQUEE_REGION = MarqueeView.MarqueeRegion.TOP;

    // 解决bug,进入播放界面快速切换到其他界面,播放器仍然播放视频问题
    private final VodPlayerLoadEndHandler vodPlayerLoadEndHandler = new VodPlayerLoadEndHandler(this);
    /**
     * 普通视频
     */
    public final static int NORMAL = 0;

    /**
     * 回放视频
     */
    public final static int REVIEW = 1;

    /**
     * 预告视频
     */
    public final static int PREVIEW = 2;

    /**
     * 直播
     */
    public final static int LIVE = 3;

    /**
     * 图文详情页
     */
    public final static int DETAIL = 4;


    // 是否是在投屏中
    private final boolean mIsScreenCosting = false;
    /**
     * 网络看门狗
     */
    private NetWatchdogUtils mNetWatchdogUtils;

    /**
     * 判断VodePlayer 是否加载完成
     */
    private final Map<MediaInfo, Boolean> hasLoadEnd = new HashMap<>();

    // 手势操作view
    private GestureView mGestureView;

    // 皮肤view
    private ControlView mControlView;

    // 网路环境view
    public NetChangeLayoutView mNetChangeLayoutView;

    // 封面view
    private ImageView mCoverView;

    // 视频时长视图
    private VideoDurationView mVideoDurationView;

    // 网络状态监听
    private NetWatchdog mNetWatchdog;

    // 屏幕方向监听
    private OrientationWatchDog mOrientationWatchDog;

    // 包括错误提示、重试提示、直播结束提示、直播垫片等视图
    private TipsView mTipsView;

    // 是否锁定全屏
    private boolean mIsFullScreenLocked = false;

    // 当前屏幕模式
    public AliyunScreenMode mCurrentScreenMode = AliyunScreenMode.Small;

    // 是不是在seek中
    private boolean inSeek = false;

    // 播放是否完成
    private boolean isCompleted = false;

    // 媒体信息
    private MediaInfo mAliyunMediaInfo;

    // 跑马灯
    private MarqueeView mMarqueeView;

    // 原视频的buffered
    private long mVideoBufferedPosition = 0;

    // 原视频的currentPosition
    private long mCurrentPosition = 0;

    // 当前播放器的状态
    public int mPlayerState = IPlayer.idle;

    // 原视频时长
    private long mSourceDuration;

    // 播放的数据类型 0普通视频 1回放 2预告 3直播
    private int playResourceType;

    // 字幕
    private SubtitleView mSubtitleView;

    // 目前支持的几种播放方式
    private VidAuth mAliyunPlayAuth;

    private VidMps mAliyunVidMps;

    private UrlSource mAliyunLocalSource;

    private VidSts mAliyunVidSts;

    private LiveSts mAliyunLiveSts;

    private final IPlayer.OnTrackReadyListener mOutOnTrackReadyListener = null;

    private IPlayer.OnSeiDataListener mOutOnSeiDataListener = null;

    private AliPlayer.OnVerifyTimeExpireCallback mOutOnVerifyTimeExpireCallback = null;

    private OnTipClickListener mOutOnTipClickListener = null;

    private OnSeekStartListener onSeekStartListener;

    private OnOrientationChangeListener orientationChangeListener;

    // 播放按钮点击监听
    private OnPlayStateBtnClickListener onPlayStateBtnClickListener;

    //分享点击监听
    private OnShareClickListener mOnShareClickListener;

    // 停止按钮监听
    private OnStoppedListener mOnStoppedListener;

    // 连网断网监听
    private NetConnectedListener mNetConnectedListener = null;

    // 广告播放器的当前状态
    private int mAdvVideoPlayerState;

    // 运营商是否自动播放 默认自动播放
    private boolean mIsOperatorPlay = true;

    private final Map<Integer, AssHeader.SubtitleType> mSubtitleTypeMap = new HashMap<>();

    // 是否只需要全屏播放
    private boolean mIsNeedOnlyFullScreen;

    // 播放器+渲染的View
    private AliyunRenderView mAliyunRenderView;

    private AssSubtitleView mAssSubtitleView;

    private static final int FULLSCREEN_FLAGS = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;

    // 通过添加和移除这个view来实现隐藏和显示系统navigation bar,可以避免出现一些奇奇怪怪的问题
    private View mHideNavBarView;

    // 当前网速
    private final String currentNetSpeed = "0KB/s";

    private int activity_landscape;

    /**
     * 是否展示ReplayView
     */
    private boolean showReplayView = false;

    public VideoAndLivePlayerView(Context context) {
        this(context, null, 0);
    }

    public VideoAndLivePlayerView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public VideoAndLivePlayerView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.VideoAndLivePlayerView);
        playResourceType = typedArray.getInt(R.styleable.VideoAndLivePlayerView_playResourceType, NORMAL);
        typedArray.recycle();
        initVideoView();
    }

    public AliyunRenderView getAliyunRenderView() {
        return mAliyunRenderView;
    }

    /**
     * 初始化view
     */
    private void initVideoView() {

        mHideNavBarView = new View(getContext());
        mHideNavBarView.setSystemUiVisibility(FULLSCREEN_FLAGS);

        // 初始化播放器
        initAliVcPlayer();
        // 初始化手势view
        initGestureView();
        // 初始化跑马灯
        initMarquee();
        // 初始化提示view
        initTipsView();
        // 初始化控制栏
        initControlView();
        // 初始化封面
        initCoverView();
        // 初始化右下角时长角标
        initDurationView();
        // 初始化网络监听器
        initNetWatchdog();
        // 初始化网络异常视图
        iniNetChangeLayoutView();
        // 先隐藏手势和控制栏,防止在没有prepare的时候做操作。
        hideGestureAndControlViews();
        // 初始化字幕
        initSubtitleView();
    }

    /**
     * 在activity调用onResume的时候调用。 解决home回来后,画面方向不对的问题
     */
    public void onResume() {

        if (mIsFullScreenLocked) {
            int orientation = getResources().getConfiguration().orientation;
            if (orientation == Configuration.ORIENTATION_PORTRAIT) {
                changeScreenMode(AliyunScreenMode.Small, false);
            } else if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
                changeScreenMode(AliyunScreenMode.Full, false);
            }
        }

        startOrientationWatchDog();

        // onStop中记录下来的状态,在这里恢复使用
        if (!mIsScreenCosting) {
            // 不是投屏中再回复播放
            resumePlayerState();
        }
    }

    /**
     * 在activity调用onDestroy的时候调用,活动销毁释放
     */
    public void onDestroy() {

        stop();
        if (mAliyunRenderView != null) {
            mAliyunRenderView.release();
            mAliyunRenderView = null;
        }
        mGestureView = null;
        mControlView = null;
        mCoverView = null;
        mTipsView = null;
        mAliyunMediaInfo = null;
        if (mOrientationWatchDog != null) {
            mOrientationWatchDog.destroy();
        }
        mOrientationWatchDog = null;
        if (hasLoadEnd != null) {
            hasLoadEnd.clear();
        }
        if (mNetWatchdogUtils != null) {
            mNetWatchdogUtils.stopWatch();
            mNetWatchdogUtils.clearContent();
            mNetWatchdogUtils.setNetChangeListener(null);
        }
        if (mNetWatchdog != null) {
            mNetWatchdog.stopWatch();
            mNetWatchdog.clearContent();
            mNetWatchdog.setNetChangeListener(null);
            mNetWatchdog.setNetConnectedListener(null);
        }
        if (mMarqueeView != null) {
            mMarqueeView.clearContent();
            mMarqueeView = null;
        }
        if (vodPlayerLoadEndHandler != null) {
            vodPlayerLoadEndHandler.removeCallbacksAndMessages(null);
        }

    }

    /**
     * 初始化播放器
     */
    private void initAliVcPlayer() {

        mAliyunRenderView = new AliyunRenderView(getContext());

        addSubView(mAliyunRenderView);
        mAliyunRenderView.setSurfaceType(AliyunRenderView.SurfaceType.SURFACE_VIEW);

        // 设置准备回调
        mAliyunRenderView.setOnPreparedListener(new VideoPlayerPreparedListener(this));
        // mAliyunRenderView.setOnTrackChangedListener();
        // 播放器出错监听
        mAliyunRenderView.setOnErrorListener(new VideoPlayerErrorListener(this));
        // 播放器加载回调
        mAliyunRenderView.setOnLoadingStatusListener(new VideoPlayerLoadingStatusListener(this));
        mAliyunRenderView.setOnTrackReadyListenenr(new VideoPlayerOnTrackReadyListener(this));
        // 播放器状态
        mAliyunRenderView.setOnStateChangedListener(new VideoPlayerStateChangedListener(this));
        // 播放结束
        mAliyunRenderView.setOnCompletionListener(new VideoPlayerCompletionListener(this));
        // 播放信息监听
        mAliyunRenderView.setOnInfoListener(new VideoPlayerInfoListener(this));
        // 第一帧显示
        mAliyunRenderView.setOnRenderingStartListener(new VideoPlayerRenderingStartListener(this));
        // trackChange监听
        mAliyunRenderView.setOnTrackChangedListener(new VideoPlayerTrackChangedListener(this));
        // 字幕显示和隐藏
        mAliyunRenderView.setOnSubtitleDisplayListener(new VideoPlayerSubtitleDeisplayListener(this));
        // seek结束事件
        mAliyunRenderView.setOnSeekCompleteListener(new VideoPlayerOnSeekCompleteListener(this));
        // sei监听事件
        mAliyunRenderView.setOnSeiDataListener(new VideoPlayerOnSeiDataListener(this));
        mAliyunRenderView.setOnVerifyTimeExpireCallback(new VideoPlayerOnVerifyStsCallback(this));
    }

    /**
     * 初始化手势view
     */
    private void initGestureView() {

        mGestureView = new GestureView(getContext());
        addSubView(mGestureView);

        // 设置手势监听
        mGestureView.setOnGestureListener(simpleGestureListener);
    }

    /**
     * 手势监听简化版
     */
    SimpleGestureListener simpleGestureListener = new SimpleGestureListener() {

        // 单机
        @Override
        public void onSingleTap() {

            if (mNetChangeLayoutView.isShow()) {
                return;
            }

            // 单击事件,显示控制栏
            if (mControlView != null) {
                if (!mControlView.getControlBarIsShow()) {
                    mControlView.show();
                } else {
                    mControlView.hide(ControlView.HideType.Normal);
                }
            }

            // 处于小窗
            if (isSpecialPlayState){
                // 切换播放状态
                switchPlayerState();
            }

        }

        // 单机
        @Override
        public void onDoubleTap() {

            if (mNetChangeLayoutView.isShow()) {
                return;
            }

            if (playResourceType == LIVE || playResourceType == REVIEW) {
                return;
            }

            // 双击事件,控制暂停播放
            if (mIsScreenCosting || (GlobalPlayerConfig.IS_TRAILER)) {
                // 投屏状态下或者试看结束时双击不做任何操作
            } else if (GlobalPlayerConfig.IS_TRAILER && mAdvVideoPlayerState == IPlayer.started) {
                // 如果是视频广告,并且视频广告在播放中,那么不做任何操作
            } else {
                // 切换播放状态
                switchPlayerState();
            }
        }

        @Override
        public void onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
            super.onScroll(e1, e2, distanceX, distanceY);
        }
    };

    /**
     * 初始化跑马灯控件
     */
    private void initMarquee() {
        mMarqueeView = new MarqueeView(getContext());
        addSubViewHeightWrap(mMarqueeView);
    }

    /**
     * 添加子View到布局中,高度设置为 WRAP_CONTENT
     *
     * @param view 子view
     */
    private void addSubViewHeightWrap(View view) {

        LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        switch (MARQUEE_REGION) {
            case TOP:
                params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
                break;
            case MIDDLE:
                params.addRule(RelativeLayout.CENTER_VERTICAL);
                break;
            case BOTTOM:
                params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
                break;
            default:
                params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
                break;
        }
        // 添加到布局中
        addView(view, params);
    }

    /**
     * 初始化提示view,包括错误提示、重试提示、直播结束提示、直播垫片等视图
     */
    private void initTipsView() {

        // 包括错误提示、重试提示、直播结束提示、直播垫片等视图
        mTipsView = new TipsView(getContext());
        addSubView(mTipsView);

        // 设置tip中的点击监听事件
        mTipsView.setOnTipClickListener(onTipClickListener);
    }

    /**
     * 提示view点击回调,包括错误提示、重试提示、直播结束提示、直播垫片等视图
     */
    OnTipClickListener onTipClickListener = new OnTipClickListener() {

        @Override
        public void onContinuePlay() {

            mIsOperatorPlay = true;
            // 继续播放。如果没有prepare或者stop了,需要重新prepare
            mTipsView.hideAll();
            if (GlobalPlayerConfig.IS_VIDEO) {
                if (mAliyunRenderView != null) {
                    mAliyunRenderView.start();
                }
                if (mControlView != null) {
                    mControlView.setHideType(ViewAction.HideType.Normal);
                }
                if (mGestureView != null) {
                    mGestureView.setVisibility(VISIBLE);
                    mGestureView.setHideType(ViewAction.HideType.Normal);
                }
                if (mNetChangeLayoutView != null) {
                    mNetChangeLayoutView.hide(ViewAction.HideType.Normal);
                }
            } else {
                if (mPlayerState == IPlayer.idle || mPlayerState == IPlayer.stopped || mPlayerState == IPlayer.error
                        || mPlayerState == IPlayer.completion) {
                    mAliyunRenderView.setAutoPlay(true);
                    if (mAliyunPlayAuth != null) {
                        prepareAuth(mAliyunPlayAuth);
                    } else if (mAliyunVidSts != null) {
                        prepareVidsts(mAliyunVidSts);
                    } else if (mAliyunVidMps != null) {
                        prepareMps(mAliyunVidMps);
                    } else if (mAliyunLocalSource != null) {
                        prepareLocalSource(mAliyunLocalSource);
                    } else if (mAliyunLiveSts != null) {
                        prepareLiveSts(mAliyunLiveSts);
                    }
                } else {
                    start();
                }
            }
        }

        @Override
        public void onStopPlay() {

            // 结束播放
            mTipsView.hideAll();
            stop();

            Context context = getContext();
            if (context instanceof Activity) {
                ((Activity) context).finish();
            }
        }

        @Override
        public void onRetryPlay(int errorCode) {

            if (mOutOnTipClickListener != null) {
                mOutOnTipClickListener.onRetryPlay(errorCode);
            }
            reTry();
        }

        @Override
        public void onReplay() {
            // 重播
            rePlay();
        }

        @Override
        public void onRefreshSts() {
        }

        @Override
        public void onWait() {
            mTipsView.hideAll();
            mTipsView.showNetLoadingTipView();
        }

        @Override
        public void onExit() {
            mTipsView.hideAll();
            if (mOutOnTipClickListener != null) {
                mOutOnTipClickListener.onExit();
            }
        }
    };

    /**
     * 初始化控制栏view
     */
    private void initControlView() {

        mControlView = new ControlView(getContext(), playResourceType);
        addSubView(mControlView);

        // 设置播放按钮点击
        mControlView.setOnPlayStateClickListener(new ControlView.OnPlayStateClickListener() {
            @Override
            public void onPlayStateClick() {

                if (mNetChangeLayoutView.isShow()) {
                    return;
                }

                // 切换播放状态。点播播放按钮之后的操作
                switchPlayerState();
            }
        });

        // 设置进度条的seek监听
        mControlView.setOnSeekListener(new OnSeekListener() {
            @Override
            public void onSeekEnd(int position) {
                if (mNetChangeLayoutView.isShow()) {
                    return;
                }
                if (mControlView != null) {
                    mControlView.setVideoPosition(position);
                }
                if (isCompleted) {
                    // 播放完成了,不能seek了
                    inSeek = false;
                } else {

                    // 拖动结束后,开始seek
                    seekTo(position);
                }
            }

            @Override
            public void onSeekStart(int position) {
                if (mNetChangeLayoutView.isShow()) {
                    return;
                }
                // 拖动开始
                inSeek = true;
            }

            @Override
            public void onProgressChanged(int progress) {

            }
        });

        // 分享点击
        mControlView.setOnShareClickListener(new OnShareClickListener() {
            @Override
            public void onShareClick() {
                if (mNetChangeLayoutView.isShow()) {
                    return;
                }
                if (mOnShareClickListener != null) {
                    mOnShareClickListener.onShareClick();
                }
            }
        });

        // 点击全屏/小屏按钮
        mControlView.setOnScreenModeClickListener(new OnScreenModeClickListener() {
            @Override
            public void onClick() {
                if (mNetChangeLayoutView.isShow()) {
                    return;
                }
                if (mCurrentScreenMode == AliyunScreenMode.Small) {
                    changedToLandForwardScape(true);
                } else if (mCurrentScreenMode == AliyunScreenMode.Full) {
                    changedToPortrait(true);
                }
            }
        });

        // 点击了标题栏的返回按钮
        mControlView.setOnBackClickListener(new OnBackClickListener() {
            @Override
            public void onClick() {
                backClick();
            }
        });

    }

    /**
     * 初始化封面
     */
    private void initCoverView() {

        // 创建一个封面视图添加到布局中
        mCoverView = new ImageView(getContext());
        mCoverView.setId(R.id.custom_id_min);
        mCoverView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        mCoverView.setAdjustViewBounds(true);
        addSubView(mCoverView);

        // 封面点击事件
        mCoverView.setOnClickListener(new BaseClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {

                if (onPlayStateBtnClickListener != null) {
                    onPlayStateBtnClickListener.onPlayBtnClick(IPlayer.idle);
                }

                // 开始播放
                start();
                // 设置是否显示控制栏
                setControlBarCanShow(true);

                // 隐藏封面和视频时长视图
                if (mCoverView.getVisibility() == View.VISIBLE) {
                    mCoverView.setVisibility(View.GONE);
                    mVideoDurationView.setVisibility(GONE);
                }
            }
        });
    }

    /**
     * 视频时长视图
     */
    private void initDurationView() {

        mVideoDurationView = new VideoDurationView(getContext());
        addSubView(mVideoDurationView);
        mVideoDurationView.setVisibility(GONE);
    }

    /**
     * 初始化网络监听
     */
    private void initNetWatchdog() {

        Context context = getContext();

        mNetWatchdog = new NetWatchdog(context);
        mNetWatchdog.setNetConnectedListener(new MyNetConnectedListener(this));

        if (mNetWatchdogUtils == null) {
            //添加网络监听
            mNetWatchdogUtils = new NetWatchdogUtils(getContext());
            mNetWatchdogUtils.setNetChangeListener(simpleNetChangeListener);
            mNetWatchdogUtils.startWatch();
        }
    }

    /**
     * 网络回调监听
     */
    SimpleNetChangeListener simpleNetChangeListener = new SimpleNetChangeListener() {

        @Override
        public void onWifiTo4G() {

            //播放的数据类型 0普通视频 1回放 2预告 3直播
            changeNetWorkView();
            if (playResourceType == LIVE && Constants.Video_Playback == 1 && !Constants.isShowNetTips) {
                double ratio = NumberUtil.divNum(16, 9);
                int playViewHeight = (int) NumberUtil.divNum(ScreenUtils.getRealWidth(), ratio);
                ToastNightUtil.showTopShort("这是一个非Wi-Fi环境。请注意流量消耗", playViewHeight / 2);
                Constants.isShowNetTips = true;
            }
        }
    };

    /**
     * 初始化直播网络失败
     */
    private void iniNetChangeLayoutView() {

        mNetChangeLayoutView = new NetChangeLayoutView(getContext());
        addSubView(mNetChangeLayoutView);
        mNetChangeLayoutView.hide(ViewAction.HideType.Normal);
    }

    /**
     * 隐藏手势和控制栏和网络
     */
    private void hideGestureAndControlViews() {

        if (mGestureView != null) {
            mGestureView.hide(ViewAction.HideType.Normal);
        }

        if (mControlView != null) {
            mControlView.hide(ViewAction.HideType.Normal);
        }

        if (mNetChangeLayoutView != null) {
            mNetChangeLayoutView.hide(ViewAction.HideType.Normal);
        }
    }

    /**
     * 屏幕方向变为横屏。
     *
     * @param fromPort 是否从竖屏变过来
     */
    public void changedToLandForwardScape(boolean fromPort) {
        // 如果不是从竖屏变过来,也就是一直是横屏的时候,就不用操作了
        if (!fromPort) {
            return;
        }
        this.removeView(mHideNavBarView);
        this.addView(mHideNavBarView);
        changeScreenMode(AliyunScreenMode.Full, false);
        if (orientationChangeListener != null) {
            orientationChangeListener.orientationChange(fromPort, mCurrentScreenMode);
        }
    }

    /**
     * 屏幕方向变为横屏。
     *
     * @param fromPort 是否从竖屏变过来
     */
    public void changedToLandReverseScape(boolean fromPort) {
        // 如果不是从竖屏变过来,也就是一直是横屏的时候,就不用操作了
        if (!fromPort) {
            return;
        }
        this.removeView(mHideNavBarView);
        this.addView(mHideNavBarView);
        changeScreenMode(AliyunScreenMode.Full, true);
        if (orientationChangeListener != null) {
            orientationChangeListener.orientationChange(fromPort, mCurrentScreenMode);
        }
    }

    /**
     * 屏幕方向变为竖屏
     *
     * @param fromLand 是否从横屏转过来
     */
    public void changedToPortrait(boolean fromLand) {
        // 屏幕转为竖屏
        if (mIsFullScreenLocked) {
            return;
        }

        if (mCurrentScreenMode == AliyunScreenMode.Full) {
            // 没有固定竖屏,就变化mode
            if (fromLand) {
                this.removeView(mHideNavBarView);
                changeScreenMode(AliyunScreenMode.Small, false);
            } else {
                // 如果没有转到过横屏,就不让他转了。防止竖屏的时候点横屏之后,又立即转回来的现象
            }
        } else if (mCurrentScreenMode == AliyunScreenMode.Small) {
            // 竖屏的情况转到了竖屏
        }
        if (orientationChangeListener != null) {
            orientationChangeListener.orientationChange(fromLand, mCurrentScreenMode);
        }
    }

    /**
     * 获取整体缓冲进度
     *
     * @return 整体缓冲进度
     */
    public int getBufferPercentage() {
        if (mAliyunRenderView != null) {
            // 整体缓冲进度
            int mCurrentBufferPercentage = 0;
            return mCurrentBufferPercentage;
        }
        return 0;
    }

    /**
     * 重置。包括一些状态值,view的状态等
     */
    private void reset() {

        isCompleted = false;
        inSeek = false;
        mCurrentPosition = 0;
        mVideoBufferedPosition = 0;

        if (mTipsView != null) {
            mTipsView.hideAll();
        }
        if (mControlView != null) {
            mControlView.reset();
        }
        if (mNetChangeLayoutView != null) {
            mNetChangeLayoutView.reset();
        }
        if (mGestureView != null) {
            mGestureView.reset();
        }
        stop();
    }

    public ControlView getControlView() {
        return mControlView;
    }

    /**
     * 返回
     */
    public void backClick() {
        if (mNetChangeLayoutView.isShow()) {
            return;
        }
        if (mCurrentScreenMode == AliyunScreenMode.Full) {
            if (isLocalSource()) {
                // 如果播放的是本地视频,并且是全屏模式下点击返回按钮,则需要关闭Activity
                if (orientationChangeListener != null) {
                    orientationChangeListener.orientationChange(false, AliyunScreenMode.Small);
                }
            } else {
                //屏幕方向变为竖屏
                changedToPortrait(true);
            }
        } else if (mCurrentScreenMode == AliyunScreenMode.Small) {
            // 小屏状态下,就结束活动
            Context context = getContext();
            if (context instanceof Activity) {
                ((Activity) context).finish();
            }
        }
    }

    /**
     * 控制大小屏切换按钮的可见性
     */
    public void setScreenModeBtnVisible(boolean visible) {
        if (mControlView != null) {
            mControlView.setScreenModeBtnVisible(visible);
        }
    }

    /**
     * 更多按钮的可见性
     */
    public void setMoreBtnVisible(boolean visible) {
        if (mControlView != null) {
            mControlView.setMoreBtnVisible(visible);
        }
    }


    public void setOnTipClickListener(OnTipClickListener listener) {
        this.mOutOnTipClickListener = listener;
    }

    /**
     * 切换播放状态。点播播放按钮之后的操作
     */
    private void switchPlayerState() {

        // 非投屏状态下的处理
        if (mPlayerState == IPlayer.started) {
            pause();
        } else if (mPlayerState == IPlayer.paused || mPlayerState == IPlayer.prepared || mPlayerState == IPlayer.stopped) {
            start();
        } else if (mPlayerState == IPlayer.completion) {
            rePlay();
        }

        if (onPlayStateBtnClickListener != null) {
            onPlayStateBtnClickListener.onPlayBtnClick(mPlayerState);
        }
    }

    /**
     * 初始化字幕
     */
    private void initSubtitleView() {

        mSubtitleView = new SubtitleView(getContext());
        mSubtitleView.setId(R.id.alivc_player_subtitle);
        SubtitleView.DefaultValueBuilder defaultValueBuilder = new SubtitleView.DefaultValueBuilder();
        defaultValueBuilder.setLocation(LocationStyle.Location_Center);
        defaultValueBuilder.setColor("#e7e7e7");
        mSubtitleView.setDefaultValue(defaultValueBuilder);
        addSubView(mSubtitleView);

        mAssSubtitleView = new AssSubtitleView(getContext());
        mAssSubtitleView.setId(R.id.cicada_player_ass_subtitle);
        addSubViewByCenter(mAssSubtitleView);
    }

    /**
     * 判断是否开启精准seek
     */
    private void isAutoAccurate(long position) {

        if (GlobalPlayerConfig.PlayConfig.mEnableAccurateSeekModule) {
            mAliyunRenderView.seekTo(position, IPlayer.SeekMode.Accurate);
        } else {
            mAliyunRenderView.seekTo(position, IPlayer.SeekMode.Inaccurate);
        }
    }

    /**
     * 判断是否是本地资源
     */
    private boolean isLocalSource() {

        String scheme = null;
        if (GlobalPlayerConfig.PLAYTYPE.STS.equals(GlobalPlayerConfig.mCurrentPlayType)
                || GlobalPlayerConfig.PLAYTYPE.MPS.equals(GlobalPlayerConfig.mCurrentPlayType)
                || GlobalPlayerConfig.PLAYTYPE.AUTH.equals(GlobalPlayerConfig.mCurrentPlayType)
                || GlobalPlayerConfig.PLAYTYPE.DEFAULT.equals(GlobalPlayerConfig.mCurrentPlayType)) {
            return false;
        }
        if (mAliyunLocalSource == null || TextUtils.isEmpty(mAliyunLocalSource.getUri())) {
            return false;
        }
        if (GlobalPlayerConfig.PLAYTYPE.URL.equals(GlobalPlayerConfig.mCurrentPlayType)) {
            Uri parse = Uri.parse(mAliyunLocalSource.getUri());
            scheme = parse.getScheme();
        }
        return scheme == null;
    }

    /**
     * 显示错误提示
     *
     * @param errorCode  错误码
     * @param errorEvent 错误事件
     * @param errorMsg   错误描述
     */
    public void showErrorTipView(int errorCode, String errorEvent, String errorMsg) {

        stop();

        if (mControlView != null) {
            mControlView.setPlayState(ControlView.PlayState.NotPlaying);
        }

        if (mTipsView != null) {

            // 隐藏其他的动作,防止点击界面去进行其他操作
            mGestureView.hide(ViewAction.HideType.End);
            //不自动隐藏标题
            mControlView.closeAutoHide();
            //报错隐藏全屏按钮
            mControlView.hideScreenModeBtnInSmallScreenMode();

            mCoverView.setVisibility(GONE);
            mTipsView.showErrorTipView(errorCode, errorEvent, "Sorry, video playback failed");
        }

    }

    /**
     * addSubView 添加子view到布局中
     *
     * @param view 子view
     */
    private void addSubView(View view) {

        LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        // 添加到布局中
        addView(view, params);
    }

    /**
     * 添加子View到布局中央
     */
    private void addSubViewByCenter(View view) {

        LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        params.addRule(RelativeLayout.CENTER_IN_PARENT);
        addView(view, params);
    }

    /**
     * 改变屏幕模式:小屏或者全屏。
     *
     * @param targetMode {@link AliyunScreenMode}
     */
    public void changeScreenMode(AliyunScreenMode targetMode, boolean isReverse) {

        AliyunScreenMode finalScreenMode = targetMode;

        if (mIsFullScreenLocked) {
            finalScreenMode = AliyunScreenMode.Full;
        }

        // 这里可能会对模式做一些修改
        if (targetMode != mCurrentScreenMode) {
            mCurrentScreenMode = finalScreenMode;
        }

        if (mControlView != null) {
            mControlView.setScreenModeStatus(finalScreenMode);
        }

        if (mMarqueeView != null) {
            mMarqueeView.setScreenMode(finalScreenMode);
        }

        Context context = getContext();
        if (context instanceof Activity) {
            if (finalScreenMode == AliyunScreenMode.Full) {
                if (isReverse) {
                    ((Activity) context).setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
                } else {
                    ((Activity) context).setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
                }

            } else if (finalScreenMode == AliyunScreenMode.Small) {

                ((Activity) context).setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
                if (mMarqueeView != null) {
                    mMarqueeView.pause();
                }
            }
        }
    }

    /**
     * 获取当前屏幕模式:小屏、全屏
     *
     * @return 当前屏幕模式
     */
    public AliyunScreenMode getScreenMode() {
        return mCurrentScreenMode;
    }

    public void setScreenMode(AliyunScreenMode mCurrentScreenMode) {
        this.mCurrentScreenMode = mCurrentScreenMode;
    }

    /**
     * 设置停止播放监听
     *
     * @param onStoppedListener 停止播放监听
     */
    public void setOnStoppedListener(OnStoppedListener onStoppedListener) {
        this.mOnStoppedListener = onStoppedListener;
    }

    /**
     * 设置加载状态监听
     *
     * @param onLoadingListener 加载状态监听
     */
    public void setOnLoadingListener(IPlayer.OnLoadingStatusListener onLoadingListener) {
        if (mAliyunRenderView != null) {
            mAliyunRenderView.setOnLoadingStatusListener(onLoadingListener);
        }
    }

    /**
     * 设置视频宽高变化监听
     *
     * @param onVideoSizeChangedListener 视频宽高变化监听
     */
    public void setOnVideoSizeChangedListener(IPlayer.OnVideoSizeChangedListener onVideoSizeChangedListener) {
        if (mAliyunRenderView != null) {
            mAliyunRenderView.setOnVideoSizeChangedListener(onVideoSizeChangedListener);
        }
    }

    /**
     * 设置PlayAuth的播放方式
     *
     * @param aliyunPlayAuth auth
     */
    public void setAuthInfo(VidAuth aliyunPlayAuth) {

        if (mAliyunRenderView == null) {
            return;
        }

        // 重置界面
        clearAllSource();
        reset();

        mAliyunPlayAuth = aliyunPlayAuth;

        // 具体的准备操作
        prepareAuth(aliyunPlayAuth);
    }

    /**
     * 设置Mps播放方式
     */
    public void setVidMps(VidMps vidMps) {

        if (mAliyunRenderView == null) {
            return;
        }

        // 重置界面
        clearAllSource();

        reset();

        mAliyunVidMps = vidMps;

        // 具体的准备操作
        prepareMps(vidMps);
    }

    /**
     * 通过playAuth prepare
     *
     * @param aliyunPlayAuth 源
     */
    private void prepareAuth(VidAuth aliyunPlayAuth) {

        if (mTipsView != null) {
            mTipsView.showNetLoadingTipView();
        }

        mAliyunRenderView.setDataSource(aliyunPlayAuth);
        mAliyunRenderView.prepare();
    }

    /**
     * 通过Mps prepare
     */
    private void prepareMps(VidMps vidMps) {
        if (mTipsView != null) {
            mTipsView.showNetLoadingTipView();
        }
        mAliyunRenderView.setDataSource(vidMps);
        mAliyunRenderView.prepare();
    }

    /**
     * 清空之前设置的播放源
     */
    private void clearAllSource() {
        mAliyunPlayAuth = null;
        mAliyunVidSts = null;
        mAliyunLocalSource = null;
        mAliyunVidMps = null;
        mAliyunLiveSts = null;
    }

    /**
     * 准备LiveSts源
     */
    public void setLiveStsDataSource(LiveSts liveSts) {
        if (mAliyunRenderView == null) {
            return;
        }

        clearAllSource();
        reset();
        mAliyunLiveSts = liveSts;
        prepareLiveSts(liveSts);
    }

    /**
     * 设置播放源
     */
    public void setLocalSource(String url) {
        UrlSource urlSource = new UrlSource();
        urlSource.setUri(url);
        setLocalSource(urlSource);
    }

    /**
     * 设置播放源
     */
    public void setLocalSource(UrlSource aliyunLocalSource) {
        if (mAliyunRenderView == null) {
            return;
        }

        clearAllSource();
        reset();
        // 播放本地资源,需要清空VIDEO_FUNCTION
        mAliyunLocalSource = aliyunLocalSource;
        if (!show4gTips()) {
            prepareLocalSource(aliyunLocalSource);
        }
    }

    /**
     * 判断是否展示4g提示,内部进行了判断
     * 1.不是本地视频
     * 2.当前网络是4G
     * 满足上述条件才会展示4G提示
     *
     * @return true:需要展示,false不需要
     */
    private boolean show4gTips() {

        //播放本地文件不需要提示
        if (isLocalSource()) {
            return false;
        }

        //不是本地文件
        if (NetWatchdog.is4GConnected(getContext())) {
            //运营商自动播放,则Toast提示后,继续播放
            return !mIsOperatorPlay;
        } else {
            return false;
        }
    }

    /**
     * prepare本地播放源
     *
     * @param aliyunLocalSource 本地播放源
     */
    private void prepareLocalSource(UrlSource aliyunLocalSource) {

        if (mTipsView != null) {
            mTipsView.showNetLoadingTipView();
        }

        // 如果是本地视频,则全屏播放
        if (isLocalSource() && mIsNeedOnlyFullScreen) {
            changeScreenMode(AliyunScreenMode.Full, false);
        }

        if (aliyunLocalSource != null && aliyunLocalSource.getUri().startsWith("artc")) {
            Logger.t(TAG).e("artc setPlayerConfig");
            PlayerConfig playerConfig = mAliyunRenderView.getPlayerConfig();
            playerConfig.mMaxDelayTime = 1000;
            playerConfig.mHighBufferDuration = 10;
            playerConfig.mStartBufferDuration = 10;
            mAliyunRenderView.setPlayerConfig(playerConfig);
        }

        mAliyunRenderView.setAutoPlay(true);
        mAliyunRenderView.setDataSource(aliyunLocalSource);
        mAliyunRenderView.prepare();
    }

    /**
     * 准备vidsts源
     *
     * @param vidSts 源
     */
    public void setVidSts(VidSts vidSts) {
        if (mAliyunRenderView == null) {
            return;
        }
        clearAllSource();
        reset();
        mAliyunVidSts = vidSts;
        showVideoFunction();
    }

    /**
     * 展示播放器不同的功能
     */
    private void showVideoFunction() {
        if (mAliyunRenderView != null) {
            mPlayerState = IPlayer.stopped;
            mAliyunRenderView.stop();
        }

        if (mControlView != null) {
            mControlView.showNativeSeekBar();
        }

    }

    public void updateStsInfo(StsInfo stsInfo) {
        if (mAliyunRenderView != null) {
            mAliyunRenderView.updateStsInfo(stsInfo);
        }
    }

    public void updateAuthInfo(VidAuth vidAuth) {
        if (mAliyunRenderView != null) {
            mAliyunRenderView.updateAuthInfo(vidAuth);
        }
    }

    /**
     * 准备vidsts 源
     */
    private void prepareVidsts(VidSts vidSts) {

        if (mTipsView != null) {
            mTipsView.showNetLoadingTipView();
        }

        if (mAliyunRenderView != null) {
            mAliyunRenderView.setDataSource(vidSts);
            mAliyunRenderView.prepare();
        }
    }

    /**
     * 准备LiveSts 源
     */
    private void prepareLiveSts(LiveSts mAliyunLiveSts) {

        if (mTipsView != null) {
            mTipsView.showNetLoadingTipView();
        }

        if (mAliyunRenderView != null) {
            mAliyunRenderView.setDataSource(mAliyunLiveSts);
            mAliyunRenderView.prepare();
        }
    }

    /**
     * 运营商是否自动播放
     *
     * @param isOperatorPlay true为自动播放,false会有tips
     */
    public void setOperatorPlay(boolean isOperatorPlay) {
        this.mIsOperatorPlay = isOperatorPlay;
    }

    /**
     * 暂停播放器的操作
     */
    public void onStop() {

        stopOrientationWatchDog();
        // 保存播放器的状态,供resume恢复使用。
        savePlayerState();
    }

    /**
     * 开启屏幕旋转监听
     */
    public void startOrientationWatchDog() {
        if (mOrientationWatchDog != null) {
            mOrientationWatchDog.startWatch();
        }
    }

    /**
     * 关闭屏幕旋转监听
     */
    public void stopOrientationWatchDog() {
        if (mOrientationWatchDog != null) {
            mOrientationWatchDog.stopWatch();
        }
    }

    /**
     * Activity回来后,恢复之前的状态
     */
    private void resumePlayerState() {
        if (mAliyunRenderView == null) {
            return;
        }
        if (playResourceType == LIVE && mPlayerState == IPlayer.stopped) {
            // 直播
            mAliyunRenderView.prepare();
        } else {
            start();
        }
    }

    /**
     * 保存当前的状态,供恢复使用
     */
    private void savePlayerState() {
        if (mAliyunRenderView == null) {
            return;
        }
        if (playResourceType == LIVE) {
            // 直播调用stop
            mPlayerState = IPlayer.stopped;
            mAliyunRenderView.stop();
        } else {
            pause();
        }
    }

    /**
     * 获取媒体信息
     *
     * @return 媒体信息
     */
    public MediaInfo getMediaInfo() {
        if (mAliyunRenderView != null) {
            return mAliyunRenderView.getMediaInfo();
        }

        return null;
    }

    /**
     * 是否只需要全屏播放,只在播放本地视频和URL播放方式下生效
     */
    public void needOnlyFullScreenPlay(boolean isNeed) {
        mIsNeedOnlyFullScreen = isNeed;
    }

    /**
     * 是否处于播放状态:start或者pause了
     *
     * @return 是否处于播放状态
     */
    public boolean isPlaying() {
        return mPlayerState == IPlayer.started;
    }

    /**
     * 获取播放器状态
     *
     * @return 播放器状态
     */
    public int getPlayerState() {
        return mPlayerState;
    }

    /**
     * reload
     */
    public void reload() {
        if (mAliyunRenderView != null) {
            mAliyunRenderView.reload();
        }
    }

    /**
     * 开始播放
     */
    public void start() {

        if (mControlView != null) {
            mControlView.setPlayState(ControlView.PlayState.Playing);
        }

        if (mAliyunRenderView == null) {
            return;
        }

        if (mAdvVideoPlayerState == IPlayer.started && GlobalPlayerConfig.IS_VIDEO) {
            mControlView.setHideType(ViewAction.HideType.Normal);
            mGestureView.setHideType(ViewAction.HideType.Normal);

        } else {
            mGestureView.show();
            mControlView.show();
        }
        if (playResourceType == LIVE && mPlayerState == IPlayer.stopped) {
            // 直播
            mAliyunRenderView.prepare();
        } else {
            mAliyunRenderView.start();
            //语音播报播放中 且 音频被占用 变成暂停

        }

        if (mMarqueeView != null && mMarqueeView.isStart() && mCurrentScreenMode == AliyunScreenMode.Full) {
            mMarqueeView.startFlip();
        }
    }

    /**
     * 暂停播放
     */
    public void pause() {

        if (mControlView != null && !mIsScreenCosting) {
            mControlView.setPlayState(ControlView.PlayState.NotPlaying);
        }
        if (mAliyunRenderView == null) {
            return;
        }
        if (mPlayerState == IPlayer.started || mPlayerState == IPlayer.prepared) {
            if (playResourceType == LIVE) {
                // 直播调用stop
                mPlayerState = IPlayer.stopped;
                mAliyunRenderView.stop();
            } else {
                mAliyunRenderView.pause();
            }

            if (mMarqueeView != null) {
                mMarqueeView.pause();
            }
        }
    }

    /**
     * 停止播放
     */
    private void stop() {

        Boolean hasLoadedEnd = null;
        MediaInfo mediaInfo = null;
        if (mAliyunRenderView != null && hasLoadEnd != null) {
            mediaInfo = mAliyunRenderView.getMediaInfo();
            hasLoadedEnd = hasLoadEnd.get(mediaInfo);
        }

        if (mAliyunRenderView != null && hasLoadedEnd != null) {
            mPlayerState = IPlayer.stopped;
            mAliyunRenderView.stop();
        }
        if (mControlView != null) {
            mControlView.setPlayState(ControlView.PlayState.NotPlaying);
        }
        if (hasLoadEnd != null) {
            hasLoadEnd.remove(mediaInfo);
        }
    }

    /**
     * 停止后是否显示最后一帧
     */
    public void clearFrameWhenStop(boolean clearFrameWhenStop) {
        if (mAliyunRenderView != null) {
            PlayerConfig config = mAliyunRenderView.getPlayerConfig();
            config.mClearFrameWhenStop = clearFrameWhenStop;
            mAliyunRenderView.setPlayerConfig(config);
        }
    }

    /**
     * seek操作
     *
     * @param position 目标位置
     */
    public void seekTo(long position) {

        if (mAliyunRenderView == null) {
            return;
        }
        inSeek = true;
        reallySeekToFunction(position);
    }

    private void reallySeekToFunction(long position) {

        // 这里由于如果是视频广告seekEnd返回的progress是包含了视频广告的时间,而这里的seek,需要的是原视频的seek时间,所以需要减去视频广告的时间
        if (GlobalPlayerConfig.IS_VIDEO) {
            // 广告视频播放次数计数
            int mAdvVideoCount = 0;
            isAutoAccurate(position - mAdvVideoCount);
        } else {
            isAutoAccurate(position);
        }
        mAliyunRenderView.start();

        if (mControlView != null) {
            mControlView.setPlayState(ControlView.PlayState.Playing);
        }
    }

    /**
     * 让home键无效
     *
     * @param keyCode 按键
     * @param event   事件
     * @return 是否处理。
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode != KeyEvent.KEYCODE_HOME && keyCode != KeyEvent.KEYCODE_VOLUME_UP && keyCode != KeyEvent.KEYCODE_VOLUME_DOWN) {
            if (mCurrentScreenMode == AliyunScreenMode.Full) {
                changedToPortrait(true);
                return false;
            } else if (mCurrentScreenMode == AliyunScreenMode.AMPLIFY) {
                //模拟点击缩放按钮
                getControlView().getScreenModeBtn().performClick();
                return false;
            }
        }
        if (mIsFullScreenLocked && (keyCode != KeyEvent.KEYCODE_HOME)) {
            return false;
        }
        return true;
    }

    /**
     * 设置分享按钮点击监听
     */
    public void setOnShareClickListener(OnShareClickListener onShareClickListener) {
        this.mOnShareClickListener = onShareClickListener;
    }

    /**
     * 设置播放状态点击监听
     */
    public void setOnPlayStateBtnClickListener(OnPlayStateBtnClickListener listener) {
        this.onPlayStateBtnClickListener = listener;
    }

    public void setOnSeekStartListener(OnSeekStartListener listener) {
        this.onSeekStartListener = listener;
    }

    public void setOrientationChangeListener(OnOrientationChangeListener listener) {
        this.orientationChangeListener = listener;
    }

    /**
     * 原视频准备完成
     */
    public void sourceVideoPlayerPrepared() {

        if (mAliyunRenderView == null) {
            return;
        }
        mAliyunMediaInfo = mAliyunRenderView.getMediaInfo();
        if (mAliyunMediaInfo == null) {
            return;
        }
        // 防止服务器信息和实际不一致
        mSourceDuration = mAliyunRenderView.getDuration();
        mAliyunMediaInfo.setDuration((int) mSourceDuration);

        // 使用用户设置的标题
        if (!GlobalPlayerConfig.IS_VIDEO) {

            TrackInfo trackInfo = mAliyunRenderView.currentTrack(TrackInfo.Type.TYPE_VOD.ordinal());
            if (trackInfo != null) {
                mControlView.setMediaInfo(mAliyunMediaInfo, mAliyunRenderView.currentTrack(TrackInfo.Type.TYPE_VOD.ordinal()).getVodDefinition());
            } else {
                mControlView.setMediaInfo(mAliyunMediaInfo, "FD");
            }

            mControlView.setScreenModeStatus(mCurrentScreenMode);
            mControlView.show();
            mGestureView.show();
        }

        mControlView.setHideType(ViewAction.HideType.Normal);
        mGestureView.setHideType(ViewAction.HideType.Normal);
        if (mTipsView != null) {
            mTipsView.hideNetLoadingTipView();
            mTipsView.hideBufferLoadingTipView();
        }

    }

    /**
     * 原视频错误监听
     */
    public void sourceVideoPlayerError(ErrorInfo errorInfo) {
        if (mTipsView != null) {
            mTipsView.hideAll();
        }
        // 出错之后解锁屏幕,防止不能做其他操作,比如返回。
        lockScreen(false);

        // errorInfo.getExtra()展示为null,修改为显示errorInfo.getCode的十六进制的值
        showErrorTipView(errorInfo.getCode().getValue(), Integer.toHexString(errorInfo.getCode().getValue()),
                errorInfo.getMsg());

        LiveDataBus.getInstance().with(EventConstants.LIVE_PYBK_ERROR).postValue(errorInfo);

    }

    public void setPlayResourceType(int playResourceType, boolean isVR) {
        this.playResourceType = playResourceType;
        if (mControlView == null) {
            return;
        }
        mControlView.setPlayResourceType(playResourceType, isVR);
    }

    public void setTitle(String title) {

        if (mControlView == null) {
            return;
        }
        mControlView.setTitle(title);
    }

    /**
     * 直播回放或者预告展示这个
     */
    public void changeNetWorkView() {

        if ((playResourceType == REVIEW || playResourceType == PREVIEW) && Constants.Video_Playback == 1 && !Constants.isShowNetTips) {
            if (mNetChangeLayoutView != null) {
                if (mControlView != null && mControlView.getVisibility() == VISIBLE) {
                    mControlView.hide(ViewAction.HideType.Normal);
                }
                mNetChangeLayoutView.show();
                this.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        pause();
                    }
                }, 1000);
                mNetChangeLayoutView.setOnClickListener(new BaseClickListener() {
                    @Override
                    protected void onNoDoubleClick(View v) {
                        Constants.isShowNetTips = true;
                        mNetChangeLayoutView.hide(ControlView.HideType.Normal);
                        start();

                    }
                });
            }
        }
    }

    private static class VideoPlayerOnTrackReadyListener implements IPlayer.OnTrackReadyListener {

        public WeakReference<VideoAndLivePlayerView> weakReference;

        public VideoPlayerOnTrackReadyListener(VideoAndLivePlayerView playerView) {
            this.weakReference = new WeakReference<>(playerView);
        }

        @Override
        public void onTrackReady(MediaInfo mediaInfo) {
            VideoAndLivePlayerView aliyunVodPlayerView = weakReference.get();
            if (aliyunVodPlayerView != null) {
                aliyunVodPlayerView.onTrackReady(mediaInfo);
            }
        }
    }

    private void onTrackReady(MediaInfo mediaInfo) {
        if (mOutOnTrackReadyListener != null) {
            mOutOnTrackReadyListener.onTrackReady(mediaInfo);
        }
    }

    /**
     * 原视频开始加载
     */
    public void sourceVideoPlayerLoadingBegin() {

        if (mTipsView != null) {
            // 视频广告,并且广告视频在播放状态,不要展示loading
            if (GlobalPlayerConfig.IS_VIDEO && mAdvVideoPlayerState == IPlayer.started) {

            } else {
                mTipsView.hideNetLoadingTipView();
                mTipsView.showBufferLoadingTipView();
                mTipsView.updateLoadingPercent(0, currentNetSpeed);
            }
        }
    }

    /**
     * 原视频开始加载进度
     */
    public void sourceVideoPlayerLoadingProgress(int percent) {

        if (mTipsView != null) {
            // 视频广告,并且广告视频在播放状态,不要展示loading
            if (GlobalPlayerConfig.IS_VIDEO && mAdvVideoPlayerState == IPlayer.started) {

            } else {
                mTipsView.updateLoadingPercent(percent, currentNetSpeed);
            }
            if (percent == 100) {
                mTipsView.hideBufferLoadingTipView();
            }
        }
    }

    /**
     * 显示直播结束view
     */
    public void showLiveEndView() {

        if (mAliyunRenderView != null) {
            mAliyunRenderView.setVisibility(GONE);
            mAliyunRenderView.release();
            mAliyunRenderView = null;
        }

        if (mControlView != null) {
            //在小屏时隐藏全屏按钮
            mControlView.hideScreenModeBtnInSmallScreenMode();
        }
    }

    /**
     * 显示直播垫片view
     */
    public void showLiveShimView(String liveBroadcastGasket) {
        if (mTipsView == null) {
            return;
        }
        if (mAliyunRenderView != null) {
            mAliyunRenderView.setVisibility(GONE);
            onStop();
        }
        mTipsView.showLiveShimView(liveBroadcastGasket, activity_landscape);
    }

    /**
     * 隐藏直播垫片view
     */
    public void hideLiveShimView(String liveBroadcastGasket) {
        if (mTipsView == null) {
            return;
        }
        if (mAliyunRenderView != null && mAliyunRenderView.getVisibility() == GONE) {
            mAliyunRenderView.setVisibility(VISIBLE);
        }
        mTipsView.hideLiveShimView(liveBroadcastGasket, activity_landscape);
    }

    /**
     * 原视频加载结束
     */
    public void sourceVideoPlayerLoadingEnd() {

        if (mTipsView != null) {
            if (isPlaying()) {
                mTipsView.hideErrorTipView();
            }
            mTipsView.hideBufferLoadingTipView();
        }
        if (mControlView != null) {
            mControlView.setHideType(ViewAction.HideType.Normal);
        }
        if (mGestureView != null) {
            mGestureView.setHideType(ViewAction.HideType.Normal);
            mGestureView.show();
        }
        if (mNetChangeLayoutView != null) {
            mNetChangeLayoutView.setHideType(ViewAction.HideType.Normal);
        }
        hasLoadEnd.put(mAliyunMediaInfo, true);
        vodPlayerLoadEndHandler.sendEmptyMessage(1);
    }

    /**
     * 原视频状态改变监听
     */
    public void sourceVideoPlayerStateChanged(int newState) {

        mPlayerState = newState;
        if (newState == IPlayer.stopped) {
            if (mOnStoppedListener != null) {
                mOnStoppedListener.onStop();
            }
        } else if (newState == IPlayer.started) {
            if (mControlView != null) {
                mControlView.setPlayState(ControlView.PlayState.Playing);
            }
            if (mNetChangeLayoutView != null) {
                mNetChangeLayoutView.setPlayState(ControlView.PlayState.Playing);
            }
            if (NetworkUtil.isNetAvailable() && !NetworkUtil.isWifi()) {
                changeNetWorkView();
            }
        }
        if (mPlayerState == IPlayer.started || mPlayerState == IPlayer.prepared || mPlayerState == IPlayer.paused || mPlayerState == IPlayer.error) {
            if (mTipsView != null) {
                mTipsView.hideAll();
            }
        }

    }

    /**
     * 原视频播放完成
     */
    public void sourceVideoPlayerCompletion() {

        LiveDataBus.getInstance().with(EventConstants.RECEIVED_LIVE_END_PYBK, Boolean.class).postValue(true);
        inSeek = false;
        //如果当前播放资源是本地资源时, 再显示replay
        if (mTipsView != null && isLocalSource()) {
            //预约不展示重播
            if (playResourceType != PREVIEW) {
                //隐藏其他的动作,防止点击界面去进行其他操作
                mGestureView.hide(ViewAction.HideType.End);
                mControlView.hide(ViewAction.HideType.End);
                mNetChangeLayoutView.hide(ViewAction.HideType.End);
                if(showReplayView){
                    mTipsView.showReplayTipView();
                }
            }
        }

        //不是循环播放
        if (!isLoop()) {
            //false,updateAllViews重复设置了,所以加false规避
            mControlView.setPlayState(ControlView.PlayState.NotPlaying, false);
            mControlView.updateAllViews();
            if (playResourceType != PREVIEW && playResourceType != REVIEW) {
                mControlView.setRePlayVisible(true);
            }
        }

    }

    /**
     * 原视频Info
     */
    public void sourceVideoPlayerInfo(InfoBean infoBean) {

        if (infoBean.getCode() == InfoCode.AutoPlayStart) {
            // 自动播放开始,需要设置播放状态
            if (mControlView != null) {
                mControlView.setPlayState(ControlView.PlayState.Playing);
                mNetChangeLayoutView.setPlayState(ControlView.PlayState.Playing);
            }
        } else if (infoBean.getCode() == InfoCode.BufferedPosition) {
            // 更新bufferedPosition
            mVideoBufferedPosition = infoBean.getExtraValue();
            mControlView.setVideoBufferPosition((int) mVideoBufferedPosition);
        } else if (infoBean.getCode() == InfoCode.CurrentPosition) {
            // 更新currentPosition
            mCurrentPosition = infoBean.getExtraValue();

            if (mControlView != null && !inSeek && mPlayerState == IPlayer.started) {
                mControlView.setVideoPosition((int) mCurrentPosition);
            }

            if (onSeekStartListener != null && mSourceDuration != 0) {
                double percentage = (double) mCurrentPosition / mSourceDuration * 100;
                onSeekStartListener.onProgressChanged((int) percentage);
            }
        }

    }

    /**
     * 原视频onVideoRenderingStart
     */
    public void sourceVideoPlayerOnVideoRenderingStart() {

        if (mControlView != null) {
            // 开启状态栏自动隐藏的功能
            mControlView.openAutoHide();
        }
        mCoverView.setVisibility(GONE);
    }

    /**
     * 字幕显示和隐藏监听
     */
    private static class VideoPlayerSubtitleDeisplayListener implements IPlayer.OnSubtitleDisplayListener {

        private final WeakReference<VideoAndLivePlayerView> weakReference;

        public VideoPlayerSubtitleDeisplayListener(VideoAndLivePlayerView aliyunVodPlayerView) {
            weakReference = new WeakReference<>(aliyunVodPlayerView);
        }

        @Override
        public void onSubtitleExtAdded(int trackIndex, String url) {

            VideoAndLivePlayerView aliyunVodPlayerView = weakReference.get();
            if (aliyunVodPlayerView != null) {
                aliyunVodPlayerView.mAliyunRenderView.selectExtSubtitle(trackIndex, true);
            }
        }

        @Override
        public void onSubtitleShow(int trackIndex, long id, String data) {
            VideoAndLivePlayerView aliyunVodPlayerView = weakReference.get();
            if (aliyunVodPlayerView != null) {
                aliyunVodPlayerView.onSubtitleShow(trackIndex, id, data);
            }
        }

        @Override
        public void onSubtitleHide(int trackIndex, long id) {
            VideoAndLivePlayerView aliyunVodPlayerView = weakReference.get();
            if (aliyunVodPlayerView != null) {
                aliyunVodPlayerView.onSubtitleHide(trackIndex, id);
            }
        }

        @Override
        public void onSubtitleHeader(int trackIndex, String header) {
            VideoAndLivePlayerView aliyunVodPlayerView = weakReference.get();
            if (aliyunVodPlayerView != null) {
                aliyunVodPlayerView.onSubtitleHeader(trackIndex, header);
            }
        }
    }

    private void onSubtitleHeader(int trackIndex, String header) {
        if (!TextUtils.isEmpty(header)) {
            mSubtitleTypeMap.put(trackIndex, AssHeader.SubtitleType.SubtitleTypeAss);
            mAssSubtitleView.setAssHeader(header);
        }
    }

    /**
     * 字幕隐藏
     *
     * @param id 索引
     */
    private void onSubtitleHide(int trackIndex, long id) {
        if (mSubtitleTypeMap.size() > 0 && mSubtitleTypeMap.get(trackIndex) == AssHeader.SubtitleType.SubtitleTypeAss) {
            mAssSubtitleView.dismiss(id);
        } else {
            mSubtitleView.dismiss(id + "");
        }
    }

    /**
     * 字幕显示
     *
     * @param id   索引
     * @param data 数据
     */
    private void onSubtitleShow(int trackIndex, long id, String data) {
        if (mSubtitleTypeMap.size() > 0 && mSubtitleTypeMap.get(trackIndex) == AssHeader.SubtitleType.SubtitleTypeAss) {
            mAssSubtitleView.show(id, data);
        } else {
            SubtitleView.Subtitle subtitle = new SubtitleView.Subtitle();
            subtitle.id = id + "";
            subtitle.content = data;
            mSubtitleView.show(subtitle);
        }
    }

    /**
     * 播放器TrackChanged监听
     */
    private static class VideoPlayerTrackChangedListener implements IPlayer.OnTrackChangedListener {

        private final WeakReference<VideoAndLivePlayerView> weakReference;

        public VideoPlayerTrackChangedListener(VideoAndLivePlayerView aliyunVodPlayerView) {
            weakReference = new WeakReference<>(aliyunVodPlayerView);
        }

        @Override
        public void onChangedSuccess(TrackInfo trackInfo) {
            VideoAndLivePlayerView aliyunVodPlayerView = weakReference.get();
            if (aliyunVodPlayerView != null) {
                aliyunVodPlayerView.sourceVideoPlayerTrackInfoChangedSuccess(trackInfo);
            }
        }

        @Override
        public void onChangedFail(TrackInfo trackInfo, ErrorInfo errorInfo) {
            VideoAndLivePlayerView aliyunVodPlayerView = weakReference.get();
            if (aliyunVodPlayerView != null) {
                aliyunVodPlayerView.sourceVideoPlayerTrackInfoChangedFail(trackInfo, errorInfo);
            }
        }
    }

    /**
     * 原视频 trackInfoChangedSuccess
     */
    private void sourceVideoPlayerTrackInfoChangedSuccess(TrackInfo trackInfo) {
        // 清晰度切换监听
        if (trackInfo.getType() == TrackInfo.Type.TYPE_VOD) {
            if (mIsScreenCosting) {
                if (mControlView != null) {
                    mControlView.setVideoPosition((int) mCurrentPosition);
                }
            } else {
                start();
            }

            if (mTipsView != null) {
                mTipsView.hideNetLoadingTipView();
            }
        }
    }

    /**
     * 原视频 trackInfochangedFail
     */
    private void sourceVideoPlayerTrackInfoChangedFail(TrackInfo trackInfo, ErrorInfo errorInfo) {
        // 失败的话,停止播放,通知上层
        if (mTipsView != null) {
            mTipsView.hideNetLoadingTipView();
        }
        stop();
    }

    /**
     * 原视频seek完成
     */
    public void sourceVideoPlayerSeekComplete() {
        inSeek = false;
    }

    /**
     * sei
     */
    private static class VideoPlayerOnSeiDataListener implements IPlayer.OnSeiDataListener {

        private final WeakReference<VideoAndLivePlayerView> weakReference;

        public VideoPlayerOnSeiDataListener(VideoAndLivePlayerView aliyunVodPlayerView) {
            weakReference = new WeakReference<>(aliyunVodPlayerView);
        }

        @Override
        public void onSeiData(int type, byte[] bytes) {
            VideoAndLivePlayerView aliyunVodPlayerView = weakReference.get();
            if (aliyunVodPlayerView != null) {
                aliyunVodPlayerView.onSeiData(type, bytes);
            }
        }
    }

    private void onSeiData(int type, byte[] bytes) {
        if (mOutOnSeiDataListener != null) {
            mOutOnSeiDataListener.onSeiData(type, bytes);
        }
    }

    public void setOutOnSeiDataListener(IPlayer.OnSeiDataListener listener) {
        this.mOutOnSeiDataListener = listener;
    }

    private static class VideoPlayerOnVerifyStsCallback implements AliPlayer.OnVerifyTimeExpireCallback {

        private final WeakReference<VideoAndLivePlayerView> weakReference;

        public VideoPlayerOnVerifyStsCallback(VideoAndLivePlayerView aliyunVodPlayerView) {
            weakReference = new WeakReference<>(aliyunVodPlayerView);
        }

        @Override
        public AliPlayer.Status onVerifySts(StsInfo stsInfo) {
            VideoAndLivePlayerView aliyunVodPlayerView = weakReference.get();
            if (aliyunVodPlayerView != null) {
                return aliyunVodPlayerView.onVerifySts(stsInfo);
            }
            return AliPlayer.Status.Valid;
        }

        @Override
        public AliPlayer.Status onVerifyAuth(VidAuth vidAuth) {
            VideoAndLivePlayerView aliyunVodPlayerView = weakReference.get();
            if (aliyunVodPlayerView != null) {
                return aliyunVodPlayerView.onVerifyAuth(vidAuth);
            }
            return AliPlayer.Status.Valid;
        }
    }

    public void setOutOnVerifyTimeExpireCallback(AliPlayer.OnVerifyTimeExpireCallback listener) {
        this.mOutOnVerifyTimeExpireCallback = listener;
    }

    private AliPlayer.Status onVerifyAuth(VidAuth vidAuth) {
        if (mOutOnVerifyTimeExpireCallback != null) {
            return mOutOnVerifyTimeExpireCallback.onVerifyAuth(vidAuth);
        }
        return AliPlayer.Status.Valid;
    }

    private AliPlayer.Status onVerifySts(StsInfo stsInfo) {
        if (mOutOnVerifyTimeExpireCallback != null) {
            return mOutOnVerifyTimeExpireCallback.onVerifySts(stsInfo);
        }
        return AliPlayer.Status.Valid;
    }

    /**
     * 设置硬解码开关
     */
    public void setEnableHardwareDecoder(boolean enableHardwareDecoder) {
        if (mAliyunRenderView != null) {
            mAliyunRenderView.enableHardwareDecoder(enableHardwareDecoder);
        }
    }

    /**
     * 多码率时默认播放码率
     */
    public void setDefaultBandWidth(int bandWidth) {
        if (mAliyunRenderView != null) {
            mAliyunRenderView.setDefaultBandWidth(bandWidth);
        }
    }

    /**
     * 获取当前播放器正在播放的媒体信息
     */
    public MediaInfo getCurrentMediaInfo() {
        return mAliyunMediaInfo;
    }

    /**
     * 截图功能
     */
    public void snapShot() {
        if (mAliyunRenderView != null) {
            mAliyunRenderView.snapshot();
        }
    }

    /**
     * 设置播放时的镜像模式
     *
     * @param mode 镜像模式
     */
    public void setRenderMirrorMode(IPlayer.MirrorMode mode) {
        if (mAliyunRenderView != null) {
            mAliyunRenderView.setMirrorMode(mode);
        }
    }

    /**
     * 设置播放时的旋转方向
     *
     * @param rotate 旋转角度
     */
    public void setRenderRotate(IPlayer.RotateMode rotate) {
        if (mAliyunRenderView != null) {
            mAliyunRenderView.setRotateModel(rotate);
        }
    }

    /**
     * 设置是否显示标题栏
     *
     * @param show true:是
     */
    public void setTitleBarCanShow(boolean show) {
        if (mControlView != null) {
            mControlView.setTitleBarCanShow(show);
        }
    }

    /**
     * 设置是否显示控制栏
     *
     * @param show true:是
     */
    public void setControlBarCanShow(boolean show) {

        if (mControlView != null) {
            mControlView.setControlBarCanShow(show);
        }
    }

    /**
     * 设置竖屏视频模式
     */
    public void setVerticalVideoScreenMode(AliyunScreenMode mode) {
        if (mControlView != null) {
            mCurrentScreenMode = mode;
            mControlView.setVerticalVideoScreenMode(mode);
        }
    }

    /**
     * 设置是否显示进度条
     *
     * @param show true:是
     */
    public void setSeekbarBarCanShow(boolean show) {
        if (mControlView != null) {
            mControlView.setSeekbarVisible(show);
        }
    }

    /**
     * 暂停按钮隐藏展示
     */
    public void setPlayStateBtnVisible(boolean show) {
        if (mControlView != null) {
            mControlView.setPlayStateBtnVisible(show);
        }
    }


    /**
     * 是否是小窗
     */
    private boolean isSpecialPlayState = false;

    /**
     * 暂停按钮隐藏展示(特殊场景,图文小屏用)
     */
    public void setSpecialPlayStateBtnVisible(boolean show) {

        // 是否是小窗
        isSpecialPlayState = show;

        if (mControlView != null) {
            mControlView.setSpecialPlayStateBtnVisible(show);
        }
    }


    /**
     * 给子View设置圆角
     */
    public void clipToOutlineChildView() {

        setOutlineProvider(new RenderViewOutlineProvider(getContext().getResources().getDimension(R.dimen.rmrb_dp4)));
        setClipToOutline(true);

        if (mAliyunRenderView != null) {
            mAliyunRenderView.setOutlineProvider(new RenderViewOutlineProvider(getContext().getResources().getDimension(R.dimen.rmrb_dp4)));
            mAliyunRenderView.setClipToOutline(true);
        }

        if (mControlView != null) {
            mControlView.setOutlineProvider(new RenderViewOutlineProvider(getContext().getResources().getDimension(R.dimen.rmrb_dp4)));
            mControlView.setClipToOutline(true);
        }

        if (mCoverView != null) {
            mCoverView.setOutlineProvider(new RenderViewOutlineProvider(getContext().getResources().getDimension(R.dimen.rmrb_dp4)));
            mCoverView.setClipToOutline(true);
        }
    }

    /**
     * 取消子View设置圆角
     */
    public void cancelClipToOutlineChildView() {

        setClipToOutline(false);

        if (mAliyunRenderView != null) {
            mAliyunRenderView.setClipToOutline(false);
        }

        if (mControlView != null) {
            mControlView.setClipToOutline(false);
        }

        if (mCoverView != null) {
            mCoverView.setClipToOutline(false);
        }
    }

    /**
     * 设置子View宽度
     */
    public void setChildViewWith(int with) {

        if (mAliyunRenderView != null) {
            LayoutParams params = (LayoutParams) mAliyunRenderView.getLayoutParams();
            params.width = with;
            mAliyunRenderView.setLayoutParams(params);
        }

        if (mControlView != null) {
            LayoutParams params = (LayoutParams) mControlView.getLayoutParams();
            params.width = with;
            mControlView.setLayoutParams(params);
        }

        if (mTipsView != null) {
            LayoutParams params = (LayoutParams) mTipsView.getLayoutParams();
            params.width = with;
            mTipsView.setLayoutParams(params);
        }

        if (mCoverView != null) {
            LayoutParams params = (LayoutParams) mCoverView.getLayoutParams();
            params.width = with;
            mCoverView.setLayoutParams(params);
        }

        if (mVideoDurationView != null) {
            LayoutParams params = (LayoutParams) mVideoDurationView.getLayoutParams();
            params.width = with;
            mVideoDurationView.setLayoutParams(params);
        }
    }


    /**
     * 设置缓存配置
     */
    public void setCacheConfig(CacheConfig cacheConfig) {
        if (mAliyunRenderView != null) {
            mAliyunRenderView.setCacheConfig(cacheConfig);
        }
    }

    /**
     * 获取Config
     */
    public PlayerConfig getPlayerConfig() {
        if (mAliyunRenderView != null) {
            return mAliyunRenderView.getPlayerConfig();
        }
        return null;
    }

    /**
     * 设置Config
     */
    public void setPlayerConfig(PlayerConfig playerConfig) {
        if (mAliyunRenderView != null) {
            mAliyunRenderView.setPlayerConfig(playerConfig);
        }
    }

    /**
     * 获取SDK版本号
     *
     * @return SDK版本号
     */
    public String getSDKVersion() {
        return AliPlayerFactory.getSdkVersion();
    }


    /**
     * 设置自动播放
     *
     * @param auto true 自动播放
     */
    public void setAutoPlay(boolean auto) {
        if (mAliyunRenderView != null) {
            mAliyunRenderView.setAutoPlay(auto);
        }
    }

    /**
     * 锁定屏幕。锁定屏幕后,只有锁会显示,其他都不会显示。手势也不可用
     *
     * @param lockScreen 是否锁住
     */
    public void lockScreen(boolean lockScreen) {
        mIsFullScreenLocked = lockScreen;
        if (mControlView != null) {
            mControlView.setScreenLockStatus(mIsFullScreenLocked);
        }
        if (mGestureView != null) {
            mGestureView.setScreenLockStatus(mIsFullScreenLocked);
        }

    }

    /**
     * 重试播放,会从当前位置开始播放
     */
    public void reTry() {

        isCompleted = false;
        inSeek = false;

        int currentPosition = mControlView.getVideoPosition();

        if (mTipsView != null) {
            mTipsView.hideAll();
        }
        if (mControlView != null) {
            // 防止被reset掉,下次还可以获取到这些值
            mControlView.setVideoPosition(currentPosition);
        }

        if (mGestureView != null) {
            mGestureView.reset();
        }

        if (mAliyunRenderView != null) {

            // 显示网络加载的loading。。
            if (mTipsView != null) {
                mTipsView.showNetLoadingTipView();
            }
            // seek到当前的位置再播放
            mAliyunRenderView.prepare();
            isAutoAccurate(currentPosition);
            countdownPlayerError();
        }

    }

    // 仅判断 直播 状态为 开始播放一直播放不出来 处理
    public void countdownPlayerError() {

        if (mAliyunRenderView != null) {

            mAliyunRenderView.postDelayed(new Runnable() {
                @Override
                public void run() {
                    if (mPlayerState == 1) {
                        ErrorInfo errorInfo = new ErrorInfo();
                        if (mTipsView != null) {
                            mTipsView.hideNetLoadingTipView();
                        }
                        showErrorTipView(0, "errorEvent", "errorMsg");
                        errorInfo.setMsg("Live_load_timeout_of_10_seconds");
                        LiveDataBus.getInstance().with(EventConstants.LIVE_PYBK_ERROR).postValue(errorInfo);
                    }
                }
            }, 10 * 1000);
        }

    }

    /**
     * 重播,将会从头开始播放
     */
    public void rePlay() {

        isCompleted = false;
        inSeek = false;

        if (mTipsView != null) {
            mTipsView.hideAll();
        }
        if (mControlView != null) {
            mControlView.reset();
        }
        if (mNetChangeLayoutView != null) {
            mNetChangeLayoutView.reset();
        }
        if (mGestureView != null) {
            mGestureView.reset();
        }

        //重播,将会从0开始播放
        if (mControlView != null) {
            mControlView.setVideoPosition(0);
        }
        seekTo(0);
    }

    /**
     * 设置静音,默认是false
     */
    public void setMuteAliyunRenderView(boolean isMute) {
        if (mAliyunRenderView != null) {
            mAliyunRenderView.setMute(isMute);
        }
    }

    /**
     * 设置封面
     *
     * @param url 封面图url
     */
    public void setCoverUrl(String url) {
        if (mCoverView == null) {
            return;
        }

        ImageUtils.getInstance().loadImageNoPlaceholder(mCoverView, url);
        mCoverView.setVisibility(isPlaying() ? GONE : VISIBLE);
    }

    /**
     * 设置时长
     *
     * @param duration 时长
     */
    public void setDuration(long duration) {
        if (mVideoDurationView != null) {
            if (mVideoDurationView.getVisibility() == GONE) {
                mVideoDurationView.setVisibility(VISIBLE);
            }
            mVideoDurationView.setDuration(duration);
        }
    }

    /**
     * 设置缩放模式
     *
     * @param scaleMode 缩放模式
     */
    public void setScaleMode(IPlayer.ScaleMode scaleMode) {
        if (mAliyunRenderView != null) {
            mAliyunRenderView.setScaleModel(scaleMode);
        }
    }

    /**
     * 获取缩放模式
     *
     * @return 当前缩放模式
     */
    public IPlayer.ScaleMode getScaleMode() {
        IPlayer.ScaleMode scaleMode = IPlayer.ScaleMode.SCALE_ASPECT_FIT;
        if (mAliyunRenderView != null) {
            scaleMode = mAliyunRenderView.getScaleModel();
        }
        return scaleMode;
    }

    /**
     * 设置是否循环播放
     */
    public void setLoop(boolean isLoop) {
        if (mAliyunRenderView != null) {
            mAliyunRenderView.setLoop(isLoop);
        }
    }

    /**
     * 是否开启循环播放
     */
    public boolean isLoop() {
        if (mAliyunRenderView != null) {
            return mAliyunRenderView.isLoop();
        }
        return false;
    }

    // 连网断网监听
    public void setNetConnectedListener(NetConnectedListener listener) {
        this.mNetConnectedListener = listener;
    }

    /**
     * 断网/连网监听
     */
    private class MyNetConnectedListener implements NetWatchdog.NetConnectedListener {

        public MyNetConnectedListener(VideoAndLivePlayerView mVideoAndLivePlayerView) {
        }

        @Override
        public void onReNetConnected(boolean isReconnect) {
            if (mNetConnectedListener != null) {
                mNetConnectedListener.onReNetConnected(isReconnect);
            }
        }

        @Override
        public void onNetUnConnected() {
            Log.e("onNetDisconnected", "onNetUnConnected2222>>>");
            if (mNetConnectedListener != null) {
                mNetConnectedListener.onNetUnConnected();
            }
        }
    }

    /**
     * 判断是否有网络的监听
     */
    public interface NetConnectedListener {
        /**
         * 网络已连接
         */
        void onReNetConnected(boolean isReconnect);

        /**
         * 网络未连接
         */
        void onNetUnConnected();
    }

    public AliyunRenderView getmAliyunRenderView() {
        return mAliyunRenderView;
    }


    /**
     * *分享 viwe 展示隐藏
     */
    public void setMoreViewShowStatus(boolean isShow) {
        if (mControlView != null) {
            mControlView.setIvMore(isShow);
        }
    }


    public void setVerticalVideoFullBtnClick(AliyunScreenMode aliyunScreenMode) {
        if (mControlView != null) {
            mControlView.setVerticalVideoFullBtnClick(aliyunScreenMode);
        }
    }

    public void setIsDetail() {
        if (mControlView != null) {
            mControlView.setIsDetail();
        }
    }

    public void setShowReplayView(boolean showReplayView) {
        this.showReplayView = showReplayView;
    }
}