Media Playback Guide ● http://developer.android.com/guide/topics/media/mediapl ayer.html
Media Playback Guide • http://developer.android.com/guide/topics/media/mediapl ayer.html 6
Media Player One of the most important components of the media framework is the MediaPlayer class.An object of this class can fetch,decode, and play both audio and video with minimal setup.It supports several different media sources such as: o Local resources o Internal URIs,such as one you might obtain from a Content Resolver o External URIs(streaming) Supported Media Formats are: o RTSP(RTP,SDP) o HTTP/HTTPS progressive streaming TECSUN 口oo口当 10360- o HTTP/HTTPS live streaming o 3GPP o MPEG-4 o MP3
Media Player • One of the most important components of the media framework is the MediaPlayer class. An object of this class can fetch, decode, and play both audio and video with minimal setup. It supports several different media sources such as: o Local resources o Internal URIs, such as one you might obtain from a Content Resolver o External URIs (streaming) • Supported Media Formats are: o RTSP (RTP, SDP) o HTTP/HTTPS progressive streaming o HTTP/HTTPS live streaming o 3GPP o MPEG-4 o MP3 7
Step 1:Create the raw folder in the project and put the media file into the folder Step 2:Configure the media player object ServiceAndMediaPlayer ·售sc to start the media playback com.example.serviceandmediaplayer gen [Generated Java Files] D☐Android4.4w /Create an instance of MediaPlayer and load the music Android Private Libraries MediaPlayermediaPlayerMediaPlayer.create(this, Android Dependencies 凸assets R.raw.music)j bin /Start the media playback D邑bs mediaPlayer.start(); ·是res drawable-hdpi drawable-ldpi drawable-mdpi To replay the media,call reset()and drawable-xhdpi drawable-xxhdpi prepare() raw To pause,call pause() music.mp3 values ·To stop,call stop() values-v11 evalues-v14 AndroidManifest.xml ic_launcher-web.png MediaPlayer class reference: clint.xml http://developer.android.com/reference/android/media/Media proguard-project.txt 目project.properties Player.html
8 // Create an instance of MediaPlayer and load the music MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.music); // Start the media playback mediaPlayer.start(); Step 1: Create the raw folder in the project and put the media file into the folder Step 2: Configure the media player object to start the media playback • To replay the media, call reset() and prepare() • To pause, call pause() • To stop, call stop() MediaPlayer class reference: http://developer.android.com/reference/android/media/Media Player.html
Media Player Whit this example you can play an audio file as a local raw resource from res/raw/directory: MediaPlayer mediaPlayer MediaPlayer.create(context,R.raw.sound_file_1); mediaPlayer.start ()/no need to call prepare;create(does that for you You can also load a local content through an URI Object Uri myUri =..../initialize Uri here MediaPlayer mediaPlayer new MediaPlayer(); mediaPlayer.setAudioStreamType(AudioManager.STREAMMUSIC; mediaPlayer.setDataSource (getApplicationContext(),myUri); mediaPlayer.prepare(); mediaPlayer.start(); You can also play a content from a remote URL via HTTP streaming. String url=“http:/∥..";/∥your URL here MediaPlayer mediaPlayer=new MediaPlayer(); mediaPlayer.setAudioStreamType (AudioManager.STREAMMUSIC); mediaPlayer.setDataSource(url); mediaPlayer.prepare(;/might take long!(for buffering,etc) mediaPlayer.start(); 9
Media Player • Whit this example you can play an audio file as a local raw resource from res/raw/ directory: • You can also load a local content through an URI Object • You can also play a content from a remote URL via HTTP streaming. 9 MediaPlayer mediaPlayer = MediaPlayer.create(context, R.raw.sound_file_1); mediaPlayer.start(); // no need to call prepare(); create() does that for you String url = "http://........"; // your URL here MediaPlayer mediaPlayer = new MediaPlayer(); mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); mediaPlayer.setDataSource(url); mediaPlayer.prepare(); // might take long! (for buffering, etc) mediaPlayer.start(); Uri myUri = ....; // initialize Uri here MediaPlayer mediaPlayer = new MediaPlayer(); mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); mediaPlayer.setDataSource(getApplicationContext(), myUri); mediaPlayer.prepare(); mediaPlayer.start();
Read Media Information void attachAuxEffect(int effectld) Attaches an auxiliary effect to the player. int getCurrentPosition( Gets the current playback position 060- int getDuration Gets the duration of the file. int getVideoHeight0 Returns the height of the video. int getVideoWidth 0 Returns the width of the video. boolean isLooping0 Checks whether the MediaPlayer is looping or non-looping. boolean isPlaying0 Checks whether the MediaPlayer is playing. 10
Read Media Information 10