音频播放(位于互联网上的文件)使用Kotlin。

huangapple go评论71阅读模式
英文:

Audio playback (file located on the internet) with kotlin

问题

在清单文件中添加以下内容:

<uses-permission android:name="android.permission.INTERNET" />

我的代码:

button1.setOnClickListener {
    val mediaPlayer = MediaPlayer()
    try {
        mediaPlayer.setDataSource("http://www.tutorialesprogramacionya.com/recursos/gato.mp3")
        mediaPlayer.prepare()
        mediaPlayer.start()
    } catch (e: IOException) {
        Toast.makeText(this, "No existe el archivo", Toast.LENGTH_LONG).show()
    }
}
英文:

I'm practicing with MediaPlayer and my problem is the moment I press the button, I don't hear the sound and the application crashes.

add this in the manifest

&lt;uses-permission android:name=&quot;android.permission.INTERNET&quot; /&gt;

My code

button1.setOnClickListener {
            val mediaPlayer = MediaPlayer()
            try {
                mediaPlayer.setDataSource(&quot;http://www.tutorialesprogramacionya.com/recursos/gato.mp3&quot;)
                mediaPlayer.prepare()
                mediaPlayer.start()
            } catch (e: IOException) {
                Toast.makeText(this, &quot;No existe el archivo&quot;, Toast.LENGTH_LONG).show()
            }

        }

答案1

得分: 0

阅读媒体播放器文档。
不要使用以下方式初始化媒体播放器:

MediaPlayer()

你需要使用以下方式创建它:

MediaPlayer.create(context, uri)

所以将上下文作为第一个参数传递,将您的URL作为URI传递,如下所示:

Uri.parse("YOUR_URL")
英文:

Read mediaplayer documentation.
Don't initialize mediaplayer with :

MediaPlayer()

You need to create it with :

MediaPlayer.create(context,uri)

So pass context as first parameter and your url as uri with this :

Uri.parse(&quot;YOUR_URL&quot;)

答案2

得分: 0

你可以编写以下代码:

button1.setOnClickListener {
    val mediaPlayer = MediaPlayer()
    try {
        mediaPlayer.setDataSource("http://www.tutorialesprogramacionya.com/recursos/gato.mp3")
        mediaPlayer.prepareAsync()
        mediaPlayer.setOnPreparedListener {
            mediaPlayer.start()
        }
    } catch (e: IOException) {
        Toast.makeText(this, "No existe el archivo", Toast.LENGTH_LONG).show()
    }
}

并且你需要在你的清单文件中添加权限:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
英文:

You can make:

button1.setOnClickListener {
            val mediaPlayer = MediaPlayer()
            try {
                mediaPlayer.setDataSource(&quot;http://www.tutorialesprogramacionya.com/recursos/gato.mp3&quot;)
                mediaPlayer.prepareAsync()
                mediaPlayer.setOnPreparedListener {
                        mediaPlayer.start()
                }
            } catch (e: IOException) {
                Toast.makeText(this, &quot;No existe el archivo&quot;, Toast.LENGTH_LONG).show()
            }

        }

And you need add permission to your manifest:

&lt;uses-permission android:name=&quot;android.permission.INTERNET&quot; /&gt;
&lt;uses-permission android:name=&quot;android.permission.ACCESS_NETWORK_STATE&quot; /&gt;

答案3

得分: 0

Step 1
在清单文件中添加以下内容:

&lt;uses-permission android:name=&quot;android.permission.INTERNET&quot; /&gt;

找到(<aplication)并在内部添加:

android:usesCleartextTraffic=&quot;true&quot;

按下alt + control键,然后选择(使用工具:targetApi属性进行抑制)。

Step 2
在MainActivity类下面:AppCompatActivity()之后,创建一个私有变量:

private val mediaPlayer = MediaPlayer()

最后删除以下内容:

val mediaPlayer = MediaPlayer()
英文:

Step 1
add this in the manifest

&lt;uses-permission android:name=&quot;android.permission.INTERNET&quot; /&gt;

located the (<aplication) and within add that:

android:usesCleartextTraffic=&quot;true&quot;

Press alt + control about it and select (Suppress with tools:targetApi attributes

Step 2
below the class MainActivity : AppCompatActivity()
Create a private val

private val mediaPlayer = MediaPlayer()

Finaly delete

val mediaPlayer = MediaPlayer()

huangapple
  • 本文由 发表于 2020年7月21日 19:24:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/63013436.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定