Create a custom audio playback application

Source: Internet
Author: User

Android contains a mediaplayer class that can be used to play and control audio and video. Now we use the audio function. Mediaplayer can be used to control the audio/video files and streams played.

The simplest mediaplayer example is whether an audio file can be packaged with an application, that is, the audio file can be placed in the original resource. Therefore, we create a new file named raw in the res folder of the project and add the audio file.

1. Start the Media Player

It is very easy to create a meidaplayer for a bit audio file. You can use the static method create to instantiate a mediaplayer object and pass in this (the activity will be derived from it) as the context and ziyuanid generated by the audio file.

Mediaplayer = mediaplayer. Create (this. R. Raw. androidmp3 );

Of course, you can also use the constructor to instantiate a mediaplayer object: mediaplayer = new mediaplayer ();

Then you can use mediaplayer. Start (); to play the file.

2. Control playback

The mediaplayer class has multiple Nested classes, which are excuses for listening to events sent by medaiplayer. These events are related to status changes. For example, mediaplayer calls the oncompletion Method for classes that implement oncompletionlistener and are registered through setoncompletionlistener. This operation is executed when the audio file is being played. The following is a complete example. This activity will repeat infinitely and use oncompletionlitener to play the same audio file. Initialize the mediaplayer object in the onstart method and start playing. Stop playing and release resources in the onstop method.

Package com. example. musicdemo;
Import Android. Media. mediaplayer;
Import Android. Media. mediaplayer. oncompletionlistener;
Import Android. OS. Bundle;
Import Android. App. activity;
Import Android. View. Menu;
Import Android. View. menuitem;
Import Android. Support. v4.app. navutils;

Public class mainactivity extends activity implements oncompletionlistener {
Mediaplayer mediaplay;

@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. activity_main );

}

@ Override
Protected void onstart (){
Super. onstart ();
Mediaplay = mediaplayer. Create (this, R. Raw. Android );
Mediaplay. setoncompletionlistener (this );
Mediaplay. Start ();
}

@ Override
Protected void onstop (){
Super. onstop ();
Mediaplay. Stop ();
Mediaplay. Release ();
}

Public void oncompletion (mediaplayer MP ){
Mediaplay. Start ();
}

}

Of course, this can be done without oncompletionlistener. You only need to set the mediaplayer to loop playback by using the setlooping (true) method.

Mediaplayer status chart

Playback controls the management of audio/video files and streams as a state machine. The following figure shows the lifecycle and a mediaplayer object that supports playback control. An ellipse indicates that the State may reside in a mediaplayer object. An arc represents a playback control operation that drives object state conversion. There are two types of arcs. An arc arrow represents the synchronous method call header, and those with double arrows represent the asynchronous method call owner.

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.