Android 如何从SD卡中播放音乐
我能读取到SD卡中音频文件的相关属性,路径能够获取到,但是如何根据路径,可以点击播放。每次这样设定获取的都是空指针,求大神帮个忙publicclassPlayServic...
我能读取到SD卡中音频文件的相关属性,路径能够获取到,但是如何根据路径,可以点击播放。每次这样设定获取的都是空指针,求大神帮个忙
public class PlayService extends Service {
private File file ;
private String mPath;
public PlayService() {
}
static boolean isPlay;
MediaPlayer player;
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public void onCreate() {
// player = MediaPlayer.create(this, R.raw.test);
player = MediaPlayer.create(this, Uri.parse(file.getAbsolutePath()));
player.reset();
player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
play();
}
});
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if(!player.isPlaying()){
player.start();
isPlay = player.isPlaying();
}else{
player.pause();
isPlay = player.isPlaying();
}
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
player.stop();
isPlay = player.isPlaying();
player.release();
super.onDestroy();
}
private void play(){
try{
player.reset();
player.setDataSource(file.getAbsolutePath());
player.prepare();
player.start();
} catch (Exception e) {
e.printStackTrace();
}
}
public void setPath(Object path) {
mPath = path.toString();
Log.v("TAG","string path = "+mPath);
file = new File(mPath);
}
} 展开
public class PlayService extends Service {
private File file ;
private String mPath;
public PlayService() {
}
static boolean isPlay;
MediaPlayer player;
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public void onCreate() {
// player = MediaPlayer.create(this, R.raw.test);
player = MediaPlayer.create(this, Uri.parse(file.getAbsolutePath()));
player.reset();
player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
play();
}
});
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if(!player.isPlaying()){
player.start();
isPlay = player.isPlaying();
}else{
player.pause();
isPlay = player.isPlaying();
}
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
player.stop();
isPlay = player.isPlaying();
player.release();
super.onDestroy();
}
private void play(){
try{
player.reset();
player.setDataSource(file.getAbsolutePath());
player.prepare();
player.start();
} catch (Exception e) {
e.printStackTrace();
}
}
public void setPath(Object path) {
mPath = path.toString();
Log.v("TAG","string path = "+mPath);
file = new File(mPath);
}
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询