gpt4 book ai didi

android - 链接源未在MediaPlayer或SoundPool中播放

转载 作者:行者123 更新时间:2023-12-03 02:04:09 26 4
gpt4 key购买 nike

我想使用MediaPlayer或SoundPool播放URL的短声音,但是尝试播放时什么也没发生。

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

我仔细检查了 list INTERNET,存储中的READ,WRITE中的权限。我读到有关以下错误的信息:MediaPlayer:错误(1,-2147483648)和准备失败。:status = 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();
}
}

日志:
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)

最佳答案

这就是我以前从URL播放声音的方式。

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

}

告诉我是否有帮助。

关于android - 链接源未在MediaPlayer或SoundPool中播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28834317/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com