2016-08-11 1 views
0

특정 TextView를 선택/터치 할 때 오디오를 재생하고 싶습니다. 내가 직면하고있는 문제는 텍스트를 3 번 ​​터치하여 재생해야한다는 것입니다.TextView를 터치 할 때 오디오 재생

활동이 시작되면 첫 번째 TexTView가 자동으로 스크롤되기 시작하고 두 번째는 처음 클릭 할 때만 스크롤됩니다. 클릭 한 번만 스크롤 할 TextViews와 FIRST 클릭 자체에서 오디오를 재생하기를 원합니다.

가 여기에 귀하의 첫 번째 클릭 켜기 layuot 파일

       <TextView 
           android:id="@+id/text1" 
           android:layout_width="wrap_content" 
           android:layout_height="wrap_content" 
           android:ellipsize="marquee" 
           android:marqueeRepeatLimit="1" 
           android:focusable="true" 
           android:focusableInTouchMode="true" 
           android:gravity="center" 
           android:paddingBottom="5dp" 
           android:paddingTop="5dp" 
           android:scrollHorizontally="true" 
           android:singleLine="true" 
           android:text="@string/text1" 
           android:textColor="#000000" 
           android:textSize="30dp" /> 

          <TextView 
           android:id="@+id/text2" 
           android:layout_width="wrap_content" 
           android:layout_height="wrap_content" 
           android:ellipsize="marquee" 
           android:marqueeRepeatLimit="1" 
           android:focusable="true" 
           android:focusableInTouchMode="true" 
           android:gravity="center" 
           android:paddingBottom="5dp" 
           android:paddingTop="5dp" 
           android:scrollHorizontally="true" 
           android:singleLine="true" 
           android:text="@string/text2" 
           android:textColor="#000000" 
           android:textSize="30dp" /> 

자바 파일

text1.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     if (mediaPlayer1 != null && mediaPlayer1.isPlaying()) { 
      text1.setClickable(false); 
     } 



     if (mediaPlayer1 == null) { 
      mediaPlayer1 = MediaPlayer.create(getActivity(), R.raw.audio1); 
     } 

     else { 
      text1.setClickable(true); 
      mediaPlayer1.start(); 
      play1.setVisibility(View.GONE); 
      pause1.setVisibility(View.VISIBLE); 
      repeatoff1.setVisibility(View.VISIBLE); 
      stop1.setVisibility(View.VISIBLE);} 
+0

두 번 클릭하면 재생되어야한다고 생각합니다. – Shaishav

+0

두 번 클릭하면 가끔씩 발생하지만 대개 3 번 클릭하면 발생합니다. 한 번의 클릭으로 완료 할 수있는 방법이 아닙니까? – Devansh

답변

0

에서 추출, 당신은 단지 MediaPlayer을 초기화하는 추가를 사용하고 있지 않습니다. 다음 코드를 입력하십시오 :

text1.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     if (mediaPlayer1 != null && mediaPlayer1.isPlaying()) { 
      text1.setClickable(false); 

      // release the media player 
      mediaPlayer1.release; 
      mediaPlayer1 = null; 
     } 



     if (mediaPlayer1 == null) { 
      mediaPlayer1 = MediaPlayer.create(getActivity(), R.raw.audio1); 
     } 
     // no need of else 
      text1.setClickable(true); 
      mediaPlayer1.start(); 
      play1.setVisibility(View.GONE); 
      pause1.setVisibility(View.VISIBLE); 
      repeatoff1.setVisibility(View.VISIBLE); 
      stop1.setVisibility(View.VISIBLE); 
    } 
관련 문제