在Android中使用Media使用URL播放媒体

问题描述 投票:0回答:3

我尝试使用媒体播放器从 URL 播放音乐,但播放时出现错误

        final MediaPlayer mp = new MediaPlayer();
    try {

        mp.setDataSource(url[0]);
        mp.prepare();
        mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mediaPlayer) {
                mp.start();
            }
        });
    } catch(Exception e){
        System.out.println(e);
    }

我也尝试过与 URI 一起使用

        Uri uri = Uri.parse(url[0]);
        mp.setDataSource(context,uri);
        mp.prepare();

还尝试使用 mp.prepareAsync();方法

但随后也会出现此类错误并且媒体无法播放

W/MediaPlayer: Couldn't open 
java.io.FileNotFoundException: No content provider: 
    at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:1675)
    at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:1505)
    at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:1422)
    at android.media.MediaPlayer.attemptDataSource(MediaPlayer.java:1101)
    at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1073)
    at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1001)
    at com.tuwy.Dailog.DialogClass.showAudioDialog(DialogClass.java:272)

也得到这个

W/AudioTrack: Use of stream types is deprecated for operations other than volume control
See the documentation of AudioTrack() for what to use instead with android.media.AudioAttributes to qualify your playback use case
V/AudioTrack: pause(2131): prior state:STATE_ACTIVE
I/System.out: java.io.FileNotFoundException: : open failed: ENOENT (No such file or directory)
android url uri android-mediaplayer
3个回答
0
投票

我有同样的问题,这段代码解决了我的问题

我使用了另一个线程从 url 播放音乐...

    final Handler handler =new Handler();

    final Thread audioThread = new Thread(new Runnable() {
        @Override
        public void run() {
            final MediaPlayer mediaPlayer = new MediaPlayer();

            Uri myUri = Uri.parse(url[0]);
            try {
                mediaPlayer.setDataSource(context, myUri);
                mediaPlayer.prepare();
            } catch (IOException e) {
                e.printStackTrace();
            }

            handler.post(new Runnable() {
                @Override
                public void run() {
                    btnPlayPause.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            if (mediaPlayer.isPlaying()) {
                                mediaPlayer.pause();
                            } else {
                                if (mediaPlayer.getCurrentPosition() > 0)
                                    mediaPlayer.seekTo(0);
                                mediaPlayer.start();
                            }
                        }
                    });
                }
            });
        }
    });

** url[0] 表示您的音乐网址,btnplaypause 是播放和暂停音频的按钮

希望这也能解决您的问题...


0
投票

尝试将 url 解析为 Uri.parse(url),这可能会起作用。


0
投票

文档是这样描述的 MediaPlayer 并且它可以工作

private var mediaPlayer: MediaPlayer? = null
fun playSound() {
    try {
        if (mediaPlayer != null) {
            mediaPlayer = if (mediaPlayer?.isPlaying!!) {
                mediaPlayer?.stop()
                mediaPlayer?.release()
                null
            }else{
                mediaPlayer?.release()
                null
            }
        }

        mediaPlayer = MediaPlayer().apply {
            setAudioAttributes(
                AudioAttributes.Builder()
                    .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
                    .setUsage(AudioAttributes.USAGE_MEDIA)
                    .build()
            )
            setDataSource(applicationContext, "https://zello.com/myy/c/BxG/profile.mp3".toUri())
            prepare()
            start()
        }
    } catch (e: Exception) {
        customLog("myTag", "welcomeMessage: Exception ${e.message}")
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.