英文:
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
<uses-permission android:name="android.permission.INTERNET" />
My code
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()
}
}
答案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("YOUR_URL")
答案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("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()
}
}
And you need add permission to your manifest:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
答案3
得分: 0
Step 1
在清单文件中添加以下内容:
<uses-permission android:name="android.permission.INTERNET" />
找到(<aplication)并在内部添加:
android:usesCleartextTraffic="true"
按下alt + control键,然后选择(使用工具:targetApi属性进行抑制)。
Step 2
在MainActivity类下面:AppCompatActivity()之后,创建一个私有变量:
private val mediaPlayer = MediaPlayer()
最后删除以下内容:
val mediaPlayer = MediaPlayer()
英文:
Step 1
add this in the manifest
<uses-permission android:name="android.permission.INTERNET" />
located the (<aplication) and within add that:
android:usesCleartextTraffic="true"
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()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论