0

I want to play an audio in background on click of an image, below is XML code of the image

<ImageView
        android:id="@+id/img_play"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/buttonplayicon"
        android:layout_below="@+id/btnChangeImage"
        android:layout_margin="5dp"
        android:contentDescription="@string/blank"
        android:tag="0"
         />    

below is the code to play an audio

public void audioPlayer(String path){

            Uri uri = Uri.parse("SERVERPATH/abc.mp3");
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            startActivity(intent);
}

This plays an audio but it opens in the browser and plays it, Does any one know to play this in background without opening it in browser?

EDIT 1

Log cat:

06-16 10:44:24.974: E/MediaPlayer(16929): Unable to create media player
06-16 10:44:24.984: W/System.err(16929): java.io.IOException: setDataSource failed.: status=0x80000000
06-16 10:44:24.984: W/System.err(16929):    at android.media.MediaPlayer._setDataSource(Native Method)
06-16 10:44:24.984: W/System.err(16929):    at android.media.MediaPlayer.setDataSource(MediaPlayer.java:991)
06-16 10:44:24.984: W/System.err(16929):    at android.media.MediaPlayer.setDataSource(MediaPlayer.java:944)
06-16 10:44:24.984: W/System.err(16929):    at com.mediaapp4.mediaapp4.MainActivity$PlaceholderFragment.audioPlayer(MainActivity.java:114)
06-16 10:44:24.984: W/System.err(16929):    at com.mediaapp4.mediaapp4.MainActivity$PlaceholderFragment$1.onClick(MainActivity.java:85)
kgandroid
  • 5,507
  • 5
  • 39
  • 69
Pradnnya Bhat
  • 45
  • 1
  • 2
  • 11

4 Answers4

0

try below code:-

    private MediaPlayer mediaPlayer;
    mediaPlayer = MediaPlayer.create(this,Uri.parse("SERVERPATH/abc.mp3"));
    mediaPlayer.start();

see below link for more detail:-

How to play Audio file Mp3 from the server

Community
  • 1
  • 1
duggu
  • 37,851
  • 12
  • 116
  • 113
0

first your intialize the media player in your app.

MediaPlayer mp ;
mp = new MediaPlayer();

set the path and file name and then start your media in bg.

mp.setDataSource(path+fileName);
mp.prepare();
mp.start();
prakash
  • 1,413
  • 1
  • 21
  • 33
  • Hi Prakash, I made this changes, the audio does not play instead i get "Unable to create media player" error. I have edited the question. – Pradnnya Bhat Jun 16 '14 at 05:17
0
Try this way,hope this will help you to solve your problem.

Custom class for audio play

public class MyAudioPlayer {

        private MediaPlayer audioPlayer;
        private AudioListener audioListener;
        static MyAudioPlayer myAudioPlayer;



        /**
         * This method used to set audio action listener.
         *
         * @param audioListener
         *            represented audio listener
         */
        public void setAudioListener(AudioListener audioListener) {
            this.audioListener = audioListener;
        }

        /**
         * This method used to play audio.
         *
         * @param url
         *            represented audio path
         */
        public void playAudio(String url) {

            if (myAudioPlayer != null) {
                myAudioPlayer.stopAudio();
                myAudioPlayer = null;
            }
            myAudioPlayer = this;
            audioPlayer = new MediaPlayer();
            audioPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {

                @Override
                public boolean onError(MediaPlayer mp, int what, int extra) {
                    if (audioListener != null) {

                        audioListener.onComplete();
                    }
                    return false;
                }
            });
            audioPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {

                @Override
                public void onCompletion(MediaPlayer mp) {

                    if (audioListener != null) {

                        audioListener.onComplete();
                    }

                }
            });

            audioPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {

                @Override
                public void onPrepared(MediaPlayer mp) {
                    audioPlayer.start();
                    if (audioListener != null) {
                        audioListener.onPrepared();
                    }

                }
            });

            try {
                audioPlayer.setDataSource(url);
                audioPlayer.prepareAsync();

            } catch (IOException e) {
                if (audioListener != null) {

                    audioListener.onComplete();
                }
                e.printStackTrace();
            }

        }

        /**
         * This method used to stop audio.
         */
        public void stopAudio() {
            if (audioPlayer != null) {

                if (audioPlayer.isPlaying()) {
                    audioPlayer.reset();
                    audioPlayer.release();
                    audioPlayer = null;
                    if (audioListener != null) {
                        audioListener.onComplete();
                    }
                } else {
                    audioPlayer.reset();
                    audioPlayer.release();
                    audioPlayer = null;
                }
            }
        }

        /**
         * Inner Interface
         *
         * @author tasol
         *
         */
        public interface AudioListener {
            void onComplete();

            void onPrepared();

            void onPlayClicked(boolean isplaying);
        }

        /**
         * This method used to check is audio playing.
         *
         * @return represented {@link Boolean}
         */
        public boolean isPlaying() {
            if (audioPlayer != null) {
                if (audioPlayer.isPlaying()) {
                    return true;
                }
            }
            return false;
        }

}

How to play audio from server url path

MyAudioPlayer audioPlayer;
    MyAudioPlayer.AudioListener audioListener;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        audioPlayer = new MyAudioPlayer();
        audioPlayer.setAudioListener(new MyAudioPlayer.AudioListener() {
            @Override
            public void onComplete() {
                audioListener.onComplete();
            }

            @Override
            public void onPrepared() {
                audioListener.onPrepared();
            }

            @Override
            public void onPlayClicked(boolean isplaying) {
                audioListener.onPlayClicked(isplaying);
            }
        });

        audioPlayer.playAudio("yourserverurlaudiopath");

    }
Haresh Chhelana
  • 24,720
  • 5
  • 57
  • 67
0
MediaPlayer mp = new MediaPlayer();

// Set data source -
setDataSource("/sdcard/path_to_song");

// Play audio
mp.start();

// Pause audio
mp.pause();

// Reset mediaplayer
mp.reset();

// Get song length duration - in milliseconds
mp.getDuration();

// Get current duration - in milliseconds
mp.getCurrentDuration();

// Move song to particular second - used for Forward or Backward
mp.seekTo(positon); // position in milliseconds

// Check if song is playing or not
mp.isPlaying(); // returns true or false

Refer this link for step by step tutorial to build audio player inside your app.

Android Building Audio Player Tutorial

Dinesh Raj
  • 664
  • 12
  • 30