求大佬救命!用Service播放音乐时,如何根据其他页面传输过来的字符串指定播放哪首音乐?

thnugvfghj 2019-06-28 09:39:33



...全文
1119 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
thnugvfghj 2019-06-30
  • 打赏
  • 举报
回复
感谢各位的回复,今天终于用十分傻瓜的方法弄出来了! 惭愧,我基础确实十分薄弱,我校大概课程安排不太好,我们java几乎没怎么学,Android studio刚学一点,就要开始做APP了,时间紧迫没来得及好好从头学起,之后会再好好搞清楚的!
Nonoas 2019-06-30
  • 打赏
  • 举报
回复
年轻人不要好高骛远,先学基础
m-oj 应用层 2019-06-29
  • 打赏
  • 举报
回复
当然闪退,你把map弄成静态变量,在onStartCommand里收到参数后再去取,你现在没取到参数直接用就报错,不能在onCreate里用
thnugvfghj 2019-06-29
  • 打赏
  • 举报
回复
引用 8 楼 m-oj 的回复:
[quote=引用 6 楼 thnugvfghj 的回复:] [quote=引用 5 楼 m-oj 的回复:] 当然闪退,你把map弄成静态变量,在onStartCommand里收到参数后再去取,你现在没取到参数直接用就报错,不能在onCreate里用
感谢大哥!我又改了改,但不知是不是我对这个“参数”是什么“参数”不太理解的原因(是指传过来的map的key吗?),结果还是闪退了,挠头.jpg 以下是代码,可以再帮我看看吗? package com.example.myalarm; import android.app.Service; import android.content.Intent; import android.media.MediaPlayer; import android.os.IBinder; import java.util.HashMap; import java.util.Map; public class MusicService extends Service { private String Sring; public MusicService() { } static boolean isplay; MediaPlayer player; static Map<String,Integer> date = new HashMap<String,Integer>(); @Override public IBinder onBind(Intent intent) { // TODO: Return the communication channel to the service. throw new UnsupportedOperationException("Not yet implemented"); } @Override public int onStartCommand(Intent intent, int flags, int startId) { if (!player.isPlaying()) { player.start(); isplay = player.isPlaying(); } date.put("bird",R.raw.bird); date.put("chimes",R.raw.chimes); date.put("cock",R.raw.cock); date.put("cuckoo",R.raw.cuckoo); date.put("notify",R.raw.notify); date.put("ringout",R.raw.ringout); date.put("water",R.raw.water); Sring=intent.getStringExtra("Sring");//这算是收到了参数吗? return super.onStartCommand(intent, flags, startId); } @Override public void onCreate() { player = MediaPlayer.create(this,date.get(Sring)); } @Override public void onDestroy() { player.stop(); isplay = player.isPlaying(); player.release(); super.onDestroy(); } } [/quote] 你在onCreate里生成player可以,但不要在这里获取date.get(String),你可以先传一个Uri.EMPTY,然后在onStartCommand接收到参数再调用player.setDataSource(this, date.get(Sring))更新数据,然后播放[/quote] Uri这个我完全没学过相关内容,所以这个什么意思、是什么步骤……我,看不懂 引用错了,再发一次
thnugvfghj 2019-06-29
  • 打赏
  • 举报
回复
[/quote] 你在onCreate里生成player可以,但不要在这里获取date.get(String),你可以先传一个Uri.EMPTY,然后在onStartCommand接收到参数再调用player.setDataSource(this, date.get(Sring))更新数据,然后播放[/quote] Uri这个我完全没学过相关内容,所以这个什么意思、是什么步骤……我,看不懂
m-oj 应用层 2019-06-29
  • 打赏
  • 举报
回复
引用 6 楼 thnugvfghj 的回复:
[quote=引用 5 楼 m-oj 的回复:] 当然闪退,你把map弄成静态变量,在onStartCommand里收到参数后再去取,你现在没取到参数直接用就报错,不能在onCreate里用
感谢大哥!我又改了改,但不知是不是我对这个“参数”是什么“参数”不太理解的原因(是指传过来的map的key吗?),结果还是闪退了,挠头.jpg 以下是代码,可以再帮我看看吗? package com.example.myalarm; import android.app.Service; import android.content.Intent; import android.media.MediaPlayer; import android.os.IBinder; import java.util.HashMap; import java.util.Map; public class MusicService extends Service { private String Sring; public MusicService() { } static boolean isplay; MediaPlayer player; static Map<String,Integer> date = new HashMap<String,Integer>(); @Override public IBinder onBind(Intent intent) { // TODO: Return the communication channel to the service. throw new UnsupportedOperationException("Not yet implemented"); } @Override public int onStartCommand(Intent intent, int flags, int startId) { if (!player.isPlaying()) { player.start(); isplay = player.isPlaying(); } date.put("bird",R.raw.bird); date.put("chimes",R.raw.chimes); date.put("cock",R.raw.cock); date.put("cuckoo",R.raw.cuckoo); date.put("notify",R.raw.notify); date.put("ringout",R.raw.ringout); date.put("water",R.raw.water); Sring=intent.getStringExtra("Sring");//这算是收到了参数吗? return super.onStartCommand(intent, flags, startId); } @Override public void onCreate() { player = MediaPlayer.create(this,date.get(Sring)); } @Override public void onDestroy() { player.stop(); isplay = player.isPlaying(); player.release(); super.onDestroy(); } } [/quote] 你在onCreate里生成player可以,但不要在这里获取date.get(String),你可以先传一个Uri.EMPTY,然后在onStartCommand接收到参数再调用player.setDataSource(this, date.get(Sring))更新数据,然后播放
Non-satisfied 2019-06-29
  • 打赏
  • 举报
回复
生命周期不对,onCreate - > onStartCommand
thnugvfghj 2019-06-29
  • 打赏
  • 举报
回复
引用 5 楼 m-oj 的回复:
当然闪退,你把map弄成静态变量,在onStartCommand里收到参数后再去取,你现在没取到参数直接用就报错,不能在onCreate里用
感谢大哥!我又改了改,但不知是不是我对这个“参数”是什么“参数”不太理解的原因(是指传过来的map的key吗?),结果还是闪退了,挠头.jpg 以下是代码,可以再帮我看看吗? package com.example.myalarm; import android.app.Service; import android.content.Intent; import android.media.MediaPlayer; import android.os.IBinder; import java.util.HashMap; import java.util.Map; public class MusicService extends Service { private String Sring; public MusicService() { } static boolean isplay; MediaPlayer player; static Map<String,Integer> date = new HashMap<String,Integer>(); @Override public IBinder onBind(Intent intent) { // TODO: Return the communication channel to the service. throw new UnsupportedOperationException("Not yet implemented"); } @Override public int onStartCommand(Intent intent, int flags, int startId) { if (!player.isPlaying()) { player.start(); isplay = player.isPlaying(); } date.put("bird",R.raw.bird); date.put("chimes",R.raw.chimes); date.put("cock",R.raw.cock); date.put("cuckoo",R.raw.cuckoo); date.put("notify",R.raw.notify); date.put("ringout",R.raw.ringout); date.put("water",R.raw.water); Sring=intent.getStringExtra("Sring");//这算是收到了参数吗? return super.onStartCommand(intent, flags, startId); } @Override public void onCreate() { player = MediaPlayer.create(this,date.get(Sring)); } @Override public void onDestroy() { player.stop(); isplay = player.isPlaying(); player.release(); super.onDestroy(); } }
thnugvfghj 2019-06-28
  • 打赏
  • 举报
回复
引用 2 楼 风轻云淡v10 的回复:
把音乐列表传到Service里面
我没太明白……是不是和楼上那位的意思差不多?
thnugvfghj 2019-06-28
  • 打赏
  • 举报
回复
引用 1 楼 m-oj 的回复:
你弄个map映射一下就好了吧
感谢回复! 不好意思我实在十分小白,是这样子吗?我运行了一下,APP会闪退,不知道是哪里出了错?
m-oj 应用层 2019-06-28
  • 打赏
  • 举报
回复
你弄个map映射一下就好了吧
头发还没秃a 2019-06-28
  • 打赏
  • 举报
回复
把音乐列表传到Service里面

80,350

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧