background music and sound effects of Android developmentOne: Add background music (MediaPlayer)
- MediaPlayer class can used to control playback of audio/video files and streams. The MediaPlayer class is used to control the playback of audio and video files;
- Methods for instantiating MediaPlayer:
- the direct new way: MediaPlayer MP = new MediaPlayer ();
- Use the Create method, such as: MediaPlayer MP = Mediaplayer.create (this, r.raw.test);//You don't have to call Setdatasource now.
- When you create an object using direct new, you set the method for the file you want to play: MediaPlayer The file to play consists of 3 main sources:
- Resource resources that the user has in advance in the application, such as MediaPlayer. CRE ate (This, R.raw. test);
- Media files stored under the SD card or other file path for example: Mymediaplayer.setdatasource ("/sdcard/test.mp3");
- Media files on the network for example: Mymediaplayer.setdatasource (http://music.baidu.com/song/490468);
- To start playback, stop playback, pause the playback method: be careful to choose a suitable time to start again after stopping and pausing to call the Seekto () method;
- The method for listening to playback is completed (the method of anonymous inner class is used here), which can be played after the play is finished, or it can be stopped:
- You must release the audio, or you will get an error:
II: Sound Effects (Soundpool)
- Soundpool
The Soundpool class manages and plays audio resources for applications. The Soundpool class is the class that Android uses to manage and play audio resources for an application. A Soundpool object can be thought of as a collection of samples that can be imported from an APK or loaded into a file system. It uses the MediaPlayer service to decode audio to an original 16-bit PCM stream. This feature allows the application to compress the stream without having to endure the CPU load and latency associated with extracting the audio.
-
Soundpool in the process of loading a sound file, a separate thread is used, with no impact on visual and UI performance. However, Soundpool has a limit on the size of the loaded sound file
- Soundpool compared with MediaPlayer, MediaPlayer has the disadvantage of high resource consumption, long delay time, not supporting multiple audio simultaneous playback, but Soundpool itself because of memory resource application has strict restrictions, so in the development process, It is recommended to use Soundpool to play some shorter sound clips or sound effects.
- Hashmap:hashmap is a non-synchronous implementation of the map interface based on the hash table , HashMap is actually a " chain table Hash" of the data structure, that is, the combination of arrays and linked lists. The function of HashMap is to quickly find "value" by "key" .
- Instantiate Soundpool, and then use HashMap to store the playing music ID: (This defines an array of Soundpool classes to store audio files)
- Soundpool[0].load (this, r.raw.m2, 0) The first parameter is the context, the 2nd parameter is the resource file, and the 3rd is the priority of the music;
- The first parameter in Spmap.put (0, Soundpool[0].load (this, r.raw.m2, 0)) is a key value, the value is found by key, and the second parameter is the value we want to store;
- AM to create a sound management Audiomanager object;
- (This is mainly for 2048 of the writing background music and sound effects), our app can have background music and button sound;
Background music and sound effects of Android development