[Android, 16] android music player and sound pool

Source: Internet
Author: User

I. Develop a music player:

1. UI file of the music player: a text box that can enter the path of the music file to be played, and four buttons:

 

Android: layout_width = "fill_parent"

Android: layout_height = "fill_parent"

>

 

Android: layout_width = "fill_parent"

Android: layout_height = "wrap_content"

>

Android: textSize = "20sp"/>

 

Android: id = "@ + id/et_path"

Android: layout_width = "fill_parent"

Android: layout_height = "wrap_content"

>

 

// When the interface is opened, the text box gets the focus

 

 

Android: layout_width = "fill_parent"

>

Android: gravity = "center_horizontal" // horizontally centered display of the specified component

Android: orientation = "horizontal">

 

Android: id = "@ + id/play"

Android: layout_width = "0dip" // set the button to 0 in width.

Android: layout_height = "wrap_content"

Android: layout_weight = "1" // set the button weight to 1

Android: text = "play"/>

 

Android: id = "@ + id/pause"

Android: layout_width = "0dip"

Android: layout_height = "wrap_content"

>

Android: text = "paused"/>

 

Android: id = "@ + id/replay"

Android: layout_width = "0dip"

Android: layout_height = "wrap_content"

Android: layout_weight = "1"

>

 

Android: id = "@ + id/stop"

Android: layout_width = "0dip"

Android: layout_height = "wrap_content"

Android: layout_weight = "1"

Android: text = "stop"/>

 

 

 

2. Player playback, replay, pause, and stop operations.

① Playback:

// Create a MediaPlayer object

MediaPlayer>

// Set the playback type

MPlayer. setAudioStreamType (AudioManager. STREAM_MUSIC );

// Set the playback file path

MPlayer. setDataSource (path );

// Note: very important

MPlayer. prepare (); // call the underlying c code to complete>

}

});

// Start playing:

MPlayer. start ();

② Pause:

// MPlayer. isPlaying () determines whether the video is being played.

If (mPlayer! =>

>

}

/Create a MediaPlayer object

MediaPlayer>

// Set the playback type

MPlayer. setAudioStreamType (AudioManager. STREAM_MUSIC );

// Set the playback file path

MPlayer. setDataSource (path );

// Note: very important

MPlayer. prepare (); // call the underlying c code to complete>

}

});

// Start playing:

MPlayer. start ();

④ Stop:

If (mPlayer! =>

}

 

2. display the lyrics with music:

1. parse the lyrics file. lrc file first.

 

① The style of the lyrics file is:

[. 93] Love in the village

[. 36] The back of the river

[. 53] cuddling in a small village

[. 82] Flowers in the blue sky

② Parse the. lrc file, read the lyrics in one row, parse the time and lyrics, use the time as the key, and the corresponding lyrics as the value, and place them in the map set.

Map>

String>

>

>

String>

SimpleDateFormat>

Date>

System. out. println (>

Map. put (date, value );

 

2. perform the following operations to display the lyrics during playback:

Public>

 

>

// Handler used to modify the lyrics

>

 

@ Override

>

// Take the time as the key and display the lyrics in the map:

}

 

};

@ Override

>

>

>

>

>

 

>

>

>

>

>

>

>

}

 

>

>

>

}

 

>

>

}

 

Play (path );

 

Break;

 

}

 

}

 

Private void play (String path ){

Try {

>

>

}

});

 

MPlayer. start ();

>

//>

// Load the audio file into the sound pool; return the id

// Method parameter 1: Context

Parameter 2: Specifies the Audio Resource file

Parameter 3:

Int>

 

}

// Click the fire button:

Public void fire (View view ){

// Play the sound in the sound pool: play () method

Method parameter 1: Sound id

Parameter 2: Left-channel size,

Parameter 3: Right-channel sound size,

Parameter 4: specify the priority. The value 0 indicates the lowest priority.

Parameter 5: Specifies the playing mode of the sound. The value 0 indicates playing only once, and-1 indicates permanent loop.

Parameter 6: Specifies the playback speed.

Soundpool. play (soundid, 1.0f, 1.0f, 0, 0, 1.0f );

}

 

③ The Call of the play () method is asynchronous.

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.