Android single-host music player

Source: Internet
Author: User

MediaPlayer uses the following two static methods to load the specified audio:

1. static MediaPlayer create (Context context, Uri uri): loads audio files from the specified Uri and returns the newly created MediaPlayer object;

2. static MediaPlayer create (Context context, int resid): load the audio file from the resource file corresponding to the resid resource ID, and return the newly created MediaPlayer object.


Generally, if you want to load multiple files to play audio files cyclically, you can use the setDataSource () method of the MediaPlayer to load the specified audio file.

1. setDataSource (String path): Specifies the file represented by the path to be loaded.

2. setDataSource (FileDescriptor fd, long offset, long length): specifies the content of the file represented by fd loaded from the start of offset and length;

3. setDataSource (FileDescriptor fd): Specifies the file to be loaded.

4. setDataSource (Context context, Uri uri): Specifies the file represented by the uri.


MediaPlayer uses the following methods for playback control:

1. prepare (): prepare audio files. The actual loading of audio files is equivalent to caching

2. start (): start or resume playback.

3. stop (): stop playing

4. pause (): pause playback

5. reset (): reset to initial status

6. release (): The resources occupied by mp are released only when the MediaPlayer object is no longer used. It is not used after a song is completed, but is called after you stop using mp, such as exit

The cycle between tart and stop should be: reset () --> setDataSource (path) --> prepare () --> start () --> stop () -- reset () --> repeat the above process

Instance


<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHByZSBjbGFzcz0 = "brush: java;">

Package com. android. xiong. mediaplayers; import java. io. IOException; import java. util. date; import java. util. timer; import java. util. timerTask; import android. app. activity; import android. media. mediaPlayer; import android. media. mediaPlayer. onCompletionListener; import android. media. mediaPlayer. onErrorListener; import android. OS. bundle; import android. view. menu; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. seekBar; import android. widget. seekBar. onSeekBarChangeListener; public class MainActivity extends Activity {private Button start, stop, pre, reset; private SeekBar seekbar1; private MediaPlayer mediaPlayer; boolean isperson; int currentPlay = 0; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); start = (Button) findViewById (R. id. start); stop = (Button) findViewById (R. id. stop); pre = (Button) findViewById (R. id. pre); reset = (Button) findViewById (R. id. reset); seekbar1 = (SeekBar) findViewById (R. id. seekbar1); mediaPlayer = MediaPlayer. create (this, R. raw. hello); SeekBarChage seekbar = new SeekBarChage (); seekbar1.setOnSeekBarChangeListener (seekbar); // set the progress bar seekbar1.setMax (mediaPlayer. getDuration (); start. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {// determine whether the stream is playing if (mediaPlayer. isPlaying () {mediaPlayer. seekTo (0);} else {mediaPlayer = MediaPlayer. create (MainActivity. this, R. raw. hello); mediaPlayer. start ();} seekbar1.setProgress (0); // Update Progress new Timer (). schedule (new TimerTask () {@ Overridepublic void run () {if (isperson = true) return; seekbar1.setProgress (mediaPlayer. getCurrentPosition () ;}}, 0, 10) ;}}; reset. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {if (mediaPlayer. isPlaying () {mediaPlayer. seekTo (0);} else {mediaPlayer = MediaPlayer. create (MainActivity. this, R. raw. hello); mediaPlayer. start () ;}seekbar1.setprogress (0) ;}}); stop. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {mediaPlayer. stop () ;}}); pre. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {mediaPlayer. pause () ;}});} class SeekBarChage implements OnSeekBarChangeListener {// call this method when the drag bar changes @ Overridepublic void onProgressChanged (SeekBar seekBar, int progress, boolean fromUser) {}// call the call method @ Overridepublic void onStartTrackingTouch (SeekBar seekBar) {isperson = true ;} // call this method when the mouse is released @ Overridepublic void onStopTrackingTouch (SeekBar seekBar) {currentPlay = seekBar. getProgress (); mediaPlayer. seekTo (currentPlay); isperson = false ;}@overridepublic boolean onCreateOptionsMenu (Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater (). inflate (R. menu. main, menu); return true ;}}



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.