我们可以从应用程序文件路径播放音频文件吗?

时间:2019-07-02 10:11:22

标签: android android-mediaplayer audio-player android-music-player android-fileprovider

我正在开发mp3音乐播放器应用程序。当用户尝试从其他应用程序播放音频文件时,我希望在我的应用程序中播放音频文件。

当用户尝试从 Xender 应用播放音频文件时,该文件将通过应用对话框打开。从该对话框中,用户将选择我的应用,并且该应用将准备播放选定的歌曲。

我正在尝试播放最新版本的Android Pie中的歌曲。我得到了文件路径URL,但 Android MediaPlayer 无法播放以下路径的音频。

  

/data/user/0/com.mp3musicapp/files/mumbai dilli di kudiyaan-songs.pk-320kbps.mp3

所以,请有人帮助我解决这个问题。我从最近两天开始尝试,但是到现在为止都没有任何解决方案。所以请帮我解决这个问题。我该如何解决?

应用代码:

Intent returnIntent = getIntent(); 
Uri returnUri = returnIntent.getData(); 
String mimeType = getContentResolver().getType(returnUri);

if (mimeType != null) {
                if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
                    if (filePathHelper.getPathnew(returnUri, getApplicationContext()) != null) {

                        audioRealPath = filePathHelper.getPathnew(returnUri, getApplicationContext()).toLowerCase();
                    } else {
                        audioRealPath = filePathHelper.getFilePathFromURI(returnUri, getApplicationContext()).toLowerCase();
                    }
                } else {
                    audioRealPath = filePathHelper.getPath(returnUri, getApplicationContext()).toLowerCase();
                }
            }

 Uri tempAudioUri = Uri.fromFile(new File(audioRealPath));


                File filePath = new File(tempAudioUri.toString());
                if (!filePath.exists()) {
                    filePath.createNewFile();
                }
                FileInputStream is = new FileInputStream(filePath);


                MediaPlayer mPlayer = new MediaPlayer();

                try {
                    mPlayer.setDataSource(is.getFD());
                } catch (IllegalArgumentException e) {
                    Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
                } catch (SecurityException e) {
                    Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
                } catch (IllegalStateException e) {
                    Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
                } catch (IOException e) {
                    e.printStackTrace();
                }

                try {
                    mPlayer.prepare();
                } catch (IllegalStateException e) {
                    Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
                } catch (IOException e) {
                    Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
                }

                mPlayer.start();

0 个答案:

没有答案