在我的应用程序中,在MediaPlayer中在线歌曲不播放。它进入准备状态。我已经放了我的代码。当它在模拟器上运行时,它会显示黑屏,这意味着它应该处于等待模式。 我有很多方法可以播放歌曲,但它无法播放。当我使用调试器并且当mediaplayer进入prepare()状态时,它应该是黑屏,在logcat或其他任何地方没有任何输出屏幕。 我的代码如下:
package com.yeshuduniya;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import com.Model.GallryModel;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnErrorListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.PowerManager;
import android.util.Patterns;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.webkit.URLUtil;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;
import android.os.PowerManager.WakeLock;
public class Songs_Player extends Activity implements OnClickListener ,OnCompletionListener
{
Uri uri;
URL url;
private MediaPlayer mediaPlayer = null;
private boolean isPlaying = false;
private String song_url;
Handler seekHandler = new Handler();
TextView txt_song_name;
private static int classID = 579; // just a number
SeekBar seek_bar;
WakeLock wakeLock;
ProgressDialog progressDialogue;
private static final String[] EXTENSIONS = { ".mp3", ".mid", ".wav", ".ogg", ".mp4" }; //Playable Extensions
List<String> trackArtworks; //Track artwork names
ImageView bg; //Track artwork
Button btn_Play,btn_prev,btn_next; //The play button will need to change from 'play' to 'pause', so we need an instance of it
int currentTrack; //index of current track selected
int type; //0 for loading from assets, 1 for loading from SD card
ArrayList<GallryModel> songList;
@SuppressWarnings({ "unchecked", "deprecation" })
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_songs__player);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
//setVolumeControlStream(AudioManager.STREAM_MUSIC);
PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
wakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, "Lexiconda");
setUI();
Intent mIntent = getIntent();
songList=(ArrayList<GallryModel>) mIntent.getSerializableExtra("SongsList");
currentTrack = mIntent.getIntExtra("position", 0);
if(savedInstanceState ==null)
{
mediaPlayer=((YeshuDuniaApplication) Songs_Player.this.getApplication()).medaiplayer;
if(mediaPlayer.isPlaying())
{
stop();
playTrack(currentTrack);
}
else
{
playTrack(currentTrack);
//Toast.makeText(getBaseContext(), "Loaded " + Integer.toString(songList.size()) + " Tracks", Toast.LENGTH_SHORT).show();
}
//play(songList.get(currentTrack).getImage());
btn_Play.setBackgroundResource(R.drawable.pause);
}
btn_next.setOnClickListener(this);
btn_prev.setOnClickListener(this);
btn_Play.setOnClickListener(this);
}
private void setUI()
{
//seek_bar = (SeekBar) findViewById(R.id.seek_bar);
btn_next=(Button)findViewById(R.id.btn_mgallary_next);
btn_prev=(Button)findViewById(R.id.btn_mgallary_previous);
btn_Play=(Button)findViewById(R.id.btn_mgallary_Play);
bg = (ImageView) findViewById(R.id.bg);
txt_song_name=(TextView)findViewById(R.id.txt_song_name);
}
// Runnable run = new Runnable() {
//
// @Override
// public void run() {
// seekUpdation();
// }
// };
// public void seekUpdation() {
//
// seek_bar.setProgress(mediaPlayer.getCurrentPosition());
// seekHandler.postDelayed(run, 1000);
// }
@Override
public void onResume(){
super.onResume();
wakeLock.acquire();
}
//Play Songs.
@SuppressWarnings("deprecation")
public void play(int currentTrack) throws MalformedURLException
{ this.currentTrack=currentTrack;
System.out.println("Playiong track is"+currentTrack);
this.song_url=songList.get(currentTrack).getSongUrl();
if (!isPlaying)
{
/*
try {
if(URLUtil.isValidUrl(song_url) || Patterns.WEB_URL.matcher(song_url).matches()) {
song_url = URLEncoder.encode(song_url,"UTF-8");
url = new URL(song_url);
// so on
} else {
Toast.makeText(getBaseContext(), "Please enter valid URL.", Toast.LENGTH_SHORT).show();
} */
isPlaying = true;
btn_Play.setBackgroundResource(R.drawable.pause);
try {
url = new URL(song_url);
mediaPlayer.setLooping(false);
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
uri = Uri.parse(url.toURI().toString());
mediaPlayer.setDataSource(uri.toString());
//mediaPlayer.setOnPreparedListener(this);
mediaPlayer.prepare();
mediaPlayer.start();
mediaPlayer.setOnCompletionListener(this);
// When song is ended then media player automatically called onCompletion method.
}
catch (MalformedURLException e)
{
btn_Play.setBackgroundResource(R.drawable.play);
isPlaying=false;
Toast.makeText(getBaseContext(), "File is not mp3 supported", Toast.LENGTH_SHORT).show();
e.printStackTrace();
} catch (IllegalArgumentException e)
{
btn_Play.setBackgroundResource(R.drawable.play);
isPlaying=false;
Toast.makeText(getBaseContext(), "File is not mp3 supported", Toast.LENGTH_SHORT).show();
e.printStackTrace();
} catch (SecurityException e)
{
btn_Play.setBackgroundResource(R.drawable.play);
isPlaying=false;
Toast.makeText(getBaseContext(), "File is not mp3 supported", Toast.LENGTH_SHORT).show();
e.printStackTrace();
} catch (IllegalStateException e)
{
btn_Play.setBackgroundResource(R.drawable.play);
isPlaying=false;
Toast.makeText(getBaseContext(), "File is not mp3 supported", Toast.LENGTH_SHORT).show();
e.printStackTrace();
} catch (IOException e)
{
btn_Play.setBackgroundResource(R.drawable.play);
isPlaying=false;
Toast.makeText(getBaseContext(), "File is not mp3 supported", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
catch (URISyntaxException e1)
{
btn_Play.setBackgroundResource(R.drawable.play);
isPlaying=false;
Toast.makeText(getBaseContext(), "File is not mp3 supported", Toast.LENGTH_SHORT).show();
e1.printStackTrace();
}
}
else
{
stop();
play(currentTrack);
}
}
//Stop song.
public void stop()
{
synchronized(this)
{
isPlaying = false;
if (mediaPlayer != null)
{
mediaPlayer.reset();
}
//stopForeground(true);
}
}
//Pause song.
public void pause()
{
if (isPlaying) {
isPlaying = false;
if (mediaPlayer != null)
{
mediaPlayer.pause();
mediaPlayer = null;
}
}
}
@Override
public void onPause()
{
super.onPause();
wakeLock.release();
}
//Generate a String Array that represents all of the files found
//Plays the Track
public void playTrack(int currentTrack)
{
this.currentTrack=currentTrack;
//this.song_url=currentTrack;
if(!isPlaying )
{
try {
play(currentTrack);
Toast.makeText(Songs_Player.this, "Playing " + songList.get(currentTrack).getSongTitle(), Toast.LENGTH_SHORT).show();
} catch (MalformedURLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
@Override
public void onClick(View v)
{ boolean flag=false;
switch(v.getId())
{
case R.id.btn_mgallary_Play:
synchronized(this){
if(isPlaying)
{
isPlaying = false;
if (mediaPlayer != null)
{
mediaPlayer.pause();
}
btn_Play.setBackgroundResource(R.drawable.play);
}
else
{
isPlaying = true;
mediaPlayer.start();
btn_Play.setBackgroundResource(R.drawable.pause);
//playTrack(songList.get(currentTrack).getImage());
}
}
return;
case R.id.btn_mgallary_previous:
stop();
if(currentTrack == 0)
{
isPlaying=false;
currentTrack=songList.size()-1;
playTrack(currentTrack);
}
else
{
isPlaying=false;
currentTrack=currentTrack-1;
btn_Play.setBackgroundResource(R.drawable.play);
playTrack(currentTrack);
}
return;
case R.id.btn_mgallary_next:
stop();
if(currentTrack == songList.size()-1)
{
isPlaying=false;
currentTrack=0;
btn_Play.setBackgroundResource(R.drawable.play);
playTrack(currentTrack);
}
else
{
isPlaying=false;
btn_Play.setBackgroundResource(R.drawable.play);
currentTrack=currentTrack+1;
playTrack(currentTrack);
}
return;
default:
return;
}
}
@Override
public void onCompletion(MediaPlayer mediaPlayer)
{
mediaPlayer.stop();
mediaPlayer.reset();
if(currentTrack != songList.size()-1)
{ isPlaying=false;
currentTrack=currentTrack+1;
btn_Play.setBackgroundResource(R.drawable.play);
playTrack(currentTrack);
}
else
{ if(currentTrack == 0)
{
isPlaying=false;
currentTrack=songList.size()-1;
btn_Play.setBackgroundResource(R.drawable.play);
playTrack(currentTrack);
}
else
{
if(currentTrack == songList.size()-1)
{
isPlaying=false;
currentTrack=0;
btn_Play.setBackgroundResource(R.drawable.play);
playTrack(currentTrack);
}
}
}
}
}
我已经使用URLEncoder来删除空格,但它应该没有找到url。我的歌曲网址是在浏览器上播放的。所以Plz给我建议。 我的网址是,
http://www.yeshuduniya.com/admin/yeshuDuniyaMusic/34_Prabhu tu mahan / 34_Prabhu tu mahan.mp3 http://www.yeshuduniya.com/admin/yeshuDuniyaMusic/bhoole_aur_bhatke_they_hum/14_bhoole_aur_bhatke_they_hum.mp3