简体   繁体   中英

Android media player works with some url but not with others

My code is working fine if I use this URL

http://programmerguru.com/android-tutorial/wp-content/uploads/2013/04/hosannatelugu.mp3

But if I tried to use this URL then I got the following exception

java.io.IOException: Prepare failed.: status=0x1
android.media.MediaPlayer.prepare(Native Method)
//after some lines


start called in state 0
   error (-38, 0)

This is the second URL that I used

http://hcservices.byethost4.com/hosannatelugu.mp3

If you open the file you'll notice that both are the same thing. Earlier I thought the .mp3 file is corrupted but it's not!

I guess issue is related to the host or something else but I'm running out of the idea.

Here's my code.

public class MainActivity extends AppCompatActivity {
    Button play;
    Button stop;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        play = (Button) findViewById(R.id.play);
        play.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Uri myUri = Uri.parse("http://hcservices.byethost4.com/hosannatelugu.mp3");
                MediaPlayer mPlayer = new MediaPlayer();
                try {
                    String a = "http://hcservices.byethost4.com/hosannatelugu.mp3";
                    mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

                    mPlayer.setDataSource(a);

                    mPlayer.prepare();

                } catch (Exception e) {
                    e.printStackTrace();
                }
                mPlayer.start();
            }
        });
    }
}

Gonna copy some documentation from the Android site here. Maybe it will help you.

Playing from a remote URL via HTTP streaming looks like this

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();

Since your code already looks like that, I am assuming your problem is this additional note.

Note: If you're passing a URL to stream an online media file, the file must be capable of progressive download.

So, if that first link works, but the second one doesn't, that would mean the second host does not support "progressive download." And that is a problem with the server, not your application or code.

Source: http://developer.android.com/guide/topics/media/mediaplayer.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM