Brief Introduction to various attributes and methods of Android mediaplayer

Source: Internet
Author: User

Main involved classes: mediaplayer
(1) When a mediaplayer object is created or the reset () method is called, it is idle and ended after the release () method is called.
1. After a mediaplayer object calls the reset () method, calling other methods may trigger the onerrorlistener. onerror () event. If the reset () method is not called
2. When the mediaplayer object is no longer in use, it is best to call the release () method to release it so that it is in the end state. At this time, it cannot be used.
3. When a mediaplayer object is created (the constructor is called), it is idle. If the CREATE () method is used, it is in the preparation status.
(2) In general, some common playback control operations may be caused by the unsupported audio and video formats, poor quality, and streaming timeout, it is also possible that the mediaplayer object is invalid due to developer negligence, which leads to errors. In this case, you can register the setonerrorlistener method for monitoring. If an error occurs, the mediaplayer object is in the Multi-fog state. You can use the reset () method to reply to the error.
(3) Any mediaplayer object must be in preparation before playing.
(4) to start playing the mediaplayer object, you must call the START () method. You can use the isplaying () method to check whether the object is playing.
(5) When a mediaplayer object is playing, it can be paused and stopped. Pause () and stop () methods can be used to stop playing. You can use the START () method to resume playing when paused. However, you must first call the prepare () method to make it ready before calling the START () method.
Main Methods:
Mediaplayer: Constructor
Create: create a multimedia file to be played.
Getcurrentposition: obtains the current playback position.
Getduration: Get the object time
Prepare: Prepare (synchronize)
Prepareasync: preparation (asynchronous)
Seekto: Specifies the playback position (in milliseconds)
Setaudiostreamtype: Set the streaming media type
Setdatasource: Set the data source
Setdisplay: Set surfaceholder to display multimedia
Setonbufferingupdatelistener: Network streaming media buffer listener
Setonerrorlistener: Set an error message listener
Setonvideosizechangedlistener: Specifies the video size listener.
Setscreenonwhileplaying: sets whether to use surfaceholder for display.
Setvolume: Set the volume
// Obtain the audio file on the SD card
Setdatasource ("/sdcard/testcard ");
// Load the music in the Resource
Mediaplayer. Create (activity01.this, R. Raw. test );
// Currently, the audio file cannot be parsed cyclically.
Cause:. android_secure folder is protected and file information cannot be obtained.
Play video
Related class: videoview
Method description:
Getbufferpercentage: Percentage of buffer obtained
Getcurrentposition: obtains the current playback position.
Getduration: get the video file time
Resolveadjustedsize: Adjust the video display size
Setmediacontroller: sets the playback controller mode (playback progress bar)
Setoncompletionlistener: triggers an event when the video file is played.
Setvideopath: Set the Video Source Path
Setvideouri: sets the video source address.
Recording
Related class: mediarecorder
Method description:
Mediarecorder: Constructor
Getmaxamplitude: get the maximum range
Setaudioencoder: sets the audio encoding.
Setaudiosource: Set the audio source
Setcamera: set the camera
Setmaxduration: set the maximum recording time
Setmaxfilesize: sets the maximum size of a file.
Setoutputfile: Set the output file
Setoutputformat: Set the output file format
Setpreviewdisplay: sets Preview
Setvideoencoder: sets the video encoding.
Setvideoframerate: sets the video frame frequency.
Setvideosize: Set the video width and height (resolution)
Setvideosource: sets the video source.

Methods In the file class:
Public static file createtempfile (string prefix, string suffix, file directory)
Creates an empty temporary file in the given directory using the given prefix and suffix as part of the file name.
The system automatically adds numbers between prefix and suffix to build the complete file name.

General steps for recording:
1. instantiate mediarecorder MR and call the constructor.
2. initialize Mr: mr. setaudiosource (MIC)/setvideosource (CAMERA)
3. Configure datasource: Set the output file format/path, encoder, etc.
4. Prepare for recording: mr. Prepare ()
5. Start recording: mr. Start ()
6. Stop recording: mr. Stop ()
7. release resources: mr. Release ()
Note: Sequence 2 and 3 cannot be changed
Add license:
<Uses-Permission Android: Name = "android. Permission. write_external_storage"/>
<Uses-Permission Android: Name = "android. Permission. record_audio">
Camera settings
Related class: camera, which is used to connect to and disconnect the camera service.
Camera events:
Camera. autofocuscallback: Auto Focus Function
Camera. errorcallback: capture error messages
Camera. parameters: Camera PROPERTY PARAMETER
Camera. picturecallback: triggered when an image is taken or generated
Camera. previewcallback: Camera preview settings
Camera. shuttercallback: shutter settings
Camera. Size: Image Size
The camera class has no constructor. You can use the open () method to open the camera device.
Method Introduction of the camera class:
Autofocus: Set auto focus
Getparameters: Get the camera parameters
Open: Start the camera service
Release: releases the camera service.
Setparameters: set parameters
Setpreviewdisplay: sets Preview
Startpreview: Start previewing
Stoppreview: Stop Preview
Takepicture: Take a photo
Note: The takepicture method must implement three callback functions as its three parameters: Camera. shuttercallback (shutter) and two camera. Picture. Callback (image data ).
License required
<Uses-Permission Android: Name = "android. Permission. Camera"/>
To store images to the SD card, you need the SD card read/write permission.

There is a problem: You can only take a photo once and cannot return to the preview page.

Alarm settings
Related class: alarmmanager, which is used to set the event to be completed at a specified time. Alarmmanager provides a service for accessing System alerts. As long as the alert service is set in the program, alarmmanager executes these events using the onreceive () method, even if the system is in STANDBY state, the operation will not be affected. You can use the context. getsystemservice (alarm_service) method to obtain the service.
Method description:
Cancel: cancels the alarmmanager service.
Set: Set the alarmmanager Service
Setinexactrepeating: sets an inaccurate period.
Setrepeating: set the exact cycle
Settimezone: set the time zone
Note: You must create a subclass of broadcastreceiver and overwrite the onreceive () method.
Ringtone settings
The built-in ringtones are stored in the/system/Medio/Audio/folder.
Ring tone type: type_ringtone, type_alarm, type_notification
Related class: ringtonemanager
Method Introduction:
Getactualdefaultringtoneuri: Get the specified type of ringtone
Getcursor: returns the cursor of all available ringtones.
Getdefaulttype: gets the default ringtone type of the specified Uri.
Getringtone
Getringtoneposition: Get the ringtone position
Getringtoneuri
Getvalidringtoneuri: Get the URI of an available ringtone
Isdefault: Get whether the specified URI is the default ringtone
Setactualdefaultringtoneuri: Set the default ringtone

The obtained cursor has four columns. The column names are: _ id, title, "content: // media/Internal/Audio/Media", title_key

The following example uses phone ringtones:
If (isfolder (strringtongfolder) {// if this folder does not exist, create
// Enable system ringtone settings
Intent intent = new intent (
Ringtonemanager. action_ringtone_picker );
Intent. putextra (ringtonemanager. extra_ringtone_picked_uri, true );
// Type: Incoming call ringtong
Intent. putextra (ringtonemanager. extra_ringtone_type,
Ringtonemanager. type_ringtone );
// Set the displayed question
Intent. putextra (ringtonemanager. extra_ringtone_title, "set the ringtone of the incoming call ");
// Return to the current activity after setting.
Startactivityforresult (intent, ringtongbutton );
}

Then rewrite the onactivityresult (INT requestcode, int resultcode, intent data) method. resultcode is to click the number of the Set dialog button. You need to determine whether the confirmation button is clicked.

If (resultcode! = Result_ OK ){
Return;
}
Try {
// Get the selected ringtone
Uri pickeduri = data. getparcelableextra (ringtonemanager. extra_ringtone_picked_uri );
// Select the selected ringtone as the default
If (pickeduri! = NULL ){
Ringtonemanager. setactualdefaultringtoneuri (
Media_ringtongactivity.this,
Ringtonemanager. type_ringtone, pickeduri );
}
} Catch (exception e ){
E. printstacktrace ();
}

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.