服务没有得到意图
为了从 SD 卡播放音频文件,我将一个名为 path 的文件传递给服务以启动媒体播放器播放路径。
这是传递它
private void playAudio(String url) throws Exception{
Intent music = new Intent(this,MusicService.class);
music.putExtra("paths", url);
startService(music);
}
这是我的服务类接收意图。
class MusicService extends Service implements OnCompletionListener {
MediaPlayer mediaPlayer;
String musicFile;
@Override
public void onCreate() {
mediaPlayer = new MediaPlayer();
mediaPlayer.setOnCompletionListener(this);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Bundle e = intent.getExtras();
musicFile= e.getString("paths");
try {
mediaPlayer.prepare();
mediaPlayer.setDataSource(musicFile);
} catch (IllegalArgumentException i) {
// TODO Auto-generated catch block
i.printStackTrace();
} catch (IllegalStateException i) {
// TODO Auto-generated catch block
i.printStackTrace();
} catch (IOException i) {
// TODO Auto-generated catch block
i.printStackTrace();
}
if (!mediaPlayer.isPlaying())
{
mediaPlayer.start();
}
return START_STICKY;
}
我的问题是媒体播放器没有播放该路径。我不确定它是否没有从 SD 接收文件或由于某种原因没有启动。
To play an audio file from an SD card I'm passing a file called path to a service to start a mediaplayer playing path.
This is passing it
private void playAudio(String url) throws Exception{
Intent music = new Intent(this,MusicService.class);
music.putExtra("paths", url);
startService(music);
}
This is my service class receiving the intent.
class MusicService extends Service implements OnCompletionListener {
MediaPlayer mediaPlayer;
String musicFile;
@Override
public void onCreate() {
mediaPlayer = new MediaPlayer();
mediaPlayer.setOnCompletionListener(this);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Bundle e = intent.getExtras();
musicFile= e.getString("paths");
try {
mediaPlayer.prepare();
mediaPlayer.setDataSource(musicFile);
} catch (IllegalArgumentException i) {
// TODO Auto-generated catch block
i.printStackTrace();
} catch (IllegalStateException i) {
// TODO Auto-generated catch block
i.printStackTrace();
} catch (IOException i) {
// TODO Auto-generated catch block
i.printStackTrace();
}
if (!mediaPlayer.isPlaying())
{
mediaPlayer.start();
}
return START_STICKY;
}
My problem is the mediaplayer isn't playing the path. I'm not sure if it's not receiving the file from the SD or not starting for some reason.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为了帮助您进行调试,请在您的服务中到处添加一些Toast:
从策略上讲,这将帮助您查看服务是否正确启动,服务是否启动其方法等。查明错误比修复错误更重要。
To help you debug, add some Toasts here and there in your service:
Putting it strategically, this will help you see if the service is started correctly, if the service launches its methods, etc. Pinpointing errors is even more important than fixing them.