[英]android media player wont play audio file
im making a music app just for fun. 我正在制作一个音乐应用程序只是为了好玩。 I can read all my music files from sd card but MediaPlayer wont play the sound.
我可以从SD卡读取所有音乐文件,但是MediaPlayer无法播放声音。 I have the path to the file which i pass to the media player with
setDataSource
but nothing happens and i get no exceptions. 我具有使用
setDataSource
传递给媒体播放器的文件的路径,但没有任何反应,我也没有异常。 Here is the code im using. 这是即时通讯使用的代码。
Uri songUri;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_player);
setUpViews();
MediaPlayer mp = new MediaPlayer();
try {
mp.setDataSource(songUri.toString());
mp.prepare();
} catch (IllegalArgumentException e) {
Toast.makeText(this, "ILLEGAL ARGUMENT EXCEPTION", Toast.LENGTH_LONG).show();
e.printStackTrace();
} catch (SecurityException e) {
Toast.makeText(this, "SECURITY EXCEPTION", Toast.LENGTH_LONG).show();
e.printStackTrace();
} catch (IllegalStateException e) {
Toast.makeText(this, "ILLEGAL STATE EXCEPTION", Toast.LENGTH_LONG).show();
e.printStackTrace();
} catch (IOException e) {
Toast.makeText(this, "IO EXCEPTION", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
mp.start();
Log.d("URI AFTER SET UP", songUri.toString());
}
As i said before nothing happens when i open this activity but i still get all the Log.d in the console. 正如我之前所说,当我打开此活动时什么也没有发生,但我仍然在控制台中获得了所有Log.d。 Is there any more configurations for the media player to play the music?
媒体播放器还可以播放音乐吗? Thanks in advance.
提前致谢。
setDataSource() requires a path argument . setDataSource()需要一个path参数。 You are passing the entire uri as string.
您将整个uri作为字符串传递。 Try this :
尝试这个 :
mp.setDataSource(songUri.getPath());
mp.prepare();
mp.start();
Moreover, if you are trying to access a file with content://
uri, it won't work. 此外,如果您尝试访问具有
content://
uri的文件,则该文件将无效。 You'll have to find the real path to the file, ie file://
uri. 您必须找到文件的真实路径,即
file://
uri。
I think you have to add setAudioStreamType to your MediaPlayer
Object. 我认为您必须将setAudioStreamType添加到
MediaPlayer
对象。 You can add this before mp.setDataSource(your_URI);
您可以在
mp.setDataSource(your_URI);
之前添加它mp.setDataSource(your_URI);
like below 像下面
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
There other types, you can choose AudioManager.STREAM_MUSIC
for Music Player 还有其他类型,您可以为Music Player选择
AudioManager.STREAM_MUSIC
I just solved the problem, and i just dont know why it works. 我只是解决了这个问题,而且我不知道为什么会这样。 What i did was to put a button to stop the music being played.
我所做的就是按下一个按钮以停止播放音乐。 I dont understand why it works so if someone could explain why it does i'd be thanksfull.
我不明白为什么会这样,所以如果有人可以解释为什么会这样,我会很感激的。
get the path of song by querying MEDIASTORE-EXTERNAL_URI .And this field MEDIASTORE.AUDIO.MEDIA.DATA will give u the path of the song(which is string). 通过查询MEDIASTORE-EXTERNAL_URI来获取歌曲的路径。此字段MEDIASTORE.AUDIO.MEDIA.DATA将为您提供歌曲的路径(字符串)。 U can set it directly to mediaplayer.setDataSource(Your_path_from_mediastore) .It will work fine than.
你可以直接将其设置为mediaplayer.setDataSource(Your_path_from_mediastore) 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.