Android MediaPlayer resume: androidmediaplayer

Source: Internet
Author: User

Android MediaPlayer resume: androidmediaplayer

Here are some simple MediaPlayer methods to help you use them in the future!

1) how to obtain the MediaPlayer instance: You can use the new method directly: MediaPlayer mp = new MediaPlayer (); you can also use the create method, for example, MediaPlayer mp = MediaPlayer. create (this, R. raw. test); // you do not need to call setDataSource now. 2) how to set the file to be played: the file to be played by the MediaPlayer mainly includes three sources:. the resource that comes with the user in the application, for example, MediaPlayer. create (this, R. raw. test); B. media files stored in SD cards or other file paths, such as mp. setDataSource ("/sdcard/testcard"); c. media files on the network, such as mp. setDataSource ("http://www.citynorth.cn/music/confucius.mp3"); setDataSource of MediaPlayer has four methods: setDataSource (String path) setDataSource (FileDescriptor fd) setDataSource (Context context, Uri uri) setDataSource (FileDescriptor fd, long offset, long length) When FileDescriptor is used, the file must be placed at the same level as the res folder. AssetsFolder, and then use: AssetFileDescriptor fileDescriptor = getAssets (). openFd ("rainsets ");
M_mediaPlayer.setDataSource (fileDescriptor. getFileDescriptor (), fileDescriptor. getStartOffset (), fileDescriptor. getLength (); To set datasource 3) Main Control Method for the player: Android controls the playback of media files by controlling the player status, where: prepare () and prepareAsync () provide synchronous and asynchronous ways to set the player to enter the prepare status. Note that if the MediaPlayer instance is created by the create method, therefore, you do not need to call prepare () before starting the playback for the first time, because prepare () has been called in the create method. Start () is the method for truly starting file playback. pause () and stop () are relatively simple and play the role of pausing and stopping playback,
SeekTo () is a positioning method that allows the player to start playing from the specified position. It should be noted that this method is an Asynchronous Method. That is to say, when this method is returned, it does not mean that the positioning is complete, in particular, the OnSeekComplete is triggered when the network file is located. onSeekComplete (). If needed, you can call setOnSeekCompleteListener (OnSeekCompleteListener) to set the listener for processing. Release () can release the resources occupied by the player. Once it is determined that the player is no longer used, it should be called to release the resources as soon as possible. Reset () enables the player to recover from the Error state and then return to the Idle state. 4) player monitoring method: mMediaPlayer. setOnCompletionListener () // call this method when the playback ends. setOnPreparedListener () // when calling mMediaPlayer. this listener is triggered when the prepareAsync () method is used to register a callback function and call the mMediaPlayer when the media source is preparing for playback. setOnErrorListener () // when an error occurs during the asynchronous operation, register a callback function to call it. If the playback fails, call this method mMediaPlayer. setOnSeekCompleteListener () // This method is used when you call MediaPlayer. when seekTo () locates where to start playing, this method is called back when the video is successfully located.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.