2014-11-15 169 views
0

我有一個列表視圖,它顯示軌道列表。每個列表項有播放和停止圖標播放和停止跟蹤。我遇到的問題是,如果我點擊多個播放按鈕MediaPlayer同時播放多個音頻,然後如果我嘗試停止多個音頻應用程序crashes.how以防止MediaPlayer同時播放多個音頻?我希望如果mediaplayer正在播放一個音軌並且用戶單擊另一個音軌播放按鈕mediaplyer停止播放上一首曲目和播放點擊播放按鈕,用戶的軌跡..點擊多個播放按鈕MediaPlayer同時開始播放多個音頻

回答

0

必須創建MediaPlayer對象只有一個實例,在適配器構造函數,像這樣:

public class SoundCloudAdapter extends BaseAdapter{ 

    private MediaPlayer mMediaPlayer; 
    private Context context; 
    private ArrayList<SoundCloud> soundcloudList; 

    public SoundCloudAdapter(Context context, ArrayList<SoundCloud> soundcloudList) 
    { 
     this.context=context; 
     this.soundcloudList = soundcloudList; 
    mMediaPlayer = new MediaPlayer(); 
    } 

    @Override 
    public long getItemId(int position) 
    { 
     return position; 
    } 

    @Override 
    public int getCount() 
    { 
     return soundcloudList.size(); 
    } 

    @Override 
    public Object getItem(int position) 
    { 
     return soundcloudList.get(position); 
    } 

    @Override 
    public View getView(final int position, View convertView, ViewGroup parent) 
    { 
     final ViewHolder holder; 

     if (convertView == null){ 
      holder = new ViewHolder(); 
      convertView = LayoutInflater.from(context).inflate(R.layout.soundcloud_item, null); 
      holder.textview = (TextView) convertView.findViewById(R.id.title); 
      holder.textview2 = (TextView) convertView.findViewById(R.id.id); 
      holder.textview3 = (TextView) convertView.findViewById(R.id.download); 
      // holder.btn = (Button) convertView.findViewById(R.id.button); 
      holder.img1 = (ImageView) convertView.findViewById(R.id.play2); 
      holder.img2 = (ImageView) convertView.findViewById(R.id.pause); 
      convertView.setTag(holder); 
     }else{ 
      holder = (ViewHolder) convertView.getTag(); 
     } 

     holder.textview.setText(soundcloudList.get(position).getTitle()); 
     holder.textview2.setText(soundcloudList.get(position).getId()); 
     holder.textview3.setText("Play Count : "+soundcloudList.get(position).getDownload()); 
     if(soundcloudList.get(position).getPlaying()) 
     { 
      holder.img1.setVisibility(View.GONE); 
      holder.img2.setVisibility(View.VISIBLE); 
     } 
     else 
     { 
      holder.img1.setVisibility(View.VISIBLE); 
      holder.img2.setVisibility(View.GONE); 
     } 
     holder.img1.setOnClickListener(new View.OnClickListener() 
     { 
      @Override 
      public void onClick(final View v) 
      { 
       soundcloudList.get(position).setPlaying(true); 
       try 
       { 
      holder.img1.setVisibility(View.INVISIBLE); 
        holder.img2.setVisibility(View.VISIBLE); 

        if(mMediaPlayer.isPlaying()) 
        { 
         mMediaPlayer.seekTo(0); 
      mMediaPlayer.pause(); 
        } 

        mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); 
        mMediaPlayer.setDataSource("http://api.soundcloud.com/tracks/" + soundcloudList.get(position).getId() + "/stream?client_id=xyz"); 
        mMediaPlayer.prepareAsync(); 
        mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() 
        { 
         @Override 
         public void onPrepared(MediaPlayer mp) 
         { 
          mp.start(); 

         } 
        }); 
        mMediaPlayer.setOnCompletionListener(
          new MediaPlayer.OnCompletionListener() 
          { 
           @Override 
           public void onCompletion(MediaPlayer mp) 
           { 
            soundcloudList.get(position).setPlaying(false); 
            holder.img1.setVisibility(View.VISIBLE); 
            holder.img2.setVisibility(View.INVISIBLE); 

           } 
          }); 
       } 
       catch (Exception e) 
       { 
        e.printStackTrace(); 
       } 

      } 

     }); 
     holder.img2.setOnClickListener(new View.OnClickListener() 
     { 

      @Override 
      public void onClick(final View v) 
      { 
       soundcloudList.get(position).setPlaying(false); 


       holder.img1.setVisibility(View.VISIBLE); 
       holder.img2.setVisibility(View.INVISIBLE); 

     mMediaPlayer.seekTo(0); 
     mMediaPlayer.pause(); 


      } 

     }); 

     return convertView; 

    } 

    static class ViewHolder 
    { 
     public TextView textview; 
     public TextView textview2; 
     public TextView textview3; 
     public ImageView img1; 
     public ImageView img2; 
     public Button btn; 
    } 

} 
+0

好的..puting mMediaPlayer = new MediaPlayer(); SoundCloudAdapter構造函數在第二次單擊播放按鈕時不會播放音頻..如果我播放了一首曲目然後停止播放,然後嘗試播放相同或另一首曲目,那麼它將不會播放..如果我正在播放曲目並且點擊另一首歌曲的播放圖標它會停止當前曲目但不播放另一首歌曲。 –

+0

同樣的問題..如果我播放了一首曲目然後停下來然後嘗試播放相同或另一首曲目,那麼它將無法播放..如果我正在播放曲目並單擊其他曲目的播放圖標,它將停止當前曲目但不播放另一曲目。 –

+0

我認爲您正在更新適配器幾次... 您的解決方案需要將mediaPlayer對象傳遞給構造函數... –

0

是我遇到的問題是,如果我點擊多種播放按鈕 MediaPlaye [R在同一時間

它的播放多個音頻,因爲你創建MediaPlayer每次onClick的新實例trigerred:

mMediaPlayer = new MediaPlayer(); 

創建實例一次(在onCreate,例如),只是改變在onClick方法中的url。

+0

我試圖謂also..if我把mMediaPlayer =新的MediaPlayer();裏面的getView()然後音頻不會暫停點擊暫停按鈕..一旦播放它繼續播放.. –

+0

檢查出更新的問題與SoundCloudFragment.java和SoundCloud.java代碼 –

+0

如果我把mMediaPlayer = new MediaPlayer();在SoundCloudAdapter構造函數軌道內不會每隔兩次播放時,我點擊播放圖標。這意味着如果我第一次點擊播放圖標,然後跟蹤開始播放,然後單擊停止圖標後,如果我嘗試通過單擊再次播放曲目在播放圖標,然後它不會播放,但圖標確實從播放圖標更改爲停止圖標,然後單擊停止圖標後,如果我再次嘗試播放曲目,則曲目開始播放。總體而言,曲目無法播放每隔一秒我點擊播放圖標 –

相關問題