2013-11-26 69 views
0

我正在試聽音樂應用程序。 我使用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; 
    } 

} 

回答

1

這是因爲每次按下這些按鈕(播放,停止)時,都會創建一個BrodcastReceiver的新實例(用於每個新輸入的廣播)。因此,一旦您開始在MediaPlayer的第一個實例中播放,您無法在其另一個實例中停止播放。 在BroadcastReceiver裏面除了指定MediaPlayer(並使用它)是一個壞主意。

+0

一個比。如果broadcastRecevier壞主意,那麼我應該使用什麼?服務還是其他? –

+0

是的,「服務」是這裏最好的選擇。您仍然可以使用廣播來與您的服務進行通信(儘管使用LocalBroadcasts更好)。如果你不想要,你不需要綁定到你的服務。 – Kristopher

+0

Thanx再次。我會試試看。 –

0

當你觸摸播放音樂,你送把額外的價值播出。當您嘗試停止或暫停時,發送另一個額外的,但具有不同的值並在廣播接收器中檢查是否存在停止值,然後停止您的音樂播放器。

相關問題