FAQ Database Discussion Community


Stop MediaPlayer when Fragment closes

android,fragment,android-mediaplayer
I am using a MediaPlayer inside a Fragment, I have managed to stop the music from playing, whenever the current fragment is changed. But the problem is that, if I was within the fragment, and I press the back button to close the application, the music keeps streaming. My code...

Play a specific notification sound

android,android-mediaplayer
I've created a dialog for selecting a notification sound for an app. I'm querying the notification sounds by getting a cursor from the RingtoneManager: RingtoneManager manager = new RingtoneManager(this); manager.setType(RingtoneManager.TYPE_NOTIFICATION); Cursor cursor = manager.getCursor(); I then store the full path to it by concatenating the sound path with the name....

Android two MediaPlayers at the same time skipping

android,video,android-mediaplayer,video-processing
I have an app which plays two videos at the same time. On a MotoG (Android 4.4.4) if the app is playing two videos which were generated on an Android device, it works fine. If it is playing a single video generated on iOS, it works fine. But if it...

Android mediaPlayer only plays file once

android,android-mediaplayer
I'm trying to create an app that play a certain audio file, that was previously recorded, when I press a button or shake my phone. Here is my code. public class Reproduzir extends Activity implements SensorEventListener{ MediaPlayer player = new MediaPlayer(); SensorManager sensor; @Override protected void onCreate(Bundle savedInstanceState ){ super.onCreate(savedInstanceState);...

Android MediaPlayer slows down the code flow

android,android-mediaplayer
I am trying to create sound effects for a game. But with using of MediaPlayer when sound effect starts playing - the game slows down for a bit of a second. And if I use a lot of sound effects - the game really works struggles. I guess something is...

How can Play Video file in android for specific time

android,video,android-mediaplayer,videoview,playback
I have four MP4 videos. And I want to play all of them one by one. Each video should play for a specific time for example 30 second. I have code like this: VideoView videoView = (VideoView)findViewById(R.id.VideoView); MediaController mediaController = new MediaController(this); mediaController.setAnchorView(videoView); videoView.setVideoPath(PATH_TO_FILE); videoView.setMediaController(mediaController); videoView.start(); How can I change...

Who knows the android mediaplayer states by their int value in LogCat?

java,android,android-mediaplayer
I have an implementation that is using the Android MediaPlayer class. I keep on bumping into various bugs, change the code, bump into another bug. Depending on the bug, I get LogCat error messages reporting back from the MediaPlayer: "xyz called in state x". I don't want to post a...

How to close a VideoView Activity (currently have to press back twice)

android,android-activity,android-mediaplayer
I have a simple Activity to preview a video in FullScreen. The layout of the Activity is: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/black" android:fillViewport="true"> <VideoView android:id="@+id/video" android:layout_width="wrap_content" android:layout_height="wrap_content"...

Record audio and play it afterwards

android,android-mediaplayer,android-sdcard
I wrote a class that shall make it possible to record an audio, save it to the sd card and then play the result. Here is my code: public void recordAudio(final String fileName) { final MediaRecorder recorder = new MediaRecorder(); recorder.reset(); ContentValues values = new ContentValues(3); values.put(MediaStore.MediaColumns.TITLE, fileName); recorder.setAudioSource(MediaRecorder.AudioSource.MIC); recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);...

Use one instance of MediaPlayer throughout several fragment classes

java,android,android-intent,android-fragments,android-mediaplayer
I'm creating a soundboard android app that will use multiple fragments to display buttons that when clicked will play a sound. The code I have so far uses two instances of MediaPlayer. I have no idea how to use a single instance of MediaPlayer while still having two fragments. Here...

Media Player: error (-19, 0)

android,media-player,android-mediaplayer
I have a GridView with your positions and I do case on the onCreate, So, case position = 1 play audio1, case position = 2 play audio2 The Activity: public class HomeEnglishAnimalsActivity extends ActionBarActivity { MediaPlayer player; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_home_english_animals); android.support.v7.app.ActionBar actionBar = getSupportActionBar(); actionBar.setHomeButtonEnabled(true);...

Break the cycle of sounds. How to fix?

android,android-mediaplayer
I made a sound in the list on the button. int i = 0; ... void nextTrack(int i) { mSound = new int [] { R.raw.error_one, R.raw.error_two, R.raw.error_three }; mp = MediaPlayer.create(getActivity(), mSound[i]); mp.start(); } ... @Override public void onClick(View v) { nextTrack(i); i++; } But after the sound R.raw.error_three,...

Game music doesnt stop in my android game when user press back button

android,android-mediaplayer,surfaceview
In my android game when user press back button music is not stopped even when user close the game music still playing. This is my main class for game. MediaPlayer mp1 is used to play music. public class GameView extends SurfaceView{ Bitmap bmp,pause; Bitmap background,kinfe,note1,appleimg,note2; Bitmap run1; Bitmap run2; Bitmap...

why is media player starts more than once and media player won't stop in button click which started at on create?

android,android-mediaplayer
I need to add background music to my android application. I have declared the MediaPlayer object as a class variable and create it inside oncreate(). I have start the MediPlayer object also inside the oncreate(). I have stop the media player in a button click method which starts a new...

java.lang.NoSuchMethodException: dispatchMediaKeyEvent [class android.view.KeyEvent]

android,android-mediaplayer,keyevent,android-audiomanager
I'm trying to use media events to play, pause, next, previous music from my android application. When I run it on Samsung s4 (Android version 4.4.2), I can perform all actions. But I'm unable to perform those actions from my Nexus 5 (Android version 5.0.1). I get error while handling...

Android: Display current track Info in mediaplayer using webpage

android,android-asynctask,inputstream,android-mediaplayer,runnable
I am a learner and developing an Android App to play live stream. It is working properly except 1 issue. I want to show title of currently playing Track which is available on a webpge. For this purpose I have used a textView and tried to use Asynchronous Task and...

Play music synchronous using 3 MediaPlayer Objects on Android/Eclipse

android,eclipse,synchronization,android-mediaplayer
What i have: I have implemented three MediaPlayer.Objects in my App. All Three are created using a thread: protected void onResume() { // Threads mTT1 = new TrackThread(this, R.raw.audiofile1, 1, mHandler); mTT2 = new TrackThread(this, R.raw.audiofile2, 2, mHandler); mTT3 = new TrackThread(this, R.raw.audiofile3, 3, mHandler); // start thread mTT1.start(); mTT2.start();...

Adroid MediaPlayer not stopping by clicking another button

android,android-mediaplayer
i have a problem! I have an MediaPlayer with many Sounds. Here my app im Google Play store: https://play.google.com/store/apps/details?id=de.logtainment.ungesoundboard . Sorry, but the app is in German! When you click on two buttons you hear two sounds. How can i Stop the first sound and play the second sound or...

mediaPlayer CompletionListener playing next song before Completion of song

java,android,android-mediaplayer
I have written a mediaPlayer Application and in that mediaplayer.setOnCompletionListener is called before the completion of song and it goes on for every song...It's keep on going to next song until it reaches the last song in playList... Please Help Here is my code: tracksListViewForSpeakers.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void...

How to use a MediaPlayer Singleton

java,android,android-fragments,media-player,android-mediaplayer
I am new to Android developing and am starting with a simple soundboard application. I started developing a soundboard using multiple fragments until I realized that I was using multiple instances of MediaPlayer. This is not good because I want only one sound to play at a time. I realized...

Android: MediaPlayer.setDataSource(FileDescriptor fd) vs MediaPlayer.setDataSource(FileDescriptor fd, long offset, long length)

android-mediaplayer
I have created 3 Asset file descriptor of 3 sounds (put in res/raw) AssetFileDescriptor afd1 = mContext.getResources().openRawResourceFd(R.raw.mp3_file_1); AssetFileDescriptor afd2 = mContext.getResources().openRawResourceFd(R.raw.mp3_file_2); AssetFileDescriptor afd3 = mContext.getResources().openRawResourceFd(R.raw.mp3_file_3); Then I put them into an array: array.add(afd1); array.add(afd2); array.add(afd3); Then I create an instance of MediaPlayer and let it play only the first sound...

Play mp3 file continuously in android

android,android-mediaplayer
I want to play mp3 file continuously in background. I mean if i an on first activity mp3 file should start in background. Now, If I navigate to second activity then also i want to play file continuously. File should not be played from start in second activity.

Android - Play Sound on Button Click

android,button,audio,click,android-mediaplayer
I have been following an online tutorial on how to play sound on button click. I am getting no errors with my Java code which leads me to believe that the code is correct. However, they did not provide a tutorial on what the xml should read. So I left...

Multiple duplicates load into GridView from SD card

android,android-asynctask,android-mediaplayer,android-gridview
I am trying to load Uri sound files from my SD card into a custom GridView where each item contains an ImageView (for the Uri) and a TextView for the sound file title, but it keeps loading duplicates. I have 6 sound files on my card, but it loads 12,...

How to stop MediaPlayer onPause() if i declare/start it inside the onButtonClick method?

android,onclick,media-player,android-mediaplayer,onpause
I am a noob programer and I encountered a problem, I have a mediaplayer.start() inside an onClick method, if i call mediaplayer.stop(); mediaplayer.reset(); mediaplayer.release(); it will crash and get the error: Attempt to invoke virtual method 'void android.media.MediaPlayer.stop()' on a null object reference ". I tried How to stop the...

Android MediaPlayer pause() stops the player and start again

android,android-studio,android-mediaplayer,music-player
I need to play audio in an Android application so I used MediaPlayer. I created 2 buttons: Play and Pause but after I paused a song and want to resume it, the song starts again instead of resuming from where I left. I wrote this code: public MediaPlayer mediaPlayer =...

How to find the right file path in sdcard?

android,datasource,android-mediaplayer
This is the previous post. About mp3 player And the picture below is my A.mp3 path I find in my phone. ` mediaPlayer.setDataSource("/storage/sdcard1/A.mp3") File file =new File(Environment.getExternalStorageDirectory(),"A.mp3"); mediaPlayer.setDataSource(file.getPath()); There are two paths above..According the picture,it should be the first one,but it does not work. I push A.mp3 into the internal...

android mediaplayer get playing url

android,android-mediaplayer
I'm interested in retrieving the current url or uri of a Media Player. if i run : String url = "http://........"; // your URL here MediaPlayer mediaPlayer = new MediaPlayer(); mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); mediaPlayer.setDataSource(url); mediaPlayer.prepare(); // might take long! (for buffering, etc) mediaPlayer.start(); is there a way i get the url this...

I have one issue like ringing siren in my application?

android,android-activity,broadcastreceiver,android-mediaplayer
I am created one receiver in that if any thing wrong(like lock-screen password number of attempts is greater than 5)then i am ringing siren and i want to stop the siren in my activity, i am created that media player as a static value and i am accessing in my...

Play recorded voice simultaneously in the earphones

java,android,android-mediaplayer,voice-recording
I am new to programming but till now I have came across the basic of it. Currently I am using Android Studio. I tried many a times on web for how can I play recorded voice simultaneously while its continuing to record. The recorded voice should be played in the...

The MediaPlayer plays all songs at once

java,android,android-mediaplayer
I have a simple app which has a button and a textview and upon the click, it buffers from a audio stream adn plays it using a mediaPlayer object. I have created a simple loop fro different songs to get played at different execution of the app. ( everytime the...

How do I call mediaplayer once only?

android,android-mediaplayer
In android, I have a mediaplayer on detail screen, which duplicates when I select another screen then play another detail, causing sound files to play on the same time. how do I fix this problem? Id like to play the most recent selected media only. public void onCreate(Bundle savedInstanceState) {...

Correct way to use onClickListener with MediaPlayer parameters and an array

android,android-mediaplayer
i started learning few days ago, and im trying to get this buttons to work, its just a grid of buttons that should play a different sound each one, but it crashes when i try to play any, almost all my code is from this link http://www.stealthcopter.com/blog/2010/09/android-creating-a-custom-adapter-for-gridview-buttonadapter/ and the onClick...

Android - Is it possible to get error “description” from MediaPlayer error code?

android,android-mediaplayer,error-code
MediaPlayer error listener looks like this: public boolean onError(MediaPlayer mp, int what, int extra) {...} Is it possible to get a meaningful message from int what or int extra, that I could display to user?...

Android MediaPlayer playing sound ok on virtual device and on tablet, but intermittently on smartphone galaxy s iii mini

android,android-activity,android-studio,android-mediaplayer,samsung-mobile
I'm using the following code in onCreate() mPlayer = MediaPlayer.create(this, R.raw.rain); mPlayer.start(); No problems in virtual emulator nor in tablet galaxy tab, but when I try on my Galaxy S 3 mini it plays the sound intermittently, my logcat looks like this: 06-11 12:48:04.253 5579-5579/? D/dalvikvm﹕ Late-enabling CheckJNI 06-11 12:48:04.573...

Android back button on media controller for videos played from webView

android,android-mediaplayer,mediacontroller
I am displaying a video in a webView and I am noticing particular behaviors from Kindle devices. First, you have to slide out the device buttons (on Kindle Fire HD), which brings up the video controls for the video. Pressing the back button while the controls are up will first...

Resume activity on notification click

android,android-activity,notifications,android-mediaplayer
I am having a problem with resuming activity on notification click. I have an app that plays a single song for some time. The idea is when you play a song, and press Home button, there should be notification that returns you to the app where you can stop the...

Android MediaPlayer stops on backpressed

android,media-player,android-mediaplayer,back,onbackpressed
I'm developing a radio stream app.I use android - mediaplayer class which plays radiostream. I m trying to run app in two devices . When I pressed back button in Samsung galaxy S3 mini(4.2.2) radio stream continues to playing. And I controls media player with notification bar. But when I...

how to play samba Share filepath through android media player?

android,android-mediaplayer
my samba Share file path is : smb://10.0.0.18/MZAES_Songs/Aashiqui 2/Asan Nahin Yahan.mp3 How can I give this path to android mediaPlayer? For example for local memory file can be played as String filepath = "sdcard/emulated/0/ashqui/asaa.mp3"; mediaplayer.setDataSource(filePath"); mediaPlayer.prepare(); mediaPlayer.start(); etc.. But how to do with samba shared file path......

Sound control in android fragment

android,android-fragments,android-mediaplayer
If I add two Fragment to one Activity, each playing different media, is there a way to mute the sound coming from one Fragment while the other plays full volume? From my research, it seems you can only control the devices volume with code, thus if two media are playing,...

Handle MediaPlayer exceptions from throwing infinite errors

android,android-mediaplayer
I've implemented a service for MediaPlayer so that I can continue playing even after minimizing the app. The problem is whenever the MediaPlayer fails to play a song (e.g. The file not exists or the MediaPlayer source path is wrong) it throws an common error, Error (-38,0) start called in...

Service Intent must be explicit

android,android-intent,android-service,android-mediaplayer
I'm trying to use service for a background music. package com.example.neotavraham; import android.app.Service; import android.content.Intent; import android.media.MediaPlayer; import android.os.IBinder; import android.util.Log; public class PlayMusicService extends Service implements MediaPlayer.OnPreparedListener { public static final String ACTION_PLAY = "com.example.neotavraham.PLAY"; MediaPlayer mMediaPlayer = null; @Override public int onStartCommand(Intent intent, int flags, int startId) {...

Error in state 4 of MediaPlayer

android,nullpointerexception,android-mediaplayer,mediastore
I'm trying to create a MediaPlayer application. To implement the same I've created a service which plays the mp3 file in the MediaPlayer. I'm getting a NullPointerException while opening the mp3 file. The code to instantiate the media player is in onStartCommand() as shown below: @Override public int onStartCommand(Intent intent,...

Playing sounds inside of a fragment using MediaPlayer

java,android,android-fragments,android-mediaplayer,android-imagebutton
I am creating a soundboard app. The app will feature different "pages" (fragments) that the user can switch between. Each fragment has a number of ImageButtons that, when clicked on, will play a sound. I put the following code first inside of the MainActivity inside the OnCreate method. After that...

Get current playing track info from all the available android devices

android,android-mediaplayer,music
I m welling to get current playing track info from all the available android devices.Therefore i have tried out this code and it works fine in the emulator or with any nexus phone but as soon as i tried it out on an xperia phone it doens work and it...

Android TextToSpeech.synthesizeToFile() file is not created

java,android,android-mediaplayer,text-to-speech
I am trying to implement a pause and play function to some text using tts and MediaPlayer. However, I can't seem to be able to create a .wav file using the synthesizeToFile function. I already added the required permission to the xml file: <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> This is the file...

Application crash with no exception after resuming

android,android-service,android-mediaplayer,surfaceview
I am making simple app to display video and plaing audio playback if activity is in background. My app works fine if i just turn screen off/on. It crashes when I resume my app after brawsing another application. Log cat show no error. import android.app.Activity; import android.content.Intent; import android.media.MediaPlayer; import...

Lock screen media control for custom Media player

android,android-mediaplayer
I am using my own custom MediaPlayer rather than android native MediaPlayer ,can i use MediaSessionCompat for lock screen media control?

How to use one instance of MediaPlayer for several fragments

java,android,android-intent,android-fragments,android-mediaplayer
I am creating a soundboard app. The app will feature different "pages" (fragments within viewpager) that the user can switch between. Each fragment has a number of ImageButtons that, when clicked on, will play a certain sound. In each of my fragment classes I put the following code in its...

android - how to delay an audio file so it starts after a few seconds

android,android-activity,android-mediaplayer
I've been wondering if there's a way with either mp or soundpool to delay a sound and make it start exactly after 10 seconds (I have a countdown and want to add a sound effect saying 3,2,1 go! after 10 seconds.)

Is it possible to control media player quality?

android,android-mediaplayer
I download video with: mPlayer.setDataSource("https://s3-eu-west-1.amazonaws.com/ellovidsout/1232/9/1423065486.mp4.m3u8") Header contains following data: #EXTM3U #EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=532000,RESOLUTION=400x224,CODECS="avc1.42001e,mp4a.40.2" 400k_1423065486.mp4.m3u8 #EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=1273000,RESOLUTION=640x360,CODECS="avc1.4d001f,mp4a.40.2" 1000k_1423065486.mp4.m3u8 #EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=2476000,RESOLUTION=1024x576,CODECS="avc1.4d001f,mp4a.40.2"...

Can an Android (Java not C/C++) plugin alter unity textures?

java,android,unity3d,android-mediaplayer
I'm trying to use an Android plugin to get around the fact that Unity does not support Video Textures on mobile. I do this by getting the texture ID of the texture that will be used for video (supplied by Texture2D.GetNativeTextureID()) which i then pass into the Java Plugin. The...