In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
Most people do not understand the knowledge of this article "how to play audio in Android", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to play audio in Android" article.
Playing audio files in Android is generally realized by using MediaPlayer class, which provides a very comprehensive control method for audio files in various formats, which makes the work of playing music very simple. The following table lists some of the more common control methods in the MediaPlayer class.
Method name
Function description
SetDataSource ()
Sets the location of the audio file to play.
Prepare ()
Call this method to complete the preparation before starting the playback.
Start ()
Start or continue playing audio.
Pause ()
Pause audio playback.
Reset ()
Reset the MediaPlayer object to the state you just created.
SeekTo ()
Starts playing audio from the specified location.
Stop ()
Stop playing audio. The MediaPlayer object after calling this method can no longer play audio.
Release ()
Release the resources associated with the MediaPlayer object.
IsPlaying ()
Determine if the current MediaPlayer is playing audio.
GetDuration ()
Gets the length of the loaded audio file.
After a brief understanding of the above methods, let's take a look at the workflow of MediaPlayer. First, you need to create a MediaPlayer object, then call the setDataSource () method to set the path of the audio file, then call the prepare () method to make the MediaPlayer ready, then call the start () method to start playing the audio, call the pause () method to pause the playback, and call the reset () method to stop the playback. Let's learn from a specific example, create a new PlayAudioTest project, and then modify the code in activity_main.xml, as shown below:
Three buttons are placed horizontally in the layout file to play, pause, and stop the audio file. Then modify the code in MainActivity as follows:
Public class MainActivity extends Activity implements OnClickListener {private Button play;private Button pause;private Button stop;private MediaPlayer mediaPlayer = new MediaPlayer (); @ Overrideprotected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_main); play = (Button) findViewById (R.id.play); pause = (Button) findViewById (R.id.pause); stop = (Button) findViewById (R.id.stop); play.setOnClickListener (this); pause.setOnClickListener (this); stop.setOnClickListener (this); initMediaPlayer () / / initialize MediaPlayer} private void initMediaPlayer () {try {File file = new File (Environment.getExternalStorageDirectory (), "music.mp3"); mediaPlayer.setDataSource (file.getPath ()); / / specify the path to the audio file mediaPlayer.prepare (); / / Let MediaPlayer enter the ready state} catch (Exception e) {e.printStackTrace () } @ Overridepublic void onClick (View v) {switch (v.getId ()) {case R.id.play:if (! mediaPlayer.isPlaying ()) {mediaPlayer.start (); / / start playing} break;case R.id.pause:if (mediaPlayer.isPlaying ()) {mediaPlayer.pause (); / / pause playback} break;case R.id.stop:if (mediaPlayer.isPlaying ()) {mediaPlayer.reset (); / / stop playing initMediaPlayer ();} break Default:break;} @ Overrideprotected void onDestroy () {super.onDestroy (); if (mediaPlayer! = null) {mediaPlayer.stop (); mediaPlayer.release ();}
As you can see, we created an instance of MediaPlayer when the class was initialized, and then called the initMediaPlayer () method in the onCreate () method to initialize the MediaPlayer object. In the initMediaPlayer () method, we first specify the path to the audio file by creating a File object, from which we can see that we need to first place an audio document named music.mp3 in the root directory of the SD card. The rear side calls the setDataSource () method and the prepare () method to prepare the MediaPlayer before playback.
Next, let's take a look at the code in the click event of each button. When the Play button is clicked, it will be judged. If the current MediaPlayer is not playing audio, call the start () method to start the playback. When the Pause button is clicked, it is judged that if the current MediaPlayer is playing audio, the pause () method is called to pause the playback. When you click the Stop button, you will determine that if the current MediaPlayer is playing audio, call the reset () method to reset the MediaPlayer to the state you just created, and then call the initMediaPlayer () method again.
Finally, in the onDestroy () method, we also need to call the stop () and release () methods, respectively, to release the resources associated with MediaPlayer. Such a simple version of the music player is completed, and now the program will be run on the phone.
The above is about the content of this article on "how to play audio in Android". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please pay attention to 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.