服务没有得到意图

发布于 2024-12-17 20:57:28 字数 1382 浏览 0 评论 0原文

为了从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谷夏 2024-12-24 20:57:28

为了帮助您进行调试,请在您的服务中到处添加一些Toast:

Toast.makeText(getApplicationContext(), "Hello", Toast.LENGTH_SHORT).show();

从策略上讲,这将帮助您查看服务是否正确启动,服务是否启动其方法等。查明错误比修复错误更重要。

To help you debug, add some Toasts here and there in your service:

Toast.makeText(getApplicationContext(), "Hello", Toast.LENGTH_SHORT).show();

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文