In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly shows you "how to use android to achieve a simple music player", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to use android to achieve a simple music player" this article.
Preface
I had no choice but to write this music player. Because our Andoird course requires writing a music player. So there is this project. This project is relatively simple, to achieve the most basic music playback function, and then the interface is modeled on the style of NetEyun music, want to write the same, but the technology is not enough.
Basic music playback
Set up to play music files
Android plays media files probably using the class MediaPlayer. Before playing music, you must set something for this MediaPlayer to play. For file resources (music and video are considered as file resources), the only thing that can uniquely identify it is the address (path) of the file. We are using native music here, so we can use the file path.
Private static final MediaPlayer mediaPlayer = new MediaPlayer (); / * set playback file * @ param path * / public static void setPlayPath (String path) {try {/ / set type mediaPlayer.setAudioStreamType (AudioManager.STREAM_MUSIC); / / reset here (if you call this method after the music has been set, there will be no reset exception) mediaPlayer.reset (); mediaPlayer.setDataSource (path); / / set file source mediaPlayer.prepare () / / parsing file} catch (IOException e) {e.printStackTrace ();}}
Play music
After setting up the playback file, the next step is to play the music, pause, skip progress, and so on. Here we first introduce the self-contained method of MediaPlayer
MediaPlayer.isPlaying (); / / whether mediaPlayer.start () is being played; / / start playback, it will have no effect if it is already playing mediaPlayer.stop (); / / stop playback-cannot restart mediaPlayer.pause using start (); / / pause the current playback mediaPlayer.seekTo (0); / / move to a playback progress mediaPlayer.getCurrentPosition (); / / get the current playback progress
After understanding the above method, you can achieve almost all the music playback functions.
So to achieve the music playback function, you only need to call setPlayPath (path) and then call mediaPlayer.start () to play music successfully. The prerequisite is that you actually set the correct music path.
Get information about music
Music information can be obtained using the MediaMetadataRetriever class. It and MediaPlayer also need a file path to find the music.
Private static final MediaMetadataRetriever mmr = new MediaMetadataRetriever (); public static Song getSong (String path) {Song song = new Song (); / / self-defined song class song.setPath (path); / / get music-related information mmr.setDataSource (path); / / song name String title = mmr.extractMetadata (MediaMetadataRetriever.METADATA_KEY_TITLE); song.setTitle (title); / / album String album = mmr.extractMetadata (MediaMetadataRetriever.METADATA_KEY_ALBUM); song.setAlbum (album) / / (artist) singer String artist = mmr.extractMetadata (MediaMetadataRetriever.METADATA_KEY_ARTIST); song.setArtist (artist); / / Song length (in mm) int duration = Integer.parseInt (mmr.extractMetadata (MediaMetadataRetriever.METADATA_KEY_DURATION)); / / playback duration in milliseconds song.setDuration (duration); / / Song byte [] image = mmr.getEmbeddedPicture () / / Picture, which can be converted to bitmap Picture via BitmapFactory.decodeByteArray / / BitmapFactory.decodeByteArray (image, 0, image.length); song.setImage (image); return song;}
Other featur
Other functions are based on Android events and the above playback functions plus MediaMetadataRetriever access to music information to achieve.
The above is all the content of the article "how to use android to implement a simple music player". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.