[Android 14] -- Multimedia 1: playing music

Source: Internet
Author: User

 Disclaimer: The book "Secrets of Android Application Development", which records the logs of the book, references the relevant code and summary, and has no commercial use, it is completely a record of self-learning, and many problems will inevitably occur in learning just now. If there are any mistakes, please criticize them a lot.

Today, I started to learn about multimedia operations, because part of our smart phones play an important role in audio, video, and image display operations. Android uses open core as the core of the Multimedia Framework and is also called pv. It supports audio and video in multiple formats. The open core framework consists of two parts:

(1) pvplayer: Provides the media player function.

(2) pvauthor: supports recording media streams to capture audio, video streams, and images.

Of course, we will not directly operate on the underlying PV during development, mainly using the media API provided by Android.

 

1. mediaplayer class

This class is mainly used for playing, audio, video, and streaming media. It includes the audio and video playing functions. For details about the lifecycle and related methods, refer to the following music playing example.

2. mediarecorder class

This class is mainly used for media sampling, including audio and video. To run mediarecorder as a state machine, you must set different parameters, such as the source device and format. After setting, you can execute recording for any time until the user stops.

 

I. Multimedia music playing

The following uses an example of playing music to verify the life cycle and related methods of the mediaplayer. The example has been implemented before. At that time, MP3 files were obtained in the raw directory of playing music, in this example, MP3 files under sdcard are automatically played.

Key source code:

Public class sdcardmusicplayactivity extends listactivity {private imagebutton mfrontimagebutton = NULL; private imagebuttonmstopimagebutton = NULL; private region = NULL; Public mediaplayer mmediaplayer = NULL; // mediaplayer object private list <string> mmusiclist = new arraylist <string> (); // playback list private int currentlistitem = 0; // index of the currently played song Private Static final string music_path = new string ("/mnt/sdcard/"); // music path @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); musiclist (); // update the display playlist. mmediaplayer = new mediaplayer (); mfrontimagebutton = (imagebutton) findviewbyid (R. id. lastimagebutton); mstopimagebutton = (imagebutton) findviewbyid (R. id. stopimagebutton); mstartimagebutton = (imagebutton) findviewbyid (R. id. startimagebutton); mpauseimagebutton = (imagebutton) findviewbyid (R. id. pauseimagebutton); mnextimagebutton = (imagebutton) findviewbyid (R. id. nextimagebutton); // the stop button mstopimagebutton. setonclicklistener (New imagebutton. onclicklistener () {public void onclick (view v) {If (mmediaplayer. isplaying () {// whether the current video is playing the mmediaplayer. reset (); // reset mediaplayer to initial status }}); // start button mstartimagebutton. setonclicklistener (New imagebutton. onclicklistener () {public void onclick (view v) {playmusic (music_path + mmusiclist. get (currentlistitem) ;}}); // pause the button mpauseimagebutton. setonclicklistener (New imagebutton. onclicklistener () {public void onclick (view v) {If (mmediaplayer. isplaying () {mmediaplayer. pause ();} else {mmediaplayer. start () ;}}); // The next mnextimagebutton. setonclicklistener (New imagebutton. onclicklistener () {public void onclick (view arg0) {nextmusic () ;}}); // The Last mfrontimagebutton. setonclicklistener (New imagebutton. onclicklistener () {public void onclick (view arg0) {frontmusic () ;}}) ;}// playlist private void musiclist () {// get the file at the specified position, set file home = new file (music_path); If (home. list (New musicfilter ()). length> 0) {for (File file: Home. listfiles (New musicfilter () {mmusiclist. add (file. getname ();} arrayadapter <string> musiclistadapter = new arrayadapter <string> (sdcardmusicplayactivity. this, R. layout. musicitme, mmusiclist); this. setlistadapter (musiclistadapter);} private void playmusic (string path) {try {mmediaplayer. reset (); // reset mediaplayermmediaplayer. setdatasource (PATH); // set the file path to be played to mmediaplayer. prepare (); // prepare to play the media player. start (); mmediaplayer. setoncompletionlistener (New oncompletionlistener () {public void oncompletion (mediaplayer MP) {// todo auto-generated method stubnextmusic (); // perform the next one after the first stream is played }});} catch (ioexception e) {// todo: handle exception} // The next private void nextmusic () {// todo auto-generated method stubif (++ currentlistitem> = mmusiclist. size () {currentlistitem = 0;} else {playmusic (music_path + mmusiclist. get (currentlistitem) ;}}// previous private void frontmusic () {If (-- currentlistitem >=0) {currentlistitem = mmusiclist. size ();} else {playmusic (music_path + mmusiclist. get (currentlistitem) ;}} public Boolean onkeydown (INT keycode, keyevent event) {If (keycode = keyevent. keycode_back) {mmediaplayer. stop (); mmediaplayer. release (); this. finish (); Return true;} return Super. onkeydown (keycode, event);} // play the clicked music protected void onlistitemclick (listview L, view V, int position, long ID) {currentlistitem = position; playmusic (music_path + mmusiclist. get (position) ;}} class musicfilter implements filenamefilter {@ overridepublic Boolean accept (File Dir, string filename) {// you can set the return (filename. endswith (". MP3 "));}}

Running effect:

[Problem]

When an MP3 file is placed in the sdcard of the simulator, the following error occurs: failed to push Selection: Read-Only file system.

Switch to the ddms view, and you can see that the Operation permission of the sdcard folder under MNT is readable. Later, when AVD edit was used, the size of the sdcard was not set before, so you set the size and restart AVD, the sdcard folder can be edited.

 

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.