android mediaplayer在线音乐,第一次边播放边缓存,后来直接播放本地音乐,我们的资源是mp3,我现在是两个线程,一个播放一个下载。主流播放器如何实现,可以获取mediaplayer自带的PCM格式缓存数据,然后将代码转成mp3格式?或者定制一个媒体播放器?顺便说一句我的英语太差了不好意思mediaPlayer。
mMediaPlayer.reset();
File file = new File(Environment.getExternalStorageDirectory()+ File.separator + "music",
playMusicDataBean.music_id + ".mp3");
if (file.exists()) {
mMediaPlayer.setDataSource(file.getAbsolutePath());
} else {
mMediaPlayer.setDataSource(playMusicDataBean.url);
new Thread() {
@Override
public void run() {
try {
URL url = new URL(playMusicDataBean.url);
URLConnection conn = url.openConnection();
InputStream in = conn.getInputStream();
if (in == null)
throw new RuntimeException("stream is null");
// File file =
// File.createTempFile("player_mp3_cache",
// ".mp3",
// MainActivity.this.getCacheDir());
File file = new File(
Environment.getExternalStorageDirectory()
+ File.separator + "nyx"
+ File.separator + "music",
playMusicDataBean.music_id + ".mp3");
if (!file.exists())
file.createNewFile();
// if(file.exists())
// file.delete();
m_strTempFilePath = file.getAbsolutePath();
System.out.println(m_strTempFilePath);
FileOutputStream fos = new FileOutputStream(file);
byte buffer[] = new byte[128];
do {
int nNumRead = in.read(buffer);
if (nNumRead <= 0)
break;
fos.write(buffer);
} while (true);
// m_player.setDataSource(m_strTempFilePath);
in.close();
fos.close();
// playMusicDataBean.isLoaded=true;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}.start();
} mMediaPlayer.prepareAsync();