Android audio playback

Source: Internet
Author: User

In Android, audio playback is mainly implemented through the MediaPlayer class. It seems like he is mysterious, but it is actually very simple. Let's take a look at his true colors.

I. MediaPalyer Lifecycle

The figure below is found on the Internet. Here we only describe several special methods:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/100H94535-0.gif "title =" 521376_00004912556ibbi.gif "/>

1: After a MediaPlayer is created or the reset () method is called, it is in the IDLE state. After release is called, it is in the end state.

2: Any MediaPlayer must be in preparation before playing. The start method must be called to start playback.

3: When the MediaPlayer object is playing, you can pause and stop the object. The pause method is paused, and the stop method is stopped. A paused MediaPlayer can resume playing from the paused position through the start method. A paused MediaPlayer must be in the preparation status through prepare, call the start method to start playing the video.

Ii. Common Methods

1: Constructor

1) static MediaPlayer create (Context context, Uri uri): Creates a multimedia player through the audio specified by URI

2) static MediaPlayer create (Context context, int resId): Creates a multimedia player using the resource ID.

2: Get attributes

1) int getCurrentPosition (): obtains the current playback position.

2) int getDuration (): obtains the total duration of the playback file.

3) int getVideoheight (): obtains the video height.

4) int getVideoWidth (): Get the video width.

3: Status

1) boolean isLooping (): Indicates whether to play cyclically.

2) boolean isPlaying (): indicates whether the video is being played.

4: Set attributes

1) void seekTo (int mesc): sets the specified playback position in milliseconds)

2) void setDateSourece (String path): Set the source of multimedia data according to the path

3) void setDateSourece (FileDescriptor file): sets the multimedia data source based on FileDescriptor

4) void setDateSourece (Uri uri): sets the multimedia data source based on the URI.

5. Common events

1) void prepare (): prepare Resources

2) void start (): start playing

3) void pause (): pause playback

4) void stop (): stop playing

Iii. Audio Playback

According to the audio source, there are three types, namely, the local resource application), the internal URL points to the resource SDcard), and the external URL points to the Resource Network ). Here we only describe the first method.

◆ Local resources

1) create a file named raw in the res folder to store audio. Create MediaPlayer based on resources

MediaPlayer mediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.shijian);

2) Call the start method of MediaPlayer to play music.

mediaPlayer.start();

3) Result: Click start to play the music. There is no stop operation here, so do not try it easily)

4. Simple music player Creation

◆ Function: place three buttons on the page. You can click start to start playing. Click pause to pause playing. After you click start, start playing from the stopped place and click stop to stop playing.

◆ Specific implementation:

1) create a MediaPlayer object based on local resources

mediaPlayer = MediaPlayer.create(this, R.raw.shijian);

2) Add listening events for the three buttons

@ Override public void onClick (View arg0) {switch (arg0.getId () {case R. id. start: mediaPlayer. start (); // play music break; case R. id. pause: mediaPlayer. pause (); // pause playing break; case R. id. stop: // implement mediaPlayer for stopped playback. pause (); // pause mediaPlayer first. seekTo (0); // then set the actual position of the file to break; }}@ Override protected void onDestroy () {mediaPlayer. stop (); // when the application exits, all music stops playing super. onDestroy ();}

◆ Result: the Mediaplayer has several important periods in its lifecycle. You can try it yourself. 650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/100H96016-1.gif "/>



This article is from the "Schindler" blog, please be sure to keep this source http://cinderella7.blog.51cto.com/7607653/1293471

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.