我正在試聽音樂應用程序。 我使用MediaPlayer對象和 我的音樂在Broadcastrecevier中播放。 當我觸摸列表中的任何歌曲時,音樂正在播放。 但我的停止,重置或暫停功能不起作用。不停止broadcastrecevier中的當前歌曲
MediaPlayerBroadcast.java
public class MediaPlayerBroadcast extends BroadcastReceiver {
private MediaPlayer mPlayer = new MediaPlayer();
@Override
public void onReceive(Context context, Intent intent) {
Bundle get_extras = intent.getExtras();
if(get_extras.getString("music_name").equals("stop")) {
mPlayer.stop();
}
else {
String url = "http://www.tupbebekailesi.com/musics/" + get_extras.getString("music_name");
mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mPlayer.setDataSource(url);
} catch (IllegalArgumentException e) {
Toast.makeText(context, "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
} catch (SecurityException e) {
Toast.makeText(context, "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
} catch (IllegalStateException e) {
Toast.makeText(context, "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
}
try {
mPlayer.prepare();
} catch (IllegalStateException e) {
Toast.makeText(context, "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
} catch (IOException e) {
Toast.makeText(context, "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
}
mPlayer.start();
}
}
}
和MusicActivity.java
public class MusicActivity extends Fragment implements AdapterView.OnItemClickListener {
private ListView music_list;
private MusicAdapter musicAdapter;
private List<String> music_list_name;
private Button stop_music;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.music_activity,container,false);
music_list = (ListView) rootView.findViewById(R.id.music_list);
stop_music = (Button) rootView.findViewById(R.id.stop);
music_list_name = new ArrayList<String>();
music_list_name.add("deadmau5-Alone-With-You.mp3");
music_list_name.add("deadmau5-Bad-Selection.mp3");
music_list_name.add("Deadmau5-Clockwork.mp3");
music_list_name.add("Deadmau5-Faxing-Berlin.mp3");
music_list_name.add("Deadmau5-Ghosts-n-Stuff.mp3");
music_list.setOnItemClickListener(this);
stop_music.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent start_music = new Intent("com.olkun.medyasef.merhaba");
//Here I am stop music.
start_music.putExtra("music_name","stop");
getActivity().sendBroadcast(start_music);
}
});
musicAdapter = new MusicAdapter(getActivity(),music_list_name);
music_list.setAdapter(musicAdapter);
return rootView;
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String name = music_list_name.get(position);
Log.d("BASLA",name);
//here I am start any music.
Intent start_music = new Intent("com.olkun.medyasef.merhaba");
start_music.putExtra("music_name",name);
getActivity().sendBroadcast(start_music);
}
private class MusicAdapter extends BaseAdapter {
private Context mContext;
private List<String> music_array_list;
private MusicAdapter(Context mContext, List<String> music_array_list) {
this.mContext = mContext;
this.music_array_list = music_array_list;
}
@Override
public int getCount() {
return music_array_list.size();
}
@Override
public String getItem(int position) {
return music_array_list.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertview, ViewGroup parent) {
ViewHolder holder = null;
String music_list = getItem(position);
if(convertview == null){
convertview = LayoutInflater.from(mContext).inflate(R.layout.music_activity_area,parent,false);
holder = new ViewHolder();
holder.music_name = (TextView) convertview.findViewById(R.id.music_name);
convertview.setTag(holder);
}
else {
holder = (ViewHolder) convertview.getTag();
}
holder.music_name.setText(music_list);
return convertview;
}
}
/*private view holder class*/
private class ViewHolder {
TextView music_name;
}
}
一個比。如果broadcastRecevier壞主意,那麼我應該使用什麼?服務還是其他? –
是的,「服務」是這裏最好的選擇。您仍然可以使用廣播來與您的服務進行通信(儘管使用LocalBroadcasts更好)。如果你不想要,你不需要綁定到你的服務。 – Kristopher
Thanx再次。我會試試看。 –