MediaPlayer mediaPlayer = new MediaPlayer();
mp.setDataSource(context, Uri.parse("http://www.domian.com/ola.mp3"));
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
mp.prepare();
mp.setOnPreparedListener(new OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
mediaPlayer.setOnErrorListener(new OnErrorListener() {
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
// Display error
return false;
}
});
Android: cant play the mp3 file from the given url
-
08-07-2023 - |
Вопрос
I want to know about the android media player. I have designed a media player such that it will take the url and play the song when i select the play button when i tested in my tablet it is not working if i select the button it will not play or give the error neither crashing is happening.
i have a question in mind that whether the authentication is needed like example while researching i saw the android.drm etc because i am linking the url to the server
can anyone help me in this
i have posted the code which i have used even the logcat details as well. logcat will tell about GPU so i tested in my tablet as well still i cant listen to the mp3 file
public class MainActivity extends Activity
{
static final String AUDIO_PATH = "url";
private MediaPlayer mediaPlayer;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void doClick(View view) {
switch(view.getId()) {
case R.id.play:
try {
playAudio(AUDIO_PATH);
} catch (Exception e) {
e.printStackTrace();
}
break;
case R.id.stop:
if(mediaPlayer != null) {
mediaPlayer.stop();
}
break;
}
}
private void playAudio(String url) throws Exception
{
killMediaPlayer();
mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(url);
mediaPlayer.prepare();
mediaPlayer.start();
}
@Override
protected void onDestroy() {
super.onDestroy();
killMediaPlayer();
}
private void killMediaPlayer() {
if(mediaPlayer!=null) {
try {
mediaPlayer.release();
}
catch(Exception e) {
e.printStackTrace();
}
}
}}
main.xml file is as shown below
<EditText
android:id="@+id/EditTextSongURL"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.54"
android:ems="10"
android:inputType="textMultiLine" >
<requestFocus />
</EditText>
<ImageButton
android:id="@+id/ButtonTestPlayPause1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/play" />
<SeekBar
android:id="@+id/SeekBarTestPlay"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
in logcat there are no errors as such excluding Gpu
Решение
Другие советы
Try this:
MediaPlayer mp=new MediaPlayer();
try {
mp.setDataSource(url);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
mp.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mp.start();
mp.setLooping(true);
}