Android-music player

Source: Internet
Author: User

Android-music player

1. What is Open Core?

Open CoreIt is the core of the Android multimedia framework. It is used for audio, video usage, playback, and other operations on all Android platforms.

 

2. Specific functions of Open Core

  • 1. multimedia file playback, download, including: 3GPP, MPEG-4, AAC and MP3 containers.
  • 2. Download and play streaming media files in real time, including 3GPP, HTTP, and RTSP/RTP.
  • 3. encoding and decoding of dynamic videos and static images, such as MPEG-4, H.263 and AVC (H.264), JPEG.
  • 4. Audio Encoding formats: MP3, AAC, and AAC +.
  • 5. video and image formats: 3GPP, MPEG-4 and JPEG.
  • 6. Video Conferencing: Based on H324-M standards.

    Open CoreIt is a multimedia framework. From a macro perspective, it mainly includes two aspects:

    • 1. PVPPlayer: Provides Media Player functions to play back Audio and Video streams.
    • 2. PVAuthor: supports recording media streams to capture various audio, video streams, and static images.

      3. Mediaplayer Introduction

      MediaPlayerClass can be used to play audio, video, and streaming media,MediaPlayerThe Audio and Video playback functions are included. On the Android interface, both the Music and Video applications callMediaPlayer. The following two figures are:MediaPlayerExamples of Android and Open Core:

       

      The following figure shows the MediaPlayer lifecycle.

      3.1 Introduction to common MediaPlayer Methods

      • Method: create (Contextcontext, Uriuri)
        Explanation: The static method creates a multimedia player through Uri.
      • Method: create (Contextcontext, int resid)
        Explanation: The static method creates a multimedia player using the resource ID.
      • Method: create (Contextcontext, Uriuri, SurfaceHolderholder)
        Explanation: The static method creates a multimedia player through Uri and the specified SurfaceHolder [abstract class ].
      • Method: getCurrentPosition ()
        Explanation: Int is returned to obtain the current playback position.
      • Method: getDuration ()
        Explanation: Int is returned to obtain the file time.
      • Method: getVideoHeight ()
        Explanation: Int is returned to get the video height.
      • Method: getVideoWidth ()
        Explanation: Int is returned to obtain the video width.
      • Method: isLooping ()
        Explanation: boolean is returned, indicating whether to play cyclically.
      • Method: isPlaying ()
        Explanation: boolean is returned, indicating whether the video is being played.
      • Method: pause ()
        Explanation: no return value. Pause.
      • Method: prepare ()
        Explanation: no return value. Prepare for synchronization.
      • Method: prepareAsync ()
        Explanation: no return value. Prepare asynchronous
      • Method: release ()
        Explanation: no return value. ReleaseMediaPlayerObject
      • Method: reset ()
        Explanation: no return value, resetMediaPlayerObject
      • Method: seekTo (int msec)
        Explanation: no return value indicates the playback position (in milliseconds)
      • Method: setAudioStreamType (int streamtype)
        Explanation: no return value indicates the streaming media type.
      • Method: setDataSource (Stringpath)
        Explanation: no return value. Set the multimedia data source (based on path]
      • Method: setDataSource (FileDescriptorfd, long offset, long length)
        Explanation: no return value. Set the multimedia data source according to FileDescriptor]
      • Method: setDataSource (FileDescriptorfd)
        Explanation: no return value. Set the multimedia data source according to FileDescriptor]
      • Method: setDataSource (Contextcontext, Uriuri)
        Explanation: no return value. Set the multimedia data source according to Uri]
      • Method: setDisplay (SurfaceHoldersh)
        Explanation: no return value. Set SurfaceHolder to display multimedia.
      • Method: setLooping (boolean looping)
        Explanation: no return value indicates whether to enable loop playback.
      • Event: setOnBufferingUpdateListener (MediaPlayer. OnBufferingUpdateListenerlistener)
        Explanation: listening events and network streaming media buffer listening
      • Event: setOnCompletionListener (MediaPlayer. OnCompletionListenerlistener)
        Explanation: Listener event, network Streaming Media Playback End listener
      • Event: setOnErrorListener (MediaPlayer. OnErrorListenerlistener)
        Explanation: listens to events and sets the error message listener.
      • Event: setOnVideoSizeChangedListener (MediaPlayer. OnVideoSizeChangedListenerlistener)
        Explanation: monitoring events and video size monitoring
      • Method: setScreenOnWhilePlaying (boolean screenOn)
        Explanation: no return value. Set whether to use SurfaceHolder for display.
      • Method: setVolume (float leftVolume, float rightVolume)
        Explanation: no return value. Set the volume.
      • Method: start ()
        Explanation: no return value. start playing.
      • Method: stop ()
        Explanation: no return value. Stop playing.

        4. demo of complete music playback

         

        Implemented functions:

        1. Pause/play, next/previous, and click a start to play

        2. Supports dragging progress bars to fast forward

        3. List sorting

        4. Stop playing when you call. Stop playing after you stop.

        5. Playback in the background

         

        Interface:

        Main. xml:

               
                
                 
                  
                  
                   
                   
                   
                     
                    
                  
                 
                
               


        Item. xml:

         

                 
                     
                  
                
                

         

        MainActivity:
        Import java. io. file; import java. util. arrayList; import java. util. collections; import java. util. hashMap; import java. util. list; import java. util. map; import Android. app. activity; import Android. content. broadcastReceiver; import Android. content. context; import Android. content. intent; import Android. content. intentFilter; import Android. media. mediaPlayer; import Android. media. mediaPlayer. onCompletionListener; import Android. OS. bundle; import Android. OS. environment; import Android. OS. handler; import Android. telephony. phoneStateListener; import Android. telephony. telephonyManager; import Android. view. view; import Android. widget. adapterView; import Android. widget. button; import Android. widget. listView; import Android. widget. seekBar; import Android. widget. simpleAdapter; import Android. widget. textView; import Android. widget. adapterView. onItemClickListener; import Android. widget. seekBar. onSeekBarChangeListener; public class MainActivity extends Activity {private TextView nameTextView; private SeekBar seekBar; private ListView; private List
               
                
        > Data; private int current; private MediaPlayer player; private Handler handler = new Handler (); private Button ppButton; private boolean isPause; private boolean isStartTrackingTouch; public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); nameTextView = (TextView) findViewById (R. id. name); seekBar = (SeekBar) findViewById (R. id. seekBar ); ListView = (ListView) findViewById (R. id. list); ppButton = (Button) findViewById (R. id. pp); // create a music player = new MediaPlayer (); // display the music playlist generateListView (); // progress bar listener seekBar. setOnSeekBarChangeListener (new MySeekBarListener (); // player listener player. setOnCompletionListener (new MyPlayerListener (); // intent filter IntentFilter filter = new IntentFilter (); // pause the stream play filter by phone. addAction ("Android. intent. action. NEW_OUTGOING_CALL "); registerReceiver (new PhoneListener (), filter); // create a telephone service TelephonyManager manager = (TelephonyManager) getSystemService (TELEPHONY_SERVICE); // listen to the phone status, stop playing manager when answering the call. listen (new MyPhoneStateListener (), PhoneStateListener. LISTEN_CALL_STATE);} private final class MyPhoneStateListener extends PhoneStateListener {public void onCallStateChanged (int state, String incomingNumber ){ Pause () ;}} private final class MyPlayerListener implements OnCompletionListener {// automatically play the next song public void onCompletion (MediaPlayer mp) {next ();}} public void next (View view) {next ();} public void previous (View view) {previous ();} private void previous () {current = current-1 <0? Data. size ()-1: current-1; play ();} private void next () {current = (current + 1) % data. size (); play ();} private final class MySeekBarListener implements OnSeekBarChangeListener {// mobile trigger public void onProgressChanged (SeekBar seekBar, int progress, boolean fromUser) {}// public void onStartTrackingTouch (SeekBar seekBar) {isStartTrackingTouch = true;} // public void onStopTrackingTouch (SeekBar seekBar) {player. seekTo (seekBar. getProgress (); isStartTrackingTouch = false;} private void generateListView () {List
                
                 
        List = new ArrayList
                 
                  
        (); // Obtain findAll (Environment. getExternalStorageDirectory (), list); // sort the playlist and collect the character order. sort (list); data = new ArrayList
                  
                   
        > (); For (File file: list) {Map
                   
                    
        Map = new HashMap
                    
                      (); Map. put ("name", file. getName (); map. put ("path", file. getAbsolutePath (); data. add (map);} SimpleAdapter adapter = new SimpleAdapter (this, data, R. layout. item, new String [] {"name"}, new int [] {R. id. mName}); listView. setAdapter (adapter); listView. setOnItemClickListener (new MyItemListener ();} private final class MyItemListener implements OnItemClickListener {public void onItemClick (AdapterView
                     Parent, View view, int position, long id) {current = position; play () ;}} private void play () {try {// replay player. reset (); // obtain the song path player. setDataSource (data. get (current ). get ("path"); // buffer player. prepare (); // start playing player. start (); // display the song name nameTextView. setText (data. get (current ). get ("name"); // set the length of the progress bar to seekBar. setMax (player. getDuration (); // playback button style ppButton. setText ("|"); // send a Runnable, after handler receives The run () method handler. post (new Runnable () {public void run () {// update progress bar status if (! IsStartTrackingTouch) seekBar. setProgress (player. getCurrentPosition (); // send handler again after 1 second. postDelayed (this, 1000) ;}}) ;}catch (Exception e) {e. printStackTrace () ;}} private void findAll (File file, List
                     
                       List) {File [] subFiles = file. listFiles (); if (subFiles! = Null) for (File subFile: subFiles) {if (subFile. isFile () & subFile. getName (). endsWith (". mp3 ") list. add (subFile); else if (subFile. isDirectory () // If the directory is findAll (subFile, list); // recursion} public void pp (View view) {// by default, if (! Player. isPlaying ()&&! IsPause) {play (); return;} Button button = (Button) view; // pause/play Button if ("| ". equals (button. getText () {pause (); button. setText ("? ");} Else {resume (); button. setText ("|") ;}} private void resume () {if (isPause) {player. start (); isPause = false ;}} private void pause () {if (player! = Null & player. isPlaying () {player. pause (); isPause = true ;}} private final class PhoneListener extends BroadcastReceiver {public void onReceive (Context context, Intent intent) {pause () ;}} protected void onResume () {super. onResume (); resume ();}}
                     
                    
                   
                  
                 
                
               


               
                  
                  
                  
                 
                   
                 
                 
                  
                  
                   
                  
                 
                
                

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.