链接源未在MediaPlayer或SoundPool中播放

时间:2015-03-03 14:28:47

标签: android audio mp3 audio-player

我想使用MediaPlayer或SoundPool播放来自网址的简短声音,但尝试播放时没有任何反应。

我有这种MP3链接格式:“http://www.universal-soundbank.com/802a/805020000000000000000000000pkjn800000000000000000000000000000090/g/85055050505050505050505/k/986.mp3

我仔细检查了存储中清单INTERNET,READ,WRITE中的权限。我读到了这个错误:MediaPlayer:错误(1,-2147483648)和准备失败:状态= 0x1。

此来源似乎有问题。

15:03:42.259 17076-17076/com.soundbox E/libEGL﹕ call to OpenGL ES API with no current context (logged once per thread) 03-03 
15:08:15.966 18139-18139/com.soundbox E/libEGL﹕ call to OpenGL ES API with no current context (logged once per thread) 03-03 
15:08:31.111 18139-18151/com.soundbox E/MediaPlayer﹕ error (1, -2147483648)

当我启动播放器时,我得到准备错误。

    public class MainActivity extends ActionBarActivity implements     AdapterView.OnItemClickListener, MediaPlayer.OnPreparedListener{

  private GridView mainGridview;
  private GridViewAdapter adapter;

  public static final int DIALOG_DOWNLOAD_PROGRESS = 0;
  private ProgressDialog mProgressDialog;


  boolean plays = false, loaded = false;


  int counter;

  private MediaPlayer player;
  FileInputStream fis = null;


  private String[] soundArray;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mainGridview = (GridView) findViewById(R.id.mainGridView);
    adapter = new GridViewAdapter(this);

    mainGridview.setAdapter(adapter);
    mainGridview.setOnItemClickListener(this);

    soundArray =  getResources().getStringArray(R.array.animalsSoundsUrls);

}

  private void play(Uri uri)
  {
    try{
        player = new MediaPlayer();
        player.setAudioStreamType(AudioManager.STREAM_MUSIC);
        player.setDataSource(this, uri);
        player.prepare(); // prepare async to not block main thread
        player.start();
    }catch (IOException e)
    {
        e.printStackTrace();
    }

  }
  private void play(String url)
  {
    try{
        player = new MediaPlayer();
        player.setAudioStreamType(AudioManager.STREAM_MUSIC);
        player.setDataSource(url);
        player.prepare(); // prepare async to not block main thread
        player.start();
    }catch (IOException e)
    {
        e.printStackTrace();
    }

  }

  @Override
  public void onPrepared(MediaPlayer mp) {
    player.start();
  }

  private void killMediaPlayer() {
    if(player!=null) {
        try {
            player.release();
        }
        catch(Exception e) {
            e.printStackTrace();
        }
    }
   }

   @Override
  public void onItemClick(AdapterView<?> parent, View view, int position,   long id) {

    killMediaPlayer();
    play(soundArray[position].toString());

}

@Override
protected void onDestroy() {

    killMediaPlayer();
    super.onDestroy();
}
}

LOG:

03-03 16:39:39.716    3612-3612/com.soundbox E/libEGL﹕ call to OpenGL ES API with no current context (logged once per thread)
03-03 16:41:44.408    3612-3624/com.soundbox E/MediaPlayer﹕ error (1, -2147483648)
03-03 16:41:52.205    4458-4458/com.soundbox E/libEGL﹕ call to OpenGL ES API with no current context (logged once per thread)
03-03 16:41:55.328    4458-4470/com.soundbox E/MediaPlayer﹕ error (1, -2147483648)
03-03 16:41:55.348    4458-4458/com.soundbox E/MediaPlayer﹕ start called in state 0
03-03 16:41:55.348    4458-4458/com.soundbox E/MediaPlayer﹕ error (-38, 0)
03-03 16:41:55.358    4458-4458/com.soundbox E/MediaPlayer﹕ Error (-38,0)
03-03 16:42:48.760    5241-5241/com.soundbox E/dalvikvm﹕ >>>>> Normal User
03-03 16:42:48.760    5241-5241/com.soundbox E/dalvikvm﹕ >>>>> com.soundbox [ userId:0 | appId:10205 ]
03-03 16:42:48.991    5241-5241/com.soundbox E/libEGL﹕ call to OpenGL ES API with no current context (logged once per thread)
03-03 16:42:54.536    5241-5253/com.soundbox E/MediaPlayer﹕ error (1, -2147483648)
03-03 16:42:54.536    5241-5241/com.soundbox E/MediaPlayer﹕ Error (1,-2147483648)
03-03 16:43:03.004    5241-5253/com.soundbox E/MediaPlayer﹕ error (1, -2147483648)
03-03 16:43:03.004    5241-5241/com.soundbox E/MediaPlayer﹕ Error (1,-2147483648)
03-03 16:43:03.915    5241-5253/com.soundbox E/MediaPlayer﹕ error (1, -2147483648)
03-03 16:43:03.915    5241-5241/com.soundbox E/MediaPlayer﹕ Error (1,-2147483648)

1 个答案:

答案 0 :(得分:0)

这就是我以前用来播放网址声音的方式。

public void playSound(String url) {
        MediaPlayer mp = new MediaPlayer();
        try {
            mp.setDataSource(url);
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            mp.prepare();
        } catch (IllegalStateException e) {

            e.printStackTrace();
        } catch (IOException e) {

            e.printStackTrace();
        }
        mp.start();
    }

这将在下载过程中播放声音,例如youtube缓冲。

完成下载后,以下功能将播放声音。

public void playSoundAsync(String url) {
        MediaPlayer mp = new MediaPlayer();
        mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
        try {
            mp.setDataSource(url);
        } catch (IllegalArgumentException e) {

            e.printStackTrace();
        } catch (SecurityException e) {

            e.printStackTrace();
        } catch (IllegalStateException e) {

            e.printStackTrace();
        } catch (IOException e) {

            e.printStackTrace();
        }
        mp.prepareAsync();
        mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {

            @Override
            public void onPrepared(MediaPlayer mp) {
                mp.start();
            }
        });

    }

告诉我它是否有帮助。