Android ring tones mediaplayer

Source: Internet
Author: User
Preface I have been studying Android for a while. In order to further understand how Android applications are designed and developed, I decided to study several open-source Android applications in detail. From some open-source applications to absorb something, while accumulating the amount, while exploring Android learning and research directions. Here, I first selected the jwood standup timer project. This article will sort out the study content notes and create an index list. Mediaplayer uses the mediaplayer class for specific operations. In Android, mediaplayer is mainly used to control Multimedia: Audio/Video audio and video. For details about mediaplayer, refer to the SDK instructions and usage. Mediaplayer has different States. Status transition is achieved by creating a mediaplayer instance or calling various methods. Note that in some states, some method members cannot be called. Otherwise, an exception is thrown.
  1. After a mediaplayer instance is created or the reset () method is reset, It is idle in the idle (?) Status. In this status, most mediaplayer methods cannot be called. For example:Getcurrentposition (),Getduration (),Getvideoheight (),Getvideowidth (),Setaudiostreamtype (INT),Setlooping (Boolean),Setvolume (float, float),Pause (),Start (),Stop (),Seekto (INT),Prepare ()OrPrepareasync ().An exception is thrown if a call is made.
  2. Once the release () method is called, The mediaplayer is in the end state.
  3. You can use setdatasource () to set the mediaplayer data source to initialize the mediaplayer instance.
  4. After initialization, The mediaplayer must be in the perpared (ready) status to start playing, pausing, and stopping. There are two ways to enter the preparation status: SynchronizationPrepare ()Or asynchronousPrepareasync () 
  5. After entering the perpared status, call the START () method to enter the started playback status. You can call the corresponding method during the playback process to enter the paused and stopped statuses, and switch between them through the START (), pause (), and stop () methods (note that in the stopped status, it cannot directly enter the started status, but can be indirectly entered through the prepared status ).
  6. If the resource file playback is complete, mediaplayer automatically enters playbackcompleted.
In standup timer, you only use mediaplayer for simple ringtone reminders, And the encoding is relatively simple. In addition, I wrote an example about mediaplayer, which is a simple music player, musicplayer (if you are interested, you can browse it ). Before encoding the resource file, import the audio file to the res/raw folder. Initialize mediaplayer declaration: Private Static Mediaplayer bell = Null ;
Private Static Mediaplayer Airhorn = Null ;

 

Generate an instance (in oncreate of activity)

Code

Private Void Initializesounds (){
If (Bell = Null )
{
Logger. D ( " Loading the bell sound " ); // Use this method to create a mediaplayer. The prepare () has been called and the mediaplayer enters the prepared state.
Bell = Mediaplayer. Create ( This , R. Raw. Bell );
}
If (Airhorn = Null )
{
Logger. D ( " Loading the Airhorn sound " );
Airhorn = Mediaplayer. Create ( This , R. Raw. Airhorn );
}

}

 

The timer is used in the standup timer. When the timer reaches a certain time, the mediaplayer is triggered to play the prepared ringtone. Timer Code

Protected Synchronized Void Updatetimervalues ()
{
Currentindividualstatusseconds ++ ;
If (Remainingindividualseconds > 0 )
{
Remainingindividualseconds -- ;
If (Remainingindividualseconds = Warningtime)
{
Logger. D ( " Playing the bell sound " );
If (Settingactivity. playsounds ( This )){
// Play a warning ringtone if it is equal to the set warning time and allows warning
Playwarningsound ();
} // If
} Else {
If (Remainingindividualseconds = 0 ){
Logger. D ( " Playing the Airhorn sound " );
If (Settingactivity. playsounds ( This )){
// If the time is equal to zero, you can switch to enable the ringtone notification.
Playfinishedsound ();
} // If
} // If
} // Else
} // If Playing ringtones

Private Void Playwarningsound (){
Playsound (Bell );

}
ProtectedVoidPlayfinishedsound (){
Playsound (Airhorn );
}
PrivateVoidPlaysound (mediaplayer MP ){
MP. seekto (0);
MP. Start ();
}

 

Remember to recycle resources when releasing resources using mediaplayer Private Void Destroysounds (){
Bell. Stop ();
Bell. Release ();
Bell = Null ;

Airhorn. Stop ();
Airhorn. Release ();
Airhorn=Null;

}

 

References mediaplayer audio and video series Indexes
Android open-source project-standuptimer learning notes Index
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.