Home android media player wont play audio file
Reply: 4

android media player wont play audio file

madcoderz
1#
madcoderz Published in 2013-04-22 14:41:48Z

im making a music app just for fun. I can read all my music files from sd card but MediaPlayer wont play the sound. I have the path to the file which i pass to the media player with setDataSource but nothing happens and i get no exceptions. Here is the code im using.

Uri songUri;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_player);
        setUpViews();
        MediaPlayer mp = new MediaPlayer();

        try {
            mp.setDataSource(songUri.toString());
            mp.prepare();

        } catch (IllegalArgumentException e) {
            Toast.makeText(this, "ILLEGAL ARGUMENT EXCEPTION", Toast.LENGTH_LONG).show();
            e.printStackTrace();
        } catch (SecurityException e) {
            Toast.makeText(this, "SECURITY EXCEPTION", Toast.LENGTH_LONG).show();
            e.printStackTrace();
        } catch (IllegalStateException e) {
            Toast.makeText(this, "ILLEGAL STATE EXCEPTION", Toast.LENGTH_LONG).show();
            e.printStackTrace();
        } catch (IOException e) {
            Toast.makeText(this, "IO EXCEPTION", Toast.LENGTH_LONG).show();
            e.printStackTrace();
        }
          mp.start();
          Log.d("URI AFTER SET UP", songUri.toString());
    }

As i said before nothing happens when i open this activity but i still get all the Log.d in the console. Is there any more configurations for the media player to play the music? Thanks in advance.

FireAndIce
2#
FireAndIce Reply to 2013-04-22 15:30:24Z

setDataSource() requires a path argument . You are passing the entire uri as string. Try this :

mp.setDataSource(songUri.getPath());
mp.prepare();
mp.start();

Moreover, if you are trying to access a file with content:// uri, it won't work. You'll have to find the real path to the file, i.e file:// uri.

nidhi_adiga
3#
nidhi_adiga Reply to 2013-04-23 07:27:38Z

I think you have to add setAudioStreamType to your MediaPlayer Object. You can add this before mp.setDataSource(your_URI); like below

mp.setAudioStreamType(AudioManager.STREAM_MUSIC);

There other types, you can choose AudioManager.STREAM_MUSIC for Music Player

madcoderz
4#
madcoderz Reply to 2013-04-23 14:09:40Z

I just solved the problem, and i just dont know why it works. What i did was to put a button to stop the music being played. I dont understand why it works so if someone could explain why it does i'd be thanksfull.

Pranav Jadav
5#
Pranav Jadav Reply to 2013-04-24 11:53:53Z

get the path of song by querying MEDIASTORE-EXTERNAL_URI.And this field MEDIASTORE.AUDIO.MEDIA.DATA will give u the path of the song(which is string). U can set it directly to mediaplayer.setDataSource(Your_path_from_mediastore) .It will work fine than.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.302088 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO