【问题标题】:How to stream music from url with Java - Android如何使用 Java 从 url 流式传输音乐 - Android
【发布时间】:2018-03-28 09:46:58
【问题描述】:

我对 Android 开发和一般的 Android 设备完全陌生,所以我不知道这里的情况如何。

我想制作一个应用程序,它可以从我的网址流式传输音乐,并且在我最小化应用程序后仍然播放歌曲。

我搜索了我的问题,但很多答案都是针对 mp3 歌曲或其他类型的,但我的网址来自现场广播,所以它不仅仅是一首歌曲。

我找到并且对我的问题有好处的答案之一是this 并使用此代码:

Uri myUri = Uri.parse("your url here");
Intent intent = new Intent(android.content.Intent.ACTION_VIEW); 
intent.setDataAndType(myUri, "audio/*"); 
startActivity(intent);

这提示我选择音乐播放器。

有什么方法可以只按我的“播放”按钮来听音乐吗?

在我的 iOS 应用程序中,我使用此代码,无需外部播放器即可随时启动和停止流媒体音乐:

func prepareToPlay() {
        
        let url = URL(string: "myUrl")
        
        playerItem = AVPlayerItem(url: url!)
        
        player = AVPlayer(playerItem: playerItem)
          
        player?.play()
}

提前致谢

编辑

在 cmets 中提出建议并回答后,我尝试使用 MPlayer 播放它,我创建了一个函数,当我像这样点击我的按钮时调用它:

public void playM() {
    String url = "http://android.programmerguru.com/wp-content/uploads/2013/04/hosannatelugu.mp3";

    mPlayer = new MediaPlayer();
    mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    try {
        mPlayer.setDataSource(url);
    } 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();
}

但我收到一个错误(第四条消息),我在日志中看到:

无法创建媒体播放器

在状态 1 中调用prepareAsync,mPlayer(0x0)

在状态 1 中开始调用,mPlayer(0x0)

错误 (-38, 0)

【问题讨论】:

  • 您的服务器使用的是什么network protocol
  • @CommonsWare 它使用http
  • AFAIK,这不够具体——请参阅我链接到的文档。欢迎您将 URL 交给MediaPlayer,看看它是否有效。
  • @CommonsWare 如果需要,请检查我的编辑。
  • "无法创建媒体播放器" => 似乎您没有在清单“”中添加权限

标签: java android ios audio stream


【解决方案1】:

在大多数情况下,带有 ACTION 标志的 Intent 旨在打开另一个应用程序。既然你不需要它。您想要自己的自定义播放器。所以Android有一个Media Player类来处理这种场景。

创建它的实例并传递您的流 URL。现在,设置数据源,然后在onBtnClickListener() 中调用prepare() 调用mp.start() 开始音乐

Uri myUri = ....; // initialize Uri here
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource(getApplicationContext(), myUri);
mediaPlayer.prepare();
mediaPlayer.start();


P.S:捕获所有异常并确保清单文件中的PERMISSIONS

【讨论】:

    【解决方案2】:

    Intent 仅用于在活动/服务和系统之间发送一些数据。它不会播放音乐。除了对某些活动说要做什么之外,它什么也不做。您需要播放多媒体流的机制。您应该使用MediaPlayer 类在您的应用程序中播放多媒体。

    这里有一些教程,如何从流中播放音乐:http://programmerguru.com/android-tutorial/android-mediaplayer-example-play-from-internet/

    【讨论】:

    • 我试过但没用,如果你想检查我的编辑
    • 你在页面末尾看到添加权限代码了吗?
    • 当我检查一切是否正常时,我注意到我第一次播放播放按钮时音乐开始 8 秒,然后停止大约 10 秒(没有按任何按钮或最小化应用程序)音乐不停地播放..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-28
    • 1970-01-01
    • 1970-01-01
    • 2010-12-30
    • 2012-11-23
    • 1970-01-01
    相关资源
    最近更新 更多