how to play audio file from url in android

前端 未结 4 1843
有刺的猬
有刺的猬 2021-01-03 04:26

I need to play audio file from remote server in my app. I could play when I tested with localhost server (using WAMP). When the same file supplied from the server it is not

4条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-03 04:55

    If you are writing Java programs to play media files, then the first port of call is the MediaPlayer class. Typical code to play a file using the streaming mechanism of MediaPlayer is

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        try {
            Uri uri = Uri.parse("http://192.168.1.9/music/test.ogg");
            MediaPlayer player = new MediaPlayer();
            player.setAudioStreamType(AudioManager.STREAM_MUSIC);
            player.setDataSource(this, uri);
            player.prepare();
            player.start();
        } catch(Exception e) {
            System.out.println(e.toString());
        }
    }
    

    Wake Lock Permission - If your player application needs to keep the screen from dimming or the processor from sleeping, or uses the MediaPlayer.setScreenOnWhilePlaying() or MediaPlayer.setWakeMode() methods, you must request this permission.

    
    

提交回复
热议问题