轻按时如何创建播放音频文件的通知? [英] How can I create a notification that plays an audio file when being tapped?

查看:92
本文介绍了轻按时如何创建播放音频文件的通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想显示一个通知,当用户点击它时,应该播放一个声音文件.

I would like to display a notification and when the user taps on it a sound file should be played.

在Android Studio中,我已将文件 test.mp3 复制到文件夹 app \ res \ raw 中.通知是通过以下代码发出的:

In Android Studio I have copied the file test.mp3 into the folder app\res\raw. The notification is emitted by this code:

Resources resources = getResources();                                                 
Uri uri = new Uri.Builder()                                                           
        .scheme(ContentResolver.SCHEME_ANDROID_RESOURCE)                              
        .authority(resources.getResourcePackageName(R.raw.test))                      
        .appendPath(resources.getResourceTypeName(R.raw.test))                        
        .appendPath(resources.getResourceEntryName(R.raw.test))                       
        .build();                                                                     
                                                                                      
Intent playSoundIntent = new Intent();                                                
playSoundIntent.setAction(android.content.Intent.ACTION_VIEW);                        
playSoundIntent.setDataAndType(uri, "audio/*");                                       
                                                                                      
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,                      
        playSoundIntent, 0);                                                          
                                                                                      
NotificationCompat.Builder builder = new NotificationCompat.Builder(this,             
        MainActivity.notificationChannelId)                                           
        .setSmallIcon(android.R.drawable.ic_media_play)                               
        .setContentTitle(getResources().getString(R.string.app_name))                 
        .setContentText("Tap to play sound!")                                         
        .setContentIntent(pendingIntent)                                              
        .setPriority(NotificationCompat.PRIORITY_DEFAULT)                             
        .setAutoCancel(true);                                                         

NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);

notificationManager.notify(12345678, builder.build());                                

它不能按预期方式工作.显示该通知,如果我点击它,它就会消失(由于 setAutoCancel(true)).但是我听不到声音.为什么?

It does not work as expected. The notification is displayed and if I tap it, it disappears (because of setAutoCancel(true)). But I hear no sound. Why?

我如何调试它?

非常感谢!

推荐答案

我通过创建这样的意图来管理它:

I have managed it by creating the intent like this:

Intent playSoundIntent = new Intent(this, PlaySoundActivity.class);

PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, playSoundIntent, 0);

并添加活动:

public class PlaySoundActivity extends AppCompatActivity
{
    private static MediaPlayer mediaPlayer;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        mediaPlayer = MediaPlayer.create(this, R.raw.test);
        mediaPlayer.start();
    }

    @Override
    public void onStop()
    {
        super.onStop();
        mediaPlayer.stop();
        mediaPlayer.release();
    }
}

尽管这行得通,但我仍然对为什么前一种方法行不通感兴趣.我已经看到了很多类似的代码片段.而且我仍然想知道如何调试以前的方法来理解错误.有人可以帮我吗?

Although this works, I am still interested in why the former approach didn't work. I have seen many code snippets like that. And still I wonder how I could debug the former approach to understand the error. Can anybody help me?

这篇关于轻按时如何创建播放音频文件的通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆