如何在用户按下通知后播放音频文件?

发布于 12-05 01:47 字数 2514 浏览 0 评论 0原文

我正在尝试借助应用程序从互联网下载歌曲。我对其进行编码以在下载完成时显示通知,但问题是我不知道单击通知时该怎么做。我希望它在单击通知时播放歌曲。

 String ns = Context.NOTIFICATION_SERVICE;
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
        int icon = R.drawable.m;        // icon from resources
        CharSequence tickerText = "Song Downloaded";              // ticker-text
        long when = System.currentTimeMillis();         // notification time
        Context context = getApplicationContext();      // application Context
        CharSequence contentTitle = name;  // expanded message title
        CharSequence contentText = "Your song "+name +" has been Song Downloaded";      // expanded message text

        Intent notificationIntent = new Intent(this, notif.class);
        notificationIntent.putExtra("song", name);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

        // the next two lines initialize the Notification, using the configurations above
        Notification notification = new Notification(icon, tickerText, when);
        notification.sound = Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "6");
        notification.flags = Notification.DEFAULT_LIGHTS & Notification.FLAG_AUTO_CANCEL; 
        notification.ledARGB = 0xff0000ff;
        notification.ledOnMS = 500;
        notification.ledOffMS = 3000;
        notification.defaults |= Notification.DEFAULT_LIGHTS;
        notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
        final int HELLO_ID = 1;

        mNotificationManager.notify(HELLO_ID, notification);

这清楚地表明意图启动 notif.class 但我不知道在 notif.class 中做什么我尝试了这个,但是当我单击 notif 时它仍然不显示任何内容。

 class notif extends Activity
    {   
MediaPlayer abc=new MediaPlayer();
public void onCreate(Bundle icicle){
File SDCardRoot = Environment.getExternalStorageDirectory();
Intent intent = getIntent();        
Bundle bundle = intent.getExtras();                
String name = bundle.getString("song");
try {
    abc.setDataSource(SDCardRoot.getAbsolutePath() + "/Music/"+name+".mp3");
    abc.prepare();
} catch (IllegalArgumentException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IllegalStateException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

   if(name!="")
{ abc.start(); }
  }
   }

I am trying to download a song from the internet with the help of a app. I coded it to show a notification when the download is complete but the problem is i dont know what to do when the notification is clicked. I want it to play the song when the notification is clicked.

 String ns = Context.NOTIFICATION_SERVICE;
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
        int icon = R.drawable.m;        // icon from resources
        CharSequence tickerText = "Song Downloaded";              // ticker-text
        long when = System.currentTimeMillis();         // notification time
        Context context = getApplicationContext();      // application Context
        CharSequence contentTitle = name;  // expanded message title
        CharSequence contentText = "Your song "+name +" has been Song Downloaded";      // expanded message text

        Intent notificationIntent = new Intent(this, notif.class);
        notificationIntent.putExtra("song", name);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

        // the next two lines initialize the Notification, using the configurations above
        Notification notification = new Notification(icon, tickerText, when);
        notification.sound = Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "6");
        notification.flags = Notification.DEFAULT_LIGHTS & Notification.FLAG_AUTO_CANCEL; 
        notification.ledARGB = 0xff0000ff;
        notification.ledOnMS = 500;
        notification.ledOffMS = 3000;
        notification.defaults |= Notification.DEFAULT_LIGHTS;
        notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
        final int HELLO_ID = 1;

        mNotificationManager.notify(HELLO_ID, notification);

This clearly shows that intent starts notif.class but i dont know what to do in notif.class i tried this but when i click on the notif it still doesnt show anything.

 class notif extends Activity
    {   
MediaPlayer abc=new MediaPlayer();
public void onCreate(Bundle icicle){
File SDCardRoot = Environment.getExternalStorageDirectory();
Intent intent = getIntent();        
Bundle bundle = intent.getExtras();                
String name = bundle.getString("song");
try {
    abc.setDataSource(SDCardRoot.getAbsolutePath() + "/Music/"+name+".mp3");
    abc.prepare();
} catch (IllegalArgumentException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IllegalStateException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

   if(name!="")
{ abc.start(); }
  }
   }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

凉城凉梦凉人心2024-12-12 01:47:32

试试这个:

   @Override
   public void onNewIntent(Intent intent)  {

            Bundle extras = intent.getExtras();
            if (extras!=null) {
                        ...
            } 


   }

try this:

   @Override
   public void onNewIntent(Intent intent)  {

            Bundle extras = intent.getExtras();
            if (extras!=null) {
                        ...
            } 


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