Android stop media player from another Activity
Android - MediaPlayerAdvertisements Previous Page Next Page Android provides many ways to control playback of audio/video files and streams. One of this way is through a class called MediaPlayer. Android is providing MediaPlayer class to access built-in mediaplayer services like playing audio,video e.t.c. In order to use MediaPlayer, we have to call a static Method create() of this class. This method returns an instance of MediaPlayer class. Its syntax is as follows MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.song);The second parameter is the name of the song that you want to play. You have to make a new folder under your project with name raw and place the music file into it. Once you have created the Mediaplayer object you can call some methods to start or stop the music. These methods are listed below. mediaPlayer.start(); mediaPlayer.pause();On call to start() method, the music will start playing from the beginning. If this method is called again after the pause() method, the music would start playing from where it is left and not from the beginning. In order to start music from the beginning, you have to call reset() method. Its syntax is given below. mediaPlayer.reset();Apart from the start and pause method, there are other methods provided by this class for better dealing with audio/video files. These methods are listed below
ExampleHere is an example demonstrating the use of MediaPlayer class. It creates a basic media player that allows you to forward, backward, play and pause a song. To experiment with this example, you need to run this on an actual device to hear the audio sound.
Following is the content of the modified main activity file src/MainActivity.java. Following is the modified content of the xml res/layout/activity_main.xml. In the below code abcindicates the logo of tutorialspoint.com Following is the content of the res/values/string.xml. Following is the content of AndroidManifest.xml file. Let's try to run your application. I assume you have connected your actual Android Mobile device with your computer. To run the app from Eclipse, open one of your project's activity files and click Run icon from the toolbar. Before starting your application, Android studio will display following screensBy default you would see the pause button disabled. Now press play button and it would become disable and pause button become enable. It is shown in the picture below Up till now, the music has been playing. Now press the pause button and see the pause notification. This is shown below Now when you press the play button again, the song will not play from the beginning but from where it was paused. Now press the fast forward or backward button to jump the song forward or backward 5 seconds. A time came when the song cannot be jump forward. At this point , the notification would appear which would be something like this Your music would remain playing in the background while you are doing other tasks in your mobile. In order to stop it , you have to exit this application from background activities. Above image shows when you pick rewind button. Previous Page Print Page Next Page Advertisements |