Android工程搭建 1、音频播放 2、视频播放 3、横竖屏切换 4、动态获取权限
1、音频播放
为了保证当前只有一首歌曲播放,创建一个MediaPlayer的工具类,使用单例模式创建,但是为了节省资源,我们需要采用懒汉式去创建
工具类胆码
package com. example. day8_medioplayer; import android. media. MediaPlayer; import java. io. IOException;
import java. util. ArrayList; public class MedioPlayerUtils { MediaPlayer mediaPlayer; public static MedioPlayerUtils medioPlayerUtils; ArrayList< Music> list = new ArrayList ( ) ; int index = 0 ; public static final int NEXT = 0 ; public static final int DAN = 1 ; public static final int RAN = 2 ; int type = 0 ; private MedioPlayerUtils ( ) { mediaPlayer = new MediaPlayer ( ) ; mediaPlayer. setOnCompletionListener ( new MediaPlayer. OnCompletionListener ( ) { @Override public void onCompletion ( MediaPlayer mp) { switch ( type) { case NEXT: next ( ) ; break ; case DAN: setDataSource ( index) ; break ; case RAN: int ran = ( int ) ( Math. random ( ) * list. size ( ) ) ; index = ran; next ( ) ; break ; } } } ) ; } public static MedioPlayerUtils getInstnce ( ) { if ( medioPlayerUtils == null) { synchronized ( String. class ) { if ( medioPlayerUtils == null) { medioPlayerUtils = new MedioPlayerUtils ( ) ; } } } return medioPlayerUtils; } public int setType ( ) { switch ( type) { case NEXT: type = DAN; break ; case DAN: type = RAN; break ; case RAN: type = NEXT; break ; } return type; } public void setDataSource ( int position) { try { mediaPlayer. reset ( ) ; mediaPlayer. setDataSource ( list. get ( position) . getData ( ) ) ; mediaPlayer. prepare ( ) ; mediaPlayer. setOnPreparedListener ( new MediaPlayer. OnPreparedListener ( ) { @Override public void onPrepared ( MediaPlayer mp) { mediaPlayer. start ( ) ; } } ) ; index = position; } catch ( IOException e) { e. printStackTrace ( ) ; } } public void start ( ) { if ( mediaPlayer != null) { mediaPlayer. start ( ) ; } } public void pause ( ) { if ( mediaPlayer != null) { mediaPlayer. pause ( ) ; } } public void next ( ) { index++ ; if ( index >= list. size ( ) - 1 ) { index = 0 ; } setDataSource ( index) ; } public void pre ( ) { index-- ; if ( index <= 0 ) { index = list. size ( ) - 1 ; } setDataSource ( index) ; } public boolean isPlaying ( ) { if ( mediaPlayer != null) { boolean playing = mediaPlayer. isPlaying ( ) ; return playing; } return false ; } public void setList ( ArrayList< Music> list) { this . list = list; } public long getDuration ( ) { long duration = 0 ; if ( mediaPlayer != null) { duration = mediaPlayer. getDuration ( ) ; } return duration; } public long getCurrentPosition ( ) { long currentPosition = 0 ; if ( mediaPlayer != null) { currentPosition = mediaPlayer. getCurrentPosition ( ) ; } return currentPosition; } public void seekTo ( int msec) { if ( mediaPlayer != null) { mediaPlayer. seekTo ( msec) ; } } public String getArtist ( ) { if ( mediaPlayer != null) { String artist = list. get ( index) . getArtist ( ) ; return artist; } return null; } public String getTitle ( ) { if ( mediaPlayer != null) { String title = list. get ( index) . getTitle ( ) ; return title; } return null; } }
2、视频播放
用MediaPlayer去播放视频,需要采用方法将SurfaceView和MediaPlayer绑定在一起
@Override public void surfaceCreated ( SurfaceHolder holder) { MedUtils. mediaPlayer. setDisplay ( surfaceHolder) ; }
3、横竖屏切换
if ( getResources ( ) . getConfiguration ( ) . orientation == Configuration. ORIENTATION_LANDSCAPE) { setRequestedOrientation ( ActivityInfo. SCREEN_ORIENTATION_PORTRAIT) ; } else if ( getResources ( ) . getConfiguration ( ) . orientation == Configuration. ORIENTATION_PORTRAIT) { setRequestedOrientation ( ActivityInfo. SCREEN_ORIENTATION_LANDSCAPE) ; }
4、动态获取权限
String[ ] strings = new String [ ] { Manifest. permission. INTERNET} ; if ( Build. VERSION. SDK_INT>= Build. VERSION_CODES. M) { for ( int i= 0 ; i< strings. length; i++ ) { int permission = ActivityCompat. checkSelfPermission ( this , strings[ i] ) ; if ( permission== PackageManager. PERMISSION_DENIED) { requestPermissions ( strings, 101 ) ; } } }