Android also has a pretty good support for playing audio and video, and it provides a more complete set of APIs that make it easy for developers to write a simple audio or video player. Today we started the learning of audio and video usage in Android.
Directory Navigation
- Playback of audio
- Playback of video
- Friendship Link
Playback of audio
The project structure is as follows: A simple read application of audio or video resources on SD card
Playing audio files in Android is generally done using the MediaPlayer class, which provides a very comprehensive control of audio files in a variety of formats, making it easy to play music.
First, the initialization of audio playback, call MediaPlayer setdatasource method, you can receive the absolute path of the audio file, it can also be http or rtsp URL:
Private Final StaticString TAG = "Mainactivity";PrivateMediaPlayer MediaPlayer =NewMediaPlayer (); @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Initmediaplayer ();}Private voidInitmediaplayer () {log.i (TAG,"Init Media Player"); Try{File File=NewFile (Environment.getexternalstoragedirectory (), "Test.mp3"); Mediaplayer.setdatasource (File.getpath ()); //specify the path to the audio fileMediaplayer.prepare ();//get MediaPlayer into the prep state .}Catch(Exception e) {e.printstacktrace (); }}
Permission to read the SD card file is involved:
<android:name= "Android.permission.READ_EXTERNAL_STORAGE"/>
Second, a series of operations on audio:
//Play Audio Public voidplaymusic (view view) {if(!mediaplayer.isplaying ()) {Mediaplayer.start ();//Start Playback }}//Pause Audio Public voidpausemusic (view view) {if(Mediaplayer.isplaying ()) {mediaplayer.pause ();//Pause Playback }}//Stop Audio Public voidstopmusic (view view) {if(Mediaplayer.isplaying ()) {mediaplayer.reset ();//Stop playingInitmediaplayer (); }
Iii. releasing resources in the OnDestroy method:
@Override protected void OnDestroy () { super. OnDestroy (); if NULL ) { mediaplayer.stop (); Mediaplayer.release (); }}
Iv.. layout file Activity_main.xml:
<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical"> <VideoviewAndroid:id= "@+id/video_view"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content" /> <LinearLayoutAndroid:layout_width= "Match_parent"Android:layout_height= "Match_parent"> <ButtonAndroid:layout_width= "0DP"Android:layout_height= "Wrap_content"Android:layout_weight= "1"Android:onclick= "PlayVideo"Android:text= "PlayVideo" /> <ButtonAndroid:layout_width= "0DP"Android:layout_height= "Wrap_content"Android:layout_weight= "1"Android:onclick= "Pausevideo"Android:text= "Pausevideo" /> <ButtonAndroid:layout_width= "0DP"Android:layout_height= "Wrap_content"Android:layout_weight= "1"Android:onclick= "Replyvideo"Android:text= "Replayvideo" /> </LinearLayout></LinearLayout>
View Code
Playback of video
First, the playback of the initial video:
Privatevideoview videoview; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_play_video); Videoview=(Videoview) Findviewbyid (R.id.video_view); Initvideopath ();}//initializing the playback filePrivate voidInitvideopath () {File file=NewFile (Environment.getexternalstoragedirectory (), "Test.mp4"); Videoview.setvideopath (File.getpath ()); //specify the path to the video file}
second, the video of a series of operations:
//Play Video Public voidPlayVideo (view view) {if(!videoview.isplaying ()) {Videoview.start ();//Start Playback }}//Pause Video Public voidpausevideo (view view) {if(Videoview.isplaying ()) {videoview.pause ();//temporarily play }}//re-play video Public voidreplyvideo (view view) {if(Videoview.isplaying ()) {videoview.resume ();//re-play }}
Iii. releasing resources in the OnDestroy method:
@Override protected void OnDestroy () { super. OnDestroy (); if NULL ) { videoview.suspend (); }}
Iv. files for the layout of the video: Activity_play_video.xml
<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical"> <VideoviewAndroid:id= "@+id/video_view"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content" /> <LinearLayoutAndroid:layout_width= "Match_parent"Android:layout_height= "Match_parent"> <ButtonAndroid:layout_width= "0DP"Android:layout_height= "Wrap_content"Android:layout_weight= "1"Android:onclick= "PlayVideo"Android:text= "PlayVideo" /> <ButtonAndroid:layout_width= "0DP"Android:layout_height= "Wrap_content"Android:layout_weight= "1"Android:onclick= "Pausevideo"Android:text= "Pausevideo" /> <ButtonAndroid:layout_width= "0DP"Android:layout_height= "Wrap_content"Android:layout_weight= "1"Android:onclick= "Replyvideo"Android:text= "Replayvideo" /> </LinearLayout></LinearLayout>
View Code
Friendship Link
Android Basic----> audio and video usage