我正在尝试使用音乐应用。 我使用MediaPlayer对象和 我的音乐剧在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;
}
}
答案 0 :(得分:1)
这是因为每当你按下这些按钮(播放,停止)时,就会创建一个BrodcastReceiver
的新实例(对于每个新的进入广播)。所以一旦你开始在MediaPlayer
的第一个实例中玩,你就无法在另一个实例中停止它。
除了在MediaPlayer
内设置BroadcastReceiver
(并使用它)是一个坏主意。
答案 1 :(得分:0)
当你在触摸时播放音乐时,你发送额外的价值给广播。当你试图停止或暂停时你发送另一个额外的,但是使用不同的值并检查广播接收器是否存在停止值然后停止你的音乐播放器。