这是我看到的讲解很细致的国外教程。 现在转过来,希望对大家有所帮助。 Androidsupport Media API as optional APIs. The media APIs providefunctionality of playing and recording audio files and video files. Intoday’s post we will see how to play and record audio files. Playing Audio File: Androidhas defined a MediaPlayer class for playing media file. This class canbe used to play the audio files. The class provides static methods tocreate the instance of the media player. Here is how the MediaPlayerobject can be obtained using create methods and Thefirst create methods gets the Uri for the audio file and can be used toplay files present on the phone sdcard. The second create method usethe application resource file song_file.mp3. The song_file.mp3 file isadded as a raw resource under res/raw directory. Notethat if the MediaPlayer object does not get created successfully, thecreate methods will return null so it will be better to check for thereturn object for null value to avoid NullPointer exception. TheMediaPlayer object can also be obtained by instantiating usingMediaPlayer constructor. The object created like this has to beinitialized with the file that needs to be played. setDataSource()method can be used to initialize the object with the audio file. TheMediaPlayer object has to be prepared before the player can startplaying the song. The prepare() method is the blocking method andblocks until the media player is ready to play the song. One nonblocking method prepareAsync() is also provided. The non blockingprepare method should be used in case media player is used to play asong from a stream and need to buffer data before song can be played.The create methods that we saw earlier call the prepare() method andapplication must not call it again. To start file playback use start()method. This is how you play a file: mediaPlayer.start(); Thefile playback can be pause with pause() method, and can be resumes withstart method. The file playback can be stopped with stop() method. Themedia player object has to reset before it can be setup for some othersong file. The media player object has to be released after its use. AndroidMedia API defines some listeners like OnCompletionListener,OnPrepareListener, OnErrorListener, OnBufferingUpdateListener OnCompletionListenerevent gets fired when media player complete the playing of the currentaudio file. You can use this listener event for playing next song fromthe list or releasing the media player object. OnPrepareListeneris needed when prepareAsync method is used. Since the prepareAsyncmethod does not block for the call duration, the application can notstart playing the song. The onPrepared() event function will be calledonce the media player is ready. You can start playing the song in theonPrepared() method. OnErrorListeneris also used in case of asynchronous operations. If any error occurs inasynchronous operation, onError() method of this listener will becalled. Recording Audio Files: TheMedia API also provides functionality to record audio files. TheMediaRecorder class can be used for media recording. The MediaRecorderhas to be initialized with the Audio Encoder, output format (codec tobe used for recorded file), Audio Source (like Microphone) and outputfile path where the recorded file will be stored. Here is how recordingcan be done: Thiswill start the recording the recording can be stored with stop()method. The MediaRecorder object also needs to be release after its use. That all for today’s post. We will see Media APIs Video support in some other post. |
android教程之MediaPlayer和MediaRecorder
最新推荐文章于 2023-04-03 21:40:24 发布