2017-12-20 15 views
0

firebaseからデータを取得するミュージックストリームアプリを作成しましたが、曲を再生して戻ってきて再び現れたときに私のアプリに1つの問題があります。だから、私は共有設定を使用すべきだと思うが、私はいずれかが私の音楽プレーヤーのための私のコードを提案することができます使用する方法を知られていない音楽プレーヤーの共有設定

public class ViewUploadsActivity extends AppCompatActivity implements 
     MediaPlayer.OnBufferingUpdateListener,View.OnTouchListener 
       ,MediaPlayer.OnCompletionListener{ 
    //the listview 
    ListView listView; 
    private SeekBar seekBarProgress; 
    private int mediaFileLengthInMilliseconds; 
    private final Handler handler = new Handler(); 
    CustomArrayAdapter adapter; 
    //database reference to get uploads data 
    DatabaseReference mDatabaseReference; 

//list to store uploads data 
    List<Upload> uploadList = new ArrayList<>(); 
    private TextView mSelectedTrackTitle,selected_track_ar; 
    private ImageView mSelectedTrackImage; 
    private MediaPlayer mMediaPlayer =new MediaPlayer(); 
    private ImageView mPlayerControl; 
    Toolbar toolbar2; 


    TextView length; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_view_upload); 
    listView = (ListView) findViewById(R.id.listView); 
    //status = (TextView) findViewById(R.id.status); 

    seekBarProgress = (SeekBar)findViewById(R.id.length); 

    listView.setStackFromBottom(true); 
      mMediaPlayer = new MediaPlayer(); 
    mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); 

    mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { 
     @Override 
     public void onPrepared(MediaPlayer mp) { 
      togglePlayPause(); 
     } 
    }); 
    mMediaPlayer.setOnCompletionListener(new 
     MediaPlayer.OnCompletionListener() { 
     @Override 
     public void onCompletion(MediaPlayer mp) { 
      mPlayerControl.setImageResource(R.drawable.ic_play); 
     } 
    }); 
    mSelectedTrackTitle = (TextView)findViewById(R.id.selected_track_title); 
    mSelectedTrackImage = 
    (ImageView)findViewById(R.id.selected_track_image); 
    mSelectedTrackTitle.setSelected(true); 
    mPlayerControl = (ImageView)findViewById(R.id.player_control); 
    selected_track_ar=(TextView)findViewById(R.id.selected_track_ar); 

     mPlayerControl.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      togglePlayPause(); 
     } 
    }); 
     get(); 
      //getting the database reference 
     mDatabaseReference = FirebaseDatabase.getInstance().getReference 
     (Constants.DATABASE_PATH_UPLOADS); 

    //retrieving upload data from firebase database 
    mDatabaseReference.addValueEventListener(new ValueEventListener() { 
     @Override 
     public void onDataChange(DataSnapshot dataSnapshot) { 
      for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) { 
       Upload upload = postSnapshot.getValue(Upload.class); 
       uploadList.add(upload); 
      } 

       adapter = new CustomArrayAdapter(getApplicationContext(), 
      (ArrayList<Upload>) uploadList); 
      listView.setAdapter(adapter); 

     } 

      @Override 
     public void onCancelled(DatabaseError databaseError) { 

     } 
    }); 


    } 
    @Override 
    protected void onDestroy() { 
    super.onDestroy(); 

    Intent svc=new Intent(this, MySongServices.class); 
    startService(svc); 
      } 
    private void togglePlayPause() { 
    if (mMediaPlayer.isPlaying()) { 
     mMediaPlayer.pause(); 
     mPlayerControl.setImageResource(R.drawable.ic_play); 
    } else if(!mMediaPlayer.isPlaying()){ 
     mMediaPlayer.start(); 
     mPlayerControl.setImageResource(R.drawable.ic_pause); 
     mediaFileLengthInMilliseconds = mMediaPlayer.getDuration(); 
      primarySeekBarProgressUpdater(); 
       } 
    } 

     public void get() 
    { 
    seekBarProgress.setMax(99); // It means 100% .0-99 
    seekBarProgress.setOnTouchListener(this); 
    mMediaPlayer.setOnBufferingUpdateListener(this); 
    mMediaPlayer.setOnCompletionListener(this); 
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> adapterView, View view, int 
    i, long l) { 
      //getting the upload 
      Upload upload = uploadList.get(i); 
      mSelectedTrackTitle.setText(upload.getName()); 
      selected_track_ar.setText(upload.getAr()); 
      if (mMediaPlayer.isPlaying()) { 
       mMediaPlayer.stop(); 
       mMediaPlayer.reset(); 
       seekBarProgress.setProgress(0); 
           } 

      try { 
       mMediaPlayer.setDataSource(upload.getUrl()); 
       mMediaPlayer.prepare(); 
       show(); 

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

         } 
      }); 



     } 

     private void primarySeekBarProgressUpdater() { 
    try { 
     seekBarProgress.setProgress((int) (((float) 
      mMediaPlayer.getCurrentPosition() 
     /mediaFileLengthInMilliseconds) * 100)); 
     if (mMediaPlayer.isPlaying()) { 
      Runnable notification = new Runnable() { 
       public void run() { 
        primarySeekBarProgressUpdater(); 
       } 
      }; 
      handler.postDelayed(notification, 1000); 
     } 
    } 
    catch (NullPointerException e) 
    { 
     e.printStackTrace(); 
    } 
     } 




    @Override 
    public boolean onTouch(View v, MotionEvent event) { 
     if(v.getId() == R.id.length){ 
      /** Seekbar onTouch event handler. Method which seeks MediaPlayer 
      to 
    seekBar primary progress position*/ 
     if(mMediaPlayer.isPlaying()){ 
      SeekBar sb = (SeekBar)v; 
      int playPositionInMillisecconds = (mediaFileLengthInMilliseconds 
    /100) * sb.getProgress(); 
      mMediaPlayer.seekTo(playPositionInMillisecconds); 
     } 
    } 
    return false; 


} 

} 

答えて

0

は、私はあなたがAndroidのライフサイクルonPause()onResumeを使用する必要があると思います()は、アプリケーションがバックグラウンドとフォアグラウンドでいつ実行されているかを検出し、必要な条件に応じてすべての必要な値を更新する機能を備えています。

詳細については、次をご覧ください。https://developer.android.com/guide/components/activities/activity-lifecycle.html#onresume

関連する問題