Android Learning Note (1)----Playing music files

Source: Internet
Author: User

Original address: http://www.cnblogs.com/wynet/p/5526905.html

Here are two ways to play a resource file:

The first kind,

The Assets class resource is placed in the assets subdirectory of the project root, which holds some original files that can be organized in any way. These files will eventually be packaged in the APK file in the original way. If we want to access these files in the program, we need to specify the file name to access. Add the music files to the

Add the Play () method.

protected voidPlay () {Assetmanager am=getassets (); Try{Mpmediaplayer.setdatasource (AM.OPENFD ("First.mp3"). Getfiledescriptor ());            Mpmediaplayer.prepare ();        Mpmediaplayer.start (); } Catch(IllegalArgumentException e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(IllegalStateException e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }

The second kind,
Add the Raw folder under the Res folder, add music files under the raw folder, continue adding music files, add Paly method

protected void Play () {        mpmediaplayer=mediaplayer.create (this, r.raw.first);         Try {            mpmediaplayer.prepare ();         Catch (IllegalStateException e) {            e.printstacktrace ();         Catch (IOException e) {            e.printstacktrace ();        }        Mpmediaplayer.start ();    }

The resources under the Res/raw directory, like the assets class resources, are packaged in the APK file, but they are given a resource ID so that we can access them through the ID in the program.

To achieve repeated playback of music files:

// Play        Music repeatedly Mpmediaplayer.setoncompletionlistener (new  Mediaplayer.oncompletionlistener () {                          @Override              publicvoid  oncompletion (MediaPlayer arg0) {                  mpmediaplayer.start ();                  Mpmediaplayer.setlooping (true);                         }          

Android Learning Note (1)----Playing music files

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.