【问题标题】:android MediaPlayer to play online(external) audioandroid MediaPlayer 播放在线(外部)音频
【发布时间】:2018-05-22 16:24:53
【问题描述】:

当我单击按钮时,我使用此代码播放在线 mp3 音频,但它不起作用。

public void play(View v) throws IllegalStateException, IOException{ 
    MediaPlayer em2 =MediaPlayer.create(this, Uri.parse("https://ia801005.us.archive.org/22/items/sslamweb.blogspot.com_201308/Maher%20Zain%20-%20Hold%20My%20Hand%20-%20Official%20Lyrics%20Video.mp3"));
    em2.setAudioStreamType(AudioManager.STREAM_MUSIC);  
    em2.prepare();
    em2.start();        
}

【问题讨论】:

    标签: android audio mp3 android-mediaplayer


    【解决方案1】:

    您正在使用create 创建MediaPlayer,它已经为您调用了prepare。不要再拨打prepare

    public void play(View v) throws IllegalStateException, IOException{ 
        MediaPlayer em2 = MediaPlayer.create(this, Uri.parse("https://ia801005.us.archive.org/22/items/sslamweb.blogspot.com_201308/Maher%20Zain%20-%20Hold%20My%20Hand%20-%20Official%20Lyrics%20Video.mp3"));
        em2.setAudioStreamType(AudioManager.STREAM_MUSIC);
        em2.start();        
    }
    

    将来,查看错误的 logcat 输出会有所帮助。

    【讨论】:

      【解决方案2】:

      导入android.media.AudioManager;

      android.media.MediaPlayer mediaplayer;
                              mediaplayer = new android.media.MediaPlayer();
                              mediaplayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
                              try {
      
                                  mediaplayer.setDataSource("https://ia801005.us.archive.org/22/items/sslamweb.blogspot.com_201308/Maher%20Zain%20-%20Hold%20My%20Hand%20-%20Official%20Lyrics%20Video.mp3");
                                  mediaplayer.prepare();
                              } catch (IllegalArgumentException e) {
                                  // TODO Auto-generated catch block
                                  e.printStackTrace();
                              } catch (SecurityException e) {
                                  // TODO Auto-generated catch block
                                  e.printStackTrace();
                              } catch (IllegalStateException e) {
                                  // TODO Auto-generated catch block
                                  e.printStackTrace();
                              } catch (IOException e) {
                                  // TODO Auto-generated catch block
                                  e.printStackTrace();
                              }
      
                              mediaplayer.start();
      

      享受.......

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多