android setonpreparedlistener
时间: 2024-06-09 10:12:15 浏览: 9
`setOnPreparedListener`是Android MediaPlayer类的一个方法,用于设置一个回调接口,在视频或音频文件准备好后被调用。当MediaPlayer对象调用`prepareAsync()`方法时,它会异步地准备媒体资源,当准备完成后,将会回调`setOnPreparedListener`接口中的`onPrepared(MediaPlayer mp)`方法。
以下是一个使用`setOnPreparedListener`的示例:
```
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource("http://example.com/myaudio.mp3");
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
// 准备完成后调用此方法
mp.start();
}
});
mediaPlayer.prepareAsync();
```
在上面的示例中,我们设置了一个`setOnPreparedListener`接口,当媒体资源准备完成后,我们调用`start()`方法开始播放。注意,我们在调用`prepareAsync()`方法时,MediaPlayer对象会异步地准备资源,所以我们需要等待`onPrepared()`方法被调用后才能开始播放。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)